Update binary_sensor.py

This commit is contained in:
Christopher Fenner
2023-10-06 08:33:56 +02:00
committed by GitHub
parent 003faefb12
commit db4bc71f1c

View File

@@ -22,7 +22,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ViCareRequiredKeysMixin from . import ViCareEntity, ViCareRequiredKeysMixin
from .const import ( from .const import (
CONF_HEATING_TYPE, CONF_HEATING_TYPE,
DOMAIN, DOMAIN,
@@ -102,7 +102,7 @@ GLOBAL_SENSORS: tuple[ViCareBinarySensorEntityDescription, ...] = (
) )
def _build_entity(name, vicare_api, device_config, sensor): def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: bool):
"""Create a ViCare binary sensor entity.""" """Create a ViCare binary sensor entity."""
try: try:
sensor.value_getter(vicare_api) sensor.value_getter(vicare_api)
@@ -119,11 +119,12 @@ def _build_entity(name, vicare_api, device_config, sensor):
vicare_api, vicare_api,
device_config, device_config,
sensor, sensor,
hasMultipleDevices,
) )
async def _entities_from_descriptions( async def _entities_from_descriptions(
hass, entities, sensor_descriptions, iterables, device hass, entities, sensor_descriptions, iterables, device, hasMultipleDevices
): ):
"""Create entities from descriptions and list of burners/circuits.""" """Create entities from descriptions and list of burners/circuits."""
for description in sensor_descriptions: for description in sensor_descriptions:
@@ -137,6 +138,7 @@ async def _entities_from_descriptions(
current, current,
device, device,
description, description,
hasMultipleDevices,
) )
if entity is not None: if entity is not None:
entities.append(entity) entities.append(entity)
@@ -149,6 +151,7 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Create the ViCare binary sensor devices.""" """Create the ViCare binary sensor devices."""
entities = [] entities = []
hasMultipleDevices = len(hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]) > 1
for device in hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]: for device in hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]:
api = getattr( api = getattr(
@@ -164,27 +167,28 @@ async def async_setup_entry(
api, api,
device, device,
description, description,
hasMultipleDevices,
) )
if entity is not None: if entity is not None:
entities.append(entity) entities.append(entity)
try: try:
await _entities_from_descriptions( await _entities_from_descriptions(
hass, entities, CIRCUIT_SENSORS, api.circuits, device hass, entities, CIRCUIT_SENSORS, api.circuits, device, hasMultipleDevices
) )
except PyViCareNotSupportedFeatureError: except PyViCareNotSupportedFeatureError:
_LOGGER.info("No circuits found") _LOGGER.info("No circuits found")
try: try:
await _entities_from_descriptions( await _entities_from_descriptions(
hass, entities, BURNER_SENSORS, api.burners, device hass, entities, BURNER_SENSORS, api.burners, device, hasMultipleDevices
) )
except PyViCareNotSupportedFeatureError: except PyViCareNotSupportedFeatureError:
_LOGGER.info("No burners found") _LOGGER.info("No burners found")
try: try:
await _entities_from_descriptions( await _entities_from_descriptions(
hass, entities, COMPRESSOR_SENSORS, api.compressors, device hass, entities, COMPRESSOR_SENSORS, api.compressors, device, hasMultipleDevices
) )
except PyViCareNotSupportedFeatureError: except PyViCareNotSupportedFeatureError:
_LOGGER.info("No compressors found") _LOGGER.info("No compressors found")
@@ -192,14 +196,14 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class ViCareBinarySensor(BinarySensorEntity): class ViCareBinarySensor(ViCareEntity, BinarySensorEntity):
"""Representation of a ViCare sensor.""" """Representation of a ViCare sensor."""
_attr_has_entity_name = True _attr_has_entity_name = True
entity_description: ViCareBinarySensorEntityDescription entity_description: ViCareBinarySensorEntityDescription
def __init__( def __init__(
self, name, api, device_config, description: ViCareBinarySensorEntityDescription self, name, api, device_config, description: ViCareBinarySensorEntityDescription, hasMultipleDevices: bool
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description self.entity_description = description
@@ -207,13 +211,7 @@ class ViCareBinarySensor(BinarySensorEntity):
self._api = api self._api = api
self.entity_description = description self.entity_description = description
self._device_config = device_config self._device_config = device_config
self._attr_device_info = DeviceInfo( ViCareEntity.__init__(self, device_config, hasMultipleDevices)
identifiers={(DOMAIN, device_config.getConfig().serial)},
name=f"{device_config.getModel()}-{device_config.getConfig().serial}",
manufacturer="Viessmann",
model=device_config.getModel(),
configuration_url="https://developer.viessmann.com/",
)
@property @property
def available(self): def available(self):