Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity for Surepetcare."""
2 
3 from __future__ import annotations
4 
5 from abc import abstractmethod
6 
7 from surepy.entities import SurepyEntity
8 
9 from homeassistant.core import callback
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.update_coordinator import CoordinatorEntity
12 
13 from .const import DOMAIN
14 from .coordinator import SurePetcareDataCoordinator
15 
16 
17 class SurePetcareEntity(CoordinatorEntity[SurePetcareDataCoordinator]):
18  """An implementation for Sure Petcare Entities."""
19 
20  def __init__(
21  self,
22  surepetcare_id: int,
23  coordinator: SurePetcareDataCoordinator,
24  ) -> None:
25  """Initialize a Sure Petcare entity."""
26  super().__init__(coordinator)
27 
28  self._id_id = surepetcare_id
29 
30  surepy_entity = coordinator.data[surepetcare_id]
31 
32  if surepy_entity.name:
33  self._device_name_device_name = surepy_entity.name.capitalize()
34  else:
35  self._device_name_device_name = surepy_entity.type.name.capitalize().replace("_", " ")
36 
37  self._device_id_device_id = f"{surepy_entity.household_id}-{surepetcare_id}"
38  self._attr_device_info_attr_device_info = DeviceInfo(
39  configuration_url="https://surepetcare.io/dashboard/",
40  identifiers={(DOMAIN, self._device_id_device_id)},
41  name=self._device_name_device_name,
42  manufacturer="Sure Petcare",
43  model=surepy_entity.type.name.capitalize().replace("_", " "),
44  )
45  self._update_attr_update_attr(coordinator.data[surepetcare_id])
46 
47  @abstractmethod
48  @callback
49  def _update_attr(self, surepy_entity: SurepyEntity) -> None:
50  """Update the state and attributes."""
51 
52  @callback
53  def _handle_coordinator_update(self) -> None:
54  """Get the latest data and update the state."""
55  self._update_attr_update_attr(self.coordinator.data[self._id_id])
56  self.async_write_ha_state()
None _update_attr(self, SurepyEntity surepy_entity)
Definition: entity.py:49
None __init__(self, int surepetcare_id, SurePetcareDataCoordinator coordinator)
Definition: entity.py:24