Reuse function to check feature support on ViCare devices (#102211)

* check feature support in utils function

* rename parameters

* restore severity

* reorder parameters

* Update .coveragerc
This commit is contained in:
Christopher Fenner 2023-10-19 10:55:30 +02:00 committed by GitHub
parent 327bdce561
commit ea61160fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 53 deletions

View File

@ -1473,6 +1473,7 @@ omit =
homeassistant/components/vicare/climate.py homeassistant/components/vicare/climate.py
homeassistant/components/vicare/entity.py homeassistant/components/vicare/entity.py
homeassistant/components/vicare/sensor.py homeassistant/components/vicare/sensor.py
homeassistant/components/vicare/utils.py
homeassistant/components/vicare/water_heater.py homeassistant/components/vicare/water_heater.py
homeassistant/components/vilfo/__init__.py homeassistant/components/vilfo/__init__.py
homeassistant/components/vilfo/sensor.py homeassistant/components/vilfo/sensor.py

View File

@ -40,10 +40,9 @@ class ViCareRequiredKeysMixin:
@dataclass() @dataclass()
class ViCareRequiredKeysMixinWithSet: class ViCareRequiredKeysMixinWithSet(ViCareRequiredKeysMixin):
"""Mixin for required keys with setter.""" """Mixin for required keys with setter."""
value_getter: Callable[[Device], bool]
value_setter: Callable[[Device], bool] value_setter: Callable[[Device], bool]

View File

@ -5,6 +5,7 @@ from contextlib import suppress
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareUtils import ( from PyViCare.PyViCareUtils import (
PyViCareInvalidDataError, PyViCareInvalidDataError,
PyViCareNotSupportedFeatureError, PyViCareNotSupportedFeatureError,
@ -24,6 +25,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ViCareRequiredKeysMixin from . import ViCareRequiredKeysMixin
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG
from .entity import ViCareEntity from .entity import ViCareEntity
from .utils import is_supported
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -102,25 +104,20 @@ GLOBAL_SENSORS: tuple[ViCareBinarySensorEntityDescription, ...] = (
def _build_entity( 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.""" """Create a ViCare binary sensor entity."""
try: if is_supported(name, entity_description, vicare_api):
sensor.value_getter(vicare_api) return ViCareBinarySensor(
_LOGGER.debug("Found entity %s", name) name,
except PyViCareNotSupportedFeatureError: vicare_api,
_LOGGER.info("Feature not supported %s", name) device_config,
return None entity_description,
except AttributeError: )
_LOGGER.debug("Attribute Error %s", name) return None
return None
return ViCareBinarySensor(
name,
vicare_api,
device_config,
sensor,
)
async def _entities_from_descriptions( async def _entities_from_descriptions(

View File

@ -5,6 +5,7 @@ from contextlib import suppress
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareUtils import ( from PyViCare.PyViCareUtils import (
PyViCareInvalidDataError, PyViCareInvalidDataError,
PyViCareNotSupportedFeatureError, PyViCareNotSupportedFeatureError,
@ -21,6 +22,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ViCareRequiredKeysMixinWithSet from . import ViCareRequiredKeysMixinWithSet
from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG from .const import DOMAIN, VICARE_API, VICARE_DEVICE_CONFIG
from .entity import ViCareEntity from .entity import ViCareEntity
from .utils import is_supported
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -47,26 +49,21 @@ BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = (
def _build_entity( 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.""" """Create a ViCare button entity."""
_LOGGER.debug("Found device %s", name) _LOGGER.debug("Found device %s", name)
try: if is_supported(name, entity_description, vicare_api):
description.value_getter(vicare_api) return ViCareButton(
_LOGGER.debug("Found entity %s", name) name,
except PyViCareNotSupportedFeatureError: vicare_api,
_LOGGER.info("Feature not supported %s", name) device_config,
return None entity_description,
except AttributeError: )
_LOGGER.debug("Attribute Error %s", name) return None
return None
return ViCareButton(
name,
vicare_api,
device_config,
description,
)
async def async_setup_entry( async def async_setup_entry(

View File

@ -7,6 +7,7 @@ from dataclasses import dataclass
import logging import logging
from PyViCare.PyViCareDevice import Device from PyViCare.PyViCareDevice import Device
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from PyViCare.PyViCareUtils import ( from PyViCare.PyViCareUtils import (
PyViCareInvalidDataError, PyViCareInvalidDataError,
PyViCareNotSupportedFeatureError, PyViCareNotSupportedFeatureError,
@ -42,6 +43,7 @@ from .const import (
VICARE_UNIT_TO_UNIT_OF_MEASUREMENT, VICARE_UNIT_TO_UNIT_OF_MEASUREMENT,
) )
from .entity import ViCareEntity from .entity import ViCareEntity
from .utils import is_supported
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -574,26 +576,21 @@ COMPRESSOR_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
def _build_entity( 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.""" """Create a ViCare sensor entity."""
_LOGGER.debug("Found device %s", name) _LOGGER.debug("Found device %s", name)
try: if is_supported(name, entity_description, vicare_api):
sensor.value_getter(vicare_api) return ViCareSensor(
_LOGGER.debug("Found entity %s", name) name,
except PyViCareNotSupportedFeatureError: vicare_api,
_LOGGER.info("Feature not supported %s", name) device_config,
return None entity_description,
except AttributeError: )
_LOGGER.debug("Attribute Error %s", name) return None
return None
return ViCareSensor(
name,
vicare_api,
device_config,
sensor,
)
async def _entities_from_descriptions( async def _entities_from_descriptions(

View File

@ -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