use base class

This commit is contained in:
Christopher Fenner
2023-10-06 09:10:23 +02:00
parent f4f6d2106e
commit 3c0f19b990
6 changed files with 50 additions and 53 deletions

View File

@@ -93,10 +93,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class ViCareEntity(Entity): class ViCareEntity(Entity):
"""Base class for ViCare entities.""" """Base class for ViCare entities."""
def __init__(self, device_config, hasMultipleDevices: bool) -> None: def __init__(self, device_config, has_multiple_devices: bool) -> None:
"""Initialize the entity.""" """Initialize the entity."""
device_name = device_config.getModel() device_name = device_config.getModel()
if hasMultipleDevices: if has_multiple_devices:
device_name = f"{device_config.getModel()}-{device_config.getConfig().serial}" device_name = f"{device_config.getModel()}-{device_config.getConfig().serial}"
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(

View File

@@ -101,7 +101,7 @@ GLOBAL_SENSORS: tuple[ViCareBinarySensorEntityDescription, ...] = (
) )
def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: bool): def _build_entity(name, vicare_api, device_config, sensor, has_multiple_devices: 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)
@@ -118,12 +118,12 @@ def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: b
vicare_api, vicare_api,
device_config, device_config,
sensor, sensor,
hasMultipleDevices, has_multiple_devices,
) )
async def _entities_from_descriptions( async def _entities_from_descriptions(
hass, entities, sensor_descriptions, iterables, device, hasMultipleDevices hass, entities, sensor_descriptions, iterables, device, has_multiple_devices
): ):
"""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,7 +137,7 @@ async def _entities_from_descriptions(
current, current,
device, device,
description, description,
hasMultipleDevices, has_multiple_devices,
) )
if entity is not None: if entity is not None:
entities.append(entity) entities.append(entity)
@@ -150,8 +150,9 @@ 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 has_multiple_devices = (
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(
device, device,
@@ -166,28 +167,28 @@ async def async_setup_entry(
api, api,
device, device,
description, description,
hasMultipleDevices, has_multiple_devices,
) )
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, hasMultipleDevices hass, entities, CIRCUIT_SENSORS, api.circuits, device, has_multiple_devices
) )
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, hasMultipleDevices hass, entities, BURNER_SENSORS, api.burners, device, has_multiple_devices
) )
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, hasMultipleDevices hass, entities, COMPRESSOR_SENSORS, api.compressors, device, has_multiple_devices
) )
except PyViCareNotSupportedFeatureError: except PyViCareNotSupportedFeatureError:
_LOGGER.info("No compressors found") _LOGGER.info("No compressors found")
@@ -202,7 +203,7 @@ class ViCareBinarySensor(ViCareEntity, BinarySensorEntity):
entity_description: ViCareBinarySensorEntityDescription entity_description: ViCareBinarySensorEntityDescription
def __init__( def __init__(
self, name, api, device_config, description: ViCareBinarySensorEntityDescription, hasMultipleDevices: bool self, name, api, device_config, description: ViCareBinarySensorEntityDescription, has_multiple_devices: bool
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description self.entity_description = description
@@ -210,7 +211,7 @@ class ViCareBinarySensor(ViCareEntity, 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
ViCareEntity.__init__(self, device_config, hasMultipleDevices) ViCareEntity.__init__(self, device_config, has_multiple_devices)
@property @property
def available(self): def available(self):

View File

@@ -16,10 +16,9 @@ from homeassistant.components.button import ButtonEntity, ButtonEntityDescriptio
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ViCareRequiredKeysMixinWithSet from . import ViCareEntity, ViCareRequiredKeysMixinWithSet
from .const import ( from .const import (
CONF_HEATING_TYPE, CONF_HEATING_TYPE,
DOMAIN, DOMAIN,
@@ -52,7 +51,7 @@ BUTTON_DESCRIPTIONS: tuple[ViCareButtonEntityDescription, ...] = (
) )
def _build_entity(name, vicare_api, device_config, description): def _build_entity(name, vicare_api, device_config, description, has_multiple_devices: bool):
"""Create a ViCare button entity.""" """Create a ViCare button entity."""
_LOGGER.debug("Found device %s", name) _LOGGER.debug("Found device %s", name)
try: try:
@@ -70,6 +69,7 @@ def _build_entity(name, vicare_api, device_config, description):
vicare_api, vicare_api,
device_config, device_config,
description, description,
has_multiple_devices,
) )
@@ -80,6 +80,9 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Create the ViCare button entities.""" """Create the ViCare button entities."""
entities = [] entities = []
has_multiple_devices = (
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(
@@ -95,6 +98,7 @@ async def async_setup_entry(
api, api,
device, device,
description, description,
has_multiple_devices,
) )
if entity is not None: if entity is not None:
entities.append(entity) entities.append(entity)
@@ -102,26 +106,20 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class ViCareButton(ButtonEntity): class ViCareButton(ViCareEntity, ButtonEntity):
"""Representation of a ViCare button.""" """Representation of a ViCare button."""
_attr_has_entity_name = True _attr_has_entity_name = True
entity_description: ViCareButtonEntityDescription entity_description: ViCareButtonEntityDescription
def __init__( def __init__(
self, name, api, device_config, description: ViCareButtonEntityDescription self, name, api, device_config, description: ViCareButtonEntityDescription, has_multiple_devices: bool
) -> None: ) -> None:
"""Initialize the button.""" """Initialize the button."""
self.entity_description = description self.entity_description = description
self._device_config = device_config self._device_config = device_config
self._api = api self._api = api
self._attr_device_info = DeviceInfo( ViCareEntity.__init__(self, device_config, has_multiple_devices)
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/",
)
def press(self) -> None: def press(self) -> None:
"""Handle the button press.""" """Handle the button press."""

View File

@@ -112,7 +112,7 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up the ViCare climate platform.""" """Set up the ViCare climate platform."""
entities = [] entities = []
hasMultipleDevices = ( has_multiple_devices = (
len(hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]) > 1 len(hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]) > 1
) )
@@ -134,7 +134,7 @@ async def async_setup_entry(
api, api,
circuit, circuit,
device, device,
hasMultipleDevices, has_multiple_devices,
) )
entities.append(entity) entities.append(entity)
@@ -163,7 +163,7 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
_attr_target_temperature_step = PRECISION_WHOLE _attr_target_temperature_step = PRECISION_WHOLE
_attr_preset_modes = list(HA_TO_VICARE_PRESET_HEATING) _attr_preset_modes = list(HA_TO_VICARE_PRESET_HEATING)
def __init__(self, name, api, circuit, device_config, hasMultipleDevices: bool): def __init__(self, name, api, circuit, device_config, has_multiple_devices: bool):
"""Initialize the climate device.""" """Initialize the climate device."""
self._attr_name = name self._attr_name = name
@@ -174,7 +174,7 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
self._current_program = None self._current_program = None
self._current_action = None self._current_action = None
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}" self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
ViCareEntity.__init__(self, device_config, hasMultipleDevices) ViCareEntity.__init__(self, device_config, has_multiple_devices)
def update(self) -> None: def update(self) -> None:
"""Let HA know there has been an update from the ViCare API.""" """Let HA know there has been an update from the ViCare API."""

View File

@@ -574,7 +574,7 @@ COMPRESSOR_SENSORS: tuple[ViCareSensorEntityDescription, ...] = (
) )
def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: bool): def _build_entity(name, vicare_api, device_config, sensor, has_multiple_devices: bool):
"""Create a ViCare sensor entity.""" """Create a ViCare sensor entity."""
_LOGGER.debug("Found device %s", name) _LOGGER.debug("Found device %s", name)
try: try:
@@ -592,12 +592,12 @@ def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: b
vicare_api, vicare_api,
device_config, device_config,
sensor, sensor,
hasMultipleDevices, has_multiple_devices,
) )
async def _entities_from_descriptions( async def _entities_from_descriptions(
hass, entities, sensor_descriptions, iterables, device, hasMultipleDevices: bool hass, entities, sensor_descriptions, iterables, device, has_multiple_devices: bool
): ):
"""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:
@@ -611,7 +611,7 @@ async def _entities_from_descriptions(
current, current,
device, device,
description, description,
hasMultipleDevices, has_multiple_devices,
) )
if entity is not None: if entity is not None:
entities.append(entity) entities.append(entity)
@@ -624,8 +624,9 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Create the ViCare sensor devices.""" """Create the ViCare sensor devices."""
entities = [] entities = []
hasMultipleDevices = len(hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]) > 1 has_multiple_devices = (
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(
device, device,
@@ -640,28 +641,28 @@ async def async_setup_entry(
api, api,
device, device,
description, description,
hasMultipleDevices, has_multiple_devices,
) )
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, hasMultipleDevices hass, entities, CIRCUIT_SENSORS, api.circuits, device, has_multiple_devices
) )
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, hasMultipleDevices hass, entities, BURNER_SENSORS, api.burners, device, has_multiple_devices
) )
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, hasMultipleDevices hass, entities, COMPRESSOR_SENSORS, api.compressors, device, has_multiple_devices
) )
except PyViCareNotSupportedFeatureError: except PyViCareNotSupportedFeatureError:
_LOGGER.info("No compressors found") _LOGGER.info("No compressors found")
@@ -676,14 +677,14 @@ class ViCareSensor(ViCareEntity, SensorEntity):
entity_description: ViCareSensorEntityDescription entity_description: ViCareSensorEntityDescription
def __init__( def __init__(
self, name, api, device_config, description: ViCareSensorEntityDescription, hasMultipleDevices: bool self, name, api, device_config, description: ViCareSensorEntityDescription, has_multiple_devices: bool
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description self.entity_description = description
self._attr_name = name self._attr_name = name
self._api = api self._api = api
self._device_config = device_config self._device_config = device_config
ViCareEntity.__init__(self, device_config, hasMultipleDevices) ViCareEntity.__init__(self, device_config, has_multiple_devices)
@property @property
def available(self): def available(self):

View File

@@ -17,9 +17,9 @@ from homeassistant.components.water_heater import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_TENTHS, UnitOfTemperature from homeassistant.const import ATTR_TEMPERATURE, PRECISION_TENTHS, UnitOfTemperature
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ViCareEntity
from .const import ( from .const import (
CONF_HEATING_TYPE, CONF_HEATING_TYPE,
DOMAIN, DOMAIN,
@@ -76,7 +76,9 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up the ViCare climate platform.""" """Set up the ViCare climate platform."""
entities = [] entities = []
has_multiple_devices = (
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(
device, device,
@@ -95,13 +97,14 @@ async def async_setup_entry(
api, api,
circuit, circuit,
device, device,
has_multiple_devices,
) )
entities.append(entity) entities.append(entity)
async_add_entities(entities) async_add_entities(entities)
class ViCareWater(WaterHeaterEntity): class ViCareWater(ViCareEntity, WaterHeaterEntity):
"""Representation of the ViCare domestic hot water device.""" """Representation of the ViCare domestic hot water device."""
_attr_has_entity_name = True _attr_has_entity_name = True
@@ -112,7 +115,7 @@ class ViCareWater(WaterHeaterEntity):
_attr_max_temp = VICARE_TEMP_WATER_MAX _attr_max_temp = VICARE_TEMP_WATER_MAX
_attr_operation_list = list(HA_TO_VICARE_HVAC_DHW) _attr_operation_list = list(HA_TO_VICARE_HVAC_DHW)
def __init__(self, name, api, circuit, device_config): def __init__(self, name, api, circuit, device_config, has_multiple_devices: bool):
"""Initialize the DHW water_heater device.""" """Initialize the DHW water_heater device."""
self._attr_name = name self._attr_name = name
self._api = api self._api = api
@@ -120,13 +123,7 @@ class ViCareWater(WaterHeaterEntity):
self._attributes = {} self._attributes = {}
self._current_mode = None self._current_mode = None
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}" self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
self._attr_device_info = DeviceInfo( ViCareEntity.__init__(self, device_config, has_multiple_devices)
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/",
)
def update(self) -> None: def update(self) -> None:
"""Let HA know there has been an update from the ViCare API.""" """Let HA know there has been an update from the ViCare API."""