diff --git a/.coveragerc b/.coveragerc index 1c07c2ea5ddb8f..e682e1741e85ef 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 fcfe6497507897..7a297ca8113e70 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 c45192f05b441a..4e3d8d05f97709 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 4cbcf811fbc931..2516446a94e71b 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 a7aa93f30bb854..325f3bf2d07158 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 00000000000000..19a75c00962458 --- /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