From ea61160fd8a741320aa2e427ff9323b31cf6e72d Mon Sep 17 00:00:00 2001 From: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Date: Thu, 19 Oct 2023 10:55:30 +0200 Subject: [PATCH] Reuse function to check feature support on ViCare devices (#102211) * check feature support in utils function * rename parameters * restore severity * reorder parameters * Update .coveragerc --- .coveragerc | 1 + homeassistant/components/vicare/__init__.py | 3 +- .../components/vicare/binary_sensor.py | 31 +++++++++---------- homeassistant/components/vicare/button.py | 31 +++++++++---------- homeassistant/components/vicare/sensor.py | 31 +++++++++---------- homeassistant/components/vicare/utils.py | 26 ++++++++++++++++ 6 files changed, 70 insertions(+), 53 deletions(-) create mode 100644 homeassistant/components/vicare/utils.py diff --git a/.coveragerc b/.coveragerc index 1c07c2ea5ddb8..e682e1741e85e 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/vicare/__init__.py b/homeassistant/components/vicare/__init__.py index fcfe649750789..7a297ca8113e7 100644 --- a/homeassistant/components/vicare/__init__.py +++ b/homeassistant/components/vicare/__init__.py @@ -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] diff --git a/homeassistant/components/vicare/binary_sensor.py b/homeassistant/components/vicare/binary_sensor.py index c45192f05b441..4e3d8d05f9770 100644 --- a/homeassistant/components/vicare/binary_sensor.py +++ b/homeassistant/components/vicare/binary_sensor.py @@ -5,6 +5,7 @@ from dataclasses import dataclass import logging +from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig from PyViCare.PyViCareUtils import ( PyViCareInvalidDataError, PyViCareNotSupportedFeatureError, @@ -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__) @@ -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( diff --git a/homeassistant/components/vicare/button.py b/homeassistant/components/vicare/button.py index 4cbcf811fbc93..2516446a94e71 100644 --- a/homeassistant/components/vicare/button.py +++ b/homeassistant/components/vicare/button.py @@ -5,6 +5,7 @@ from dataclasses import dataclass import logging +from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig from PyViCare.PyViCareUtils import ( PyViCareInvalidDataError, PyViCareNotSupportedFeatureError, @@ -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__) @@ -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( diff --git a/homeassistant/components/vicare/sensor.py b/homeassistant/components/vicare/sensor.py index a7aa93f30bb85..325f3bf2d0715 100644 --- a/homeassistant/components/vicare/sensor.py +++ b/homeassistant/components/vicare/sensor.py @@ -7,6 +7,7 @@ import logging from PyViCare.PyViCareDevice import Device +from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig from PyViCare.PyViCareUtils import ( PyViCareInvalidDataError, PyViCareNotSupportedFeatureError, @@ -42,6 +43,7 @@ VICARE_UNIT_TO_UNIT_OF_MEASUREMENT, ) from .entity import ViCareEntity +from .utils import is_supported _LOGGER = logging.getLogger(__name__) @@ -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( diff --git a/homeassistant/components/vicare/utils.py b/homeassistant/components/vicare/utils.py new file mode 100644 index 0000000000000..19a75c0096245 --- /dev/null +++ b/homeassistant/components/vicare/utils.py @@ -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