Files
core/homeassistant/components/withings/entity.py
T

71 lines
2.2 KiB
Python

"""Base entity for Withings."""
from typing import Any, override
from aiowithings import Device
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import (
WithingsDataUpdateCoordinator,
WithingsDeviceDataUpdateCoordinator,
)
class WithingsEntity[_T: WithingsDataUpdateCoordinator[Any]](CoordinatorEntity[_T]):
"""Base class for withings entities."""
_attr_has_entity_name = True
def __init__(
self,
coordinator: _T,
key: str,
) -> None:
"""Initialize the Withings entity."""
super().__init__(coordinator)
self._attr_unique_id = f"withings_{coordinator.config_entry.unique_id}_{key}" # pylint: disable=home-assistant-entity-unique-id-redundant-domain
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, str(coordinator.config_entry.unique_id))},
)
class WithingsDeviceEntity(WithingsEntity[WithingsDeviceDataUpdateCoordinator]):
"""Base class for withings device entities."""
def __init__(
self,
coordinator: WithingsDeviceDataUpdateCoordinator,
device_id: str,
key: str,
) -> None:
"""Initialize the Withings entity."""
super().__init__(coordinator, key)
self._attr_unique_id = f"{device_id}_{key}"
self.device_id = device_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, device_id)},
manufacturer="Withings",
name=self.device.raw_model,
model=self.device.raw_model,
via_device_id=dr.async_get_device_id_by_identifier(
coordinator.hass,
(DOMAIN, str(coordinator.config_entry.unique_id)),
config_entry_id=coordinator.config_entry.entry_id,
),
)
@property
@override
def available(self) -> bool:
"""Return True if entity is available."""
return super().available and self.device_id in self.coordinator.data
@property
def device(self) -> Device:
"""Return the Withings device."""
return self.coordinator.data[self.device_id]