Skip to content

Commit

Permalink
Reuse function to check feature support on ViCare devices (home-assis…
Browse files Browse the repository at this point in the history
…tant#102211)

* check feature support in utils function

* rename parameters

* restore severity

* reorder parameters

* Update .coveragerc
  • Loading branch information
CFenner committed Oct 19, 2023
1 parent 327bdce commit ea61160
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 53 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@ omit =
homeassistant/components/vicare/climate.py
homeassistant/components/vicare/entity.py
homeassistant/components/vicare/sensor.py
homeassistant/components/vicare/utils.py
homeassistant/components/vicare/water_heater.py
homeassistant/components/vilfo/__init__.py
homeassistant/components/vilfo/sensor.py
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/vicare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ class ViCareRequiredKeysMixin:


@dataclass()
class ViCareRequiredKeysMixinWithSet:
class ViCareRequiredKeysMixinWithSet(ViCareRequiredKeysMixin):
"""Mixin for required keys with setter."""

value_getter: Callable[[Device], bool]
value_setter: Callable[[Device], bool]


Expand Down
31 changes: 14 additions & 17 deletions homeassistant/components/vicare/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dataclasses import dataclass
import logging

from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareUtils import (
PyViCareInvalidDataError,
PyViCareNotSupportedFeatureError,
Expand All @@ -24,6 +25,7 @@
from . import ViCareRequiredKeysMixin
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG
from .entity import ViCareEntity
from .utils import is_supported

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -102,25 +104,20 @@ class ViCareBinarySensorEntityDescription(


def _build_entity(
name: str, vicare_api, device_config, sensor: ViCareBinarySensorEntityDescription
name: str,
vicare_api,
device_config: PyViCareDeviceConfig,
entity_description: ViCareBinarySensorEntityDescription,
):
"""Create a ViCare binary sensor entity."""
try:
sensor.value_getter(vicare_api)
_LOGGER.debug("Found entity %s", name)
except PyViCareNotSupportedFeatureError:
_LOGGER.info("Feature not supported %s", name)
return None
except AttributeError:
_LOGGER.debug("Attribute Error %s", name)
return None

return ViCareBinarySensor(
name,
vicare_api,
device_config,
sensor,
)
if is_supported(name, entity_description, vicare_api):
return ViCareBinarySensor(
name,
vicare_api,
device_config,
entity_description,
)
return None


async def _entities_from_descriptions(
Expand Down
31 changes: 14 additions & 17 deletions homeassistant/components/vicare/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dataclasses import dataclass
import logging

from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareUtils import (
PyViCareInvalidDataError,
PyViCareNotSupportedFeatureError,
Expand All @@ -21,6 +22,7 @@
from . import ViCareRequiredKeysMixinWithSet
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG
from .entity import ViCareEntity
from .utils import is_supported

_LOGGER = logging.getLogger(__name__)

Expand All @@ -47,26 +49,21 @@ class ViCareButtonEntityDescription(


def _build_entity(
name: str, vicare_api, device_config, description: ViCareButtonEntityDescription
name: str,
vicare_api,
device_config: PyViCareDeviceConfig,
entity_description: ViCareButtonEntityDescription,
):
"""Create a ViCare button entity."""
_LOGGER.debug("Found device %s", name)
try:
description.value_getter(vicare_api)
_LOGGER.debug("Found entity %s", name)
except PyViCareNotSupportedFeatureError:
_LOGGER.info("Feature not supported %s", name)
return None
except AttributeError:
_LOGGER.debug("Attribute Error %s", name)
return None

return ViCareButton(
name,
vicare_api,
device_config,
description,
)
if is_supported(name, entity_description, vicare_api):
return ViCareButton(
name,
vicare_api,
device_config,
entity_description,
)
return None


async def async_setup_entry(
Expand Down
31 changes: 14 additions & 17 deletions homeassistant/components/vicare/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging

from PyViCare.PyViCareDevice import Device
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareUtils import (
PyViCareInvalidDataError,
PyViCareNotSupportedFeatureError,
Expand Down Expand Up @@ -42,6 +43,7 @@
VICARE_UNIT_TO_UNIT_OF_MEASUREMENT,
)
from .entity import ViCareEntity
from .utils import is_supported

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -574,26 +576,21 @@ class ViCareSensorEntityDescription(SensorEntityDescription, ViCareRequiredKeysM


def _build_entity(
name: str, vicare_api, device_config, sensor: ViCareSensorEntityDescription
name: str,
vicare_api,
device_config: PyViCareDeviceConfig,
entity_description: ViCareSensorEntityDescription,
):
"""Create a ViCare sensor entity."""
_LOGGER.debug("Found device %s", name)
try:
sensor.value_getter(vicare_api)
_LOGGER.debug("Found entity %s", name)
except PyViCareNotSupportedFeatureError:
_LOGGER.info("Feature not supported %s", name)
return None
except AttributeError:
_LOGGER.debug("Attribute Error %s", name)
return None

return ViCareSensor(
name,
vicare_api,
device_config,
sensor,
)
if is_supported(name, entity_description, vicare_api):
return ViCareSensor(
name,
vicare_api,
device_config,
entity_description,
)
return None


async def _entities_from_descriptions(
Expand Down
26 changes: 26 additions & 0 deletions homeassistant/components/vicare/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""ViCare helpers functions."""
import logging

from PyViCare.PyViCareUtils import PyViCareNotSupportedFeatureError

from . import ViCareRequiredKeysMixin

_LOGGER = logging.getLogger(__name__)


def is_supported(
name: str,
entity_description: ViCareRequiredKeysMixin,
vicare_device,
) -> bool:
"""Check if the PyViCare device supports the requested sensor."""
try:
entity_description.value_getter(vicare_device)
_LOGGER.debug("Found entity %s", name)
except PyViCareNotSupportedFeatureError:
_LOGGER.info("Feature not supported %s", name)
return False
except AttributeError as error:
_LOGGER.debug("Attribute Error %s: %s", name, error)
return False
return True

0 comments on commit ea61160

Please sign in to comment.