Set unique_id by base entity in ViCare integration (#104277)

* set unique_id in ViCareEntity

* remove individual unique_id functions

* remove description

* remove individual _attr_unique_id

* fix return types
pull/104299/head
Christopher Fenner 2023-11-21 09:53:02 +01:00 committed by GitHub
parent 6f41243410
commit 5805601a83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 50 deletions

View File

@ -198,28 +198,16 @@ class ViCareBinarySensor(ViCareEntity, BinarySensorEntity):
self, name, api, device_config, description: ViCareBinarySensorEntityDescription
) -> None:
"""Initialize the sensor."""
super().__init__(device_config)
super().__init__(device_config, api, description.key)
self.entity_description = description
self._attr_name = name
self._api = api
self._device_config = device_config
@property
def available(self):
def available(self) -> bool:
"""Return True if entity is available."""
return self._attr_is_on is not None
@property
def unique_id(self) -> str:
"""Return unique ID for this device."""
tmp_id = (
f"{self._device_config.getConfig().serial}-{self.entity_description.key}"
)
if hasattr(self._api, "id"):
return f"{tmp_id}-{self._api.id}"
return tmp_id
def update(self):
def update(self) -> None:
"""Update state of sensor."""
try:
with suppress(PyViCareNotSupportedFeatureError):

View File

@ -99,10 +99,8 @@ class ViCareButton(ViCareEntity, ButtonEntity):
self, name, api, device_config, description: ViCareButtonEntityDescription
) -> None:
"""Initialize the button."""
super().__init__(device_config)
super().__init__(device_config, api, description.key)
self.entity_description = description
self._device_config = device_config
self._api = api
def press(self) -> None:
"""Handle the button press."""
@ -117,13 +115,3 @@ class ViCareButton(ViCareEntity, ButtonEntity):
_LOGGER.error("Vicare API rate limit exceeded: %s", limit_exception)
except PyViCareInvalidDataError as invalid_data_exception:
_LOGGER.error("Invalid data from Vicare server: %s", invalid_data_exception)
@property
def unique_id(self) -> str:
"""Return unique ID for this device."""
tmp_id = (
f"{self._device_config.getConfig().serial}-{self.entity_description.key}"
)
if hasattr(self._api, "id"):
return f"{tmp_id}-{self._api.id}"
return tmp_id

View File

@ -150,13 +150,11 @@ class ViCareClimate(ViCareEntity, ClimateEntity):
def __init__(self, name, api, circuit, device_config) -> None:
"""Initialize the climate device."""
super().__init__(device_config)
super().__init__(device_config, api, circuit.id)
self._attr_name = name
self._api = api
self._circuit = circuit
self._attributes: dict[str, Any] = {}
self._current_program = None
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
def update(self) -> None:
"""Let HA know there has been an update from the ViCare API."""

View File

@ -1,4 +1,7 @@
"""Entities for the ViCare integration."""
from PyViCare.PyViCareDevice import Device as PyViCareDevice
from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
@ -10,8 +13,19 @@ class ViCareEntity(Entity):
_attr_has_entity_name = True
def __init__(self, device_config) -> None:
def __init__(
self,
device_config: PyViCareDeviceConfig,
device: PyViCareDevice,
unique_id_suffix: str,
) -> None:
"""Initialize the entity."""
self._api = device
self._attr_unique_id = f"{device_config.getConfig().serial}-{unique_id_suffix}"
# valid for compressors, circuits, burners (HeatingDeviceWithComponent)
if hasattr(device, "id"):
self._attr_unique_id += f"-{device.id}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_config.getConfig().serial)},

View File

@ -688,28 +688,16 @@ class ViCareSensor(ViCareEntity, SensorEntity):
self, name, api, device_config, description: ViCareSensorEntityDescription
) -> None:
"""Initialize the sensor."""
super().__init__(device_config)
super().__init__(device_config, api, description.key)
self.entity_description = description
self._attr_name = name
self._api = api
self._device_config = device_config
@property
def available(self):
def available(self) -> bool:
"""Return True if entity is available."""
return self._attr_native_value is not None
@property
def unique_id(self) -> str:
"""Return unique ID for this device."""
tmp_id = (
f"{self._device_config.getConfig().serial}-{self.entity_description.key}"
)
if hasattr(self._api, "id"):
return f"{tmp_id}-{self._api.id}"
return tmp_id
def update(self):
def update(self) -> None:
"""Update state of sensor."""
try:
with suppress(PyViCareNotSupportedFeatureError):

View File

@ -101,13 +101,11 @@ class ViCareWater(ViCareEntity, WaterHeaterEntity):
def __init__(self, name, api, circuit, device_config) -> None:
"""Initialize the DHW water_heater device."""
super().__init__(device_config)
super().__init__(device_config, api, circuit.id)
self._attr_name = name
self._api = api
self._circuit = circuit
self._attributes: dict[str, Any] = {}
self._current_mode = None
self._attr_unique_id = f"{device_config.getConfig().serial}-{circuit.id}"
def update(self) -> None:
"""Let HA know there has been an update from the ViCare API."""