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:
parent
327bdce561
commit
ea61160fd8
6 changed files with 70 additions and 53 deletions
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ from contextlib import suppress
|
|||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
from PyViCare.PyViCareUtils import (
|
||||
PyViCareInvalidDataError,
|
||||
PyViCareNotSupportedFeatureError,
|
||||
|
@ -24,6 +25,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
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 @@ GLOBAL_SENSORS: tuple[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(
|
||||
|
|
|
@ -5,6 +5,7 @@ from contextlib import suppress
|
|||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
from PyViCare.PyViCareUtils import (
|
||||
PyViCareInvalidDataError,
|
||||
PyViCareNotSupportedFeatureError,
|
||||
|
@ -21,6 +22,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
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 @@ BUTTON_DESCRIPTIONS: tuple[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(
|
||||
|
|
|
@ -7,6 +7,7 @@ from dataclasses import dataclass
|
|||
import logging
|
||||
|
||||
from PyViCare.PyViCareDevice import Device
|
||||
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
|
||||
from PyViCare.PyViCareUtils import (
|
||||
PyViCareInvalidDataError,
|
||||
PyViCareNotSupportedFeatureError,
|
||||
|
@ -42,6 +43,7 @@ from .const import (
|
|||
VICARE_UNIT_TO_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
from .entity import ViCareEntity
|
||||
from .utils import is_supported
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -574,26 +576,21 @@ COMPRESSOR_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
|
|||
|
||||
|
||||
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(
|
||||
|
|
26
homeassistant/components/vicare/utils.py
Normal file
26
homeassistant/components/vicare/utils.py
Normal 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
|
Loading…
Add table
Reference in a new issue