mirror of
https://github.com/home-assistant/core.git
synced 2025-08-07 06:35:10 +02:00
use base class
This commit is contained in:
@@ -93,10 +93,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
class ViCareEntity(Entity):
|
||||
"""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."""
|
||||
device_name = device_config.getModel()
|
||||
if hasMultipleDevices:
|
||||
if has_multiple_devices:
|
||||
device_name = f"{device_config.getModel()}-{device_config.getConfig().serial}"
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
|
@@ -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."""
|
||||
try:
|
||||
sensor.value_getter(vicare_api)
|
||||
@@ -118,12 +118,12 @@ def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: b
|
||||
vicare_api,
|
||||
device_config,
|
||||
sensor,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
|
||||
|
||||
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."""
|
||||
for description in sensor_descriptions:
|
||||
@@ -137,7 +137,7 @@ async def _entities_from_descriptions(
|
||||
current,
|
||||
device,
|
||||
description,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
if entity is not None:
|
||||
entities.append(entity)
|
||||
@@ -150,8 +150,9 @@ async def async_setup_entry(
|
||||
) -> None:
|
||||
"""Create the ViCare binary sensor devices."""
|
||||
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]:
|
||||
api = getattr(
|
||||
device,
|
||||
@@ -166,28 +167,28 @@ async def async_setup_entry(
|
||||
api,
|
||||
device,
|
||||
description,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
if entity is not None:
|
||||
entities.append(entity)
|
||||
|
||||
try:
|
||||
await _entities_from_descriptions(
|
||||
hass, entities, CIRCUIT_SENSORS, api.circuits, device, hasMultipleDevices
|
||||
hass, entities, CIRCUIT_SENSORS, api.circuits, device, has_multiple_devices
|
||||
)
|
||||
except PyViCareNotSupportedFeatureError:
|
||||
_LOGGER.info("No circuits found")
|
||||
|
||||
try:
|
||||
await _entities_from_descriptions(
|
||||
hass, entities, BURNER_SENSORS, api.burners, device, hasMultipleDevices
|
||||
hass, entities, BURNER_SENSORS, api.burners, device, has_multiple_devices
|
||||
)
|
||||
except PyViCareNotSupportedFeatureError:
|
||||
_LOGGER.info("No burners found")
|
||||
|
||||
try:
|
||||
await _entities_from_descriptions(
|
||||
hass, entities, COMPRESSOR_SENSORS, api.compressors, device, hasMultipleDevices
|
||||
hass, entities, COMPRESSOR_SENSORS, api.compressors, device, has_multiple_devices
|
||||
)
|
||||
except PyViCareNotSupportedFeatureError:
|
||||
_LOGGER.info("No compressors found")
|
||||
@@ -202,7 +203,7 @@ class ViCareBinarySensor(ViCareEntity, BinarySensorEntity):
|
||||
entity_description: ViCareBinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self, name, api, device_config, description: ViCareBinarySensorEntityDescription, hasMultipleDevices: bool
|
||||
self, name, api, device_config, description: ViCareBinarySensorEntityDescription, has_multiple_devices: bool
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
self.entity_description = description
|
||||
@@ -210,7 +211,7 @@ class ViCareBinarySensor(ViCareEntity, BinarySensorEntity):
|
||||
self._api = api
|
||||
self.entity_description = description
|
||||
self._device_config = device_config
|
||||
ViCareEntity.__init__(self, device_config, hasMultipleDevices)
|
||||
ViCareEntity.__init__(self, device_config, has_multiple_devices)
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
|
@@ -16,10 +16,9 @@ from homeassistant.components.button import ButtonEntity, ButtonEntityDescriptio
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import ViCareRequiredKeysMixinWithSet
|
||||
from . import ViCareEntity, ViCareRequiredKeysMixinWithSet
|
||||
from .const import (
|
||||
CONF_HEATING_TYPE,
|
||||
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."""
|
||||
_LOGGER.debug("Found device %s", name)
|
||||
try:
|
||||
@@ -70,6 +69,7 @@ def _build_entity(name, vicare_api, device_config, description):
|
||||
vicare_api,
|
||||
device_config,
|
||||
description,
|
||||
has_multiple_devices,
|
||||
)
|
||||
|
||||
|
||||
@@ -80,6 +80,9 @@ async def async_setup_entry(
|
||||
) -> None:
|
||||
"""Create the ViCare button 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]:
|
||||
api = getattr(
|
||||
@@ -95,6 +98,7 @@ async def async_setup_entry(
|
||||
api,
|
||||
device,
|
||||
description,
|
||||
has_multiple_devices,
|
||||
)
|
||||
if entity is not None:
|
||||
entities.append(entity)
|
||||
@@ -102,26 +106,20 @@ async def async_setup_entry(
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class ViCareButton(ButtonEntity):
|
||||
class ViCareButton(ViCareEntity, ButtonEntity):
|
||||
"""Representation of a ViCare button."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
entity_description: ViCareButtonEntityDescription
|
||||
|
||||
def __init__(
|
||||
self, name, api, device_config, description: ViCareButtonEntityDescription
|
||||
self, name, api, device_config, description: ViCareButtonEntityDescription, has_multiple_devices: bool
|
||||
) -> None:
|
||||
"""Initialize the button."""
|
||||
self.entity_description = description
|
||||
self._device_config = device_config
|
||||
self._api = api
|
||||
self._attr_device_info = DeviceInfo(
|
||||
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/",
|
||||
)
|
||||
ViCareEntity.__init__(self, device_config, has_multiple_devices)
|
||||
|
||||
def press(self) -> None:
|
||||
"""Handle the button press."""
|
||||
|
@@ -112,7 +112,7 @@ async def async_setup_entry(
|
||||
) -> None:
|
||||
"""Set up the ViCare climate platform."""
|
||||
entities = []
|
||||
hasMultipleDevices = (
|
||||
has_multiple_devices = (
|
||||
len(hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_LIST]) > 1
|
||||
)
|
||||
|
||||
@@ -134,7 +134,7 @@ async def async_setup_entry(
|
||||
api,
|
||||
circuit,
|
||||
device,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
entities.append(entity)
|
||||
|
||||
@@ -163,7 +163,7 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
|
||||
_attr_target_temperature_step = PRECISION_WHOLE
|
||||
_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."""
|
||||
|
||||
self._attr_name = name
|
||||
@@ -174,7 +174,7 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
|
||||
self._current_program = None
|
||||
self._current_action = None
|
||||
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:
|
||||
"""Let HA know there has been an update from the ViCare API."""
|
||||
|
@@ -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."""
|
||||
_LOGGER.debug("Found device %s", name)
|
||||
try:
|
||||
@@ -592,12 +592,12 @@ def _build_entity(name, vicare_api, device_config, sensor, hasMultipleDevices: b
|
||||
vicare_api,
|
||||
device_config,
|
||||
sensor,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
|
||||
|
||||
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."""
|
||||
for description in sensor_descriptions:
|
||||
@@ -611,7 +611,7 @@ async def _entities_from_descriptions(
|
||||
current,
|
||||
device,
|
||||
description,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
if entity is not None:
|
||||
entities.append(entity)
|
||||
@@ -624,8 +624,9 @@ async def async_setup_entry(
|
||||
) -> None:
|
||||
"""Create the ViCare sensor devices."""
|
||||
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]:
|
||||
api = getattr(
|
||||
device,
|
||||
@@ -640,28 +641,28 @@ async def async_setup_entry(
|
||||
api,
|
||||
device,
|
||||
description,
|
||||
hasMultipleDevices,
|
||||
has_multiple_devices,
|
||||
)
|
||||
if entity is not None:
|
||||
entities.append(entity)
|
||||
|
||||
try:
|
||||
await _entities_from_descriptions(
|
||||
hass, entities, CIRCUIT_SENSORS, api.circuits, device, hasMultipleDevices
|
||||
hass, entities, CIRCUIT_SENSORS, api.circuits, device, has_multiple_devices
|
||||
)
|
||||
except PyViCareNotSupportedFeatureError:
|
||||
_LOGGER.info("No circuits found")
|
||||
|
||||
try:
|
||||
await _entities_from_descriptions(
|
||||
hass, entities, BURNER_SENSORS, api.burners, device, hasMultipleDevices
|
||||
hass, entities, BURNER_SENSORS, api.burners, device, has_multiple_devices
|
||||
)
|
||||
except PyViCareNotSupportedFeatureError:
|
||||
_LOGGER.info("No burners found")
|
||||
|
||||
try:
|
||||
await _entities_from_descriptions(
|
||||
hass, entities, COMPRESSOR_SENSORS, api.compressors, device, hasMultipleDevices
|
||||
hass, entities, COMPRESSOR_SENSORS, api.compressors, device, has_multiple_devices
|
||||
)
|
||||
except PyViCareNotSupportedFeatureError:
|
||||
_LOGGER.info("No compressors found")
|
||||
@@ -676,14 +677,14 @@ class ViCareSensor(ViCareEntity, SensorEntity):
|
||||
entity_description: ViCareSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self, name, api, device_config, description: ViCareSensorEntityDescription, hasMultipleDevices: bool
|
||||
self, name, api, device_config, description: ViCareSensorEntityDescription, has_multiple_devices: bool
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
self.entity_description = description
|
||||
self._attr_name = name
|
||||
self._api = api
|
||||
self._device_config = device_config
|
||||
ViCareEntity.__init__(self, device_config, hasMultipleDevices)
|
||||
ViCareEntity.__init__(self, device_config, has_multiple_devices)
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
|
@@ -17,9 +17,9 @@ from homeassistant.components.water_heater import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_TEMPERATURE, PRECISION_TENTHS, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import ViCareEntity
|
||||
from .const import (
|
||||
CONF_HEATING_TYPE,
|
||||
DOMAIN,
|
||||
@@ -76,7 +76,9 @@ async def async_setup_entry(
|
||||
) -> None:
|
||||
"""Set up the ViCare climate platform."""
|
||||
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]:
|
||||
api = getattr(
|
||||
device,
|
||||
@@ -95,13 +97,14 @@ async def async_setup_entry(
|
||||
api,
|
||||
circuit,
|
||||
device,
|
||||
has_multiple_devices,
|
||||
)
|
||||
entities.append(entity)
|
||||
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class ViCareWater(WaterHeaterEntity):
|
||||
class ViCareWater(ViCareEntity, WaterHeaterEntity):
|
||||
"""Representation of the ViCare domestic hot water device."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
@@ -112,7 +115,7 @@ class ViCareWater(WaterHeaterEntity):
|
||||
_attr_max_temp = VICARE_TEMP_WATER_MAX
|
||||
_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."""
|
||||
self._attr_name = name
|
||||
self._api = api
|
||||
@@ -120,13 +123,7 @@ class ViCareWater(WaterHeaterEntity):
|
||||
self._attributes = {}
|
||||
self._current_mode = None
|
||||
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
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/",
|
||||
)
|
||||
ViCareEntity.__init__(self, device_config, has_multiple_devices)
|
||||
|
||||
def update(self) -> None:
|
||||
"""Let HA know there has been an update from the ViCare API."""
|
||||
|
Reference in New Issue
Block a user