Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base class for Acmeda Roller Blinds."""
2 
3 from __future__ import annotations
4 
5 import aiopulse
6 
7 from homeassistant.core import callback
8 from homeassistant.helpers import device_registry as dr, entity, entity_registry as er
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 
11 from .const import ACMEDA_ENTITY_REMOVE, DOMAIN, LOGGER
12 
13 
14 class AcmedaEntity(entity.Entity):
15  """Base representation of an Acmeda roller."""
16 
17  _attr_should_poll = False
18  _attr_has_entity_name = True
19 
20  def __init__(self, roller: aiopulse.Roller) -> None:
21  """Initialize the roller."""
22  self.rollerroller = roller
23 
24  async def async_remove_and_unregister(self) -> None:
25  """Unregister from registries and call entity remove function."""
26  LOGGER.error("Removing %s %s", self.__class__.__name__, self.unique_idunique_id)
27 
28  ent_registry = er.async_get(self.hass)
29  if self.entity_id in ent_registry.entities:
30  ent_registry.async_remove(self.entity_id)
31 
32  dev_registry = dr.async_get(self.hass)
33  device = dev_registry.async_get_device(identifiers={(DOMAIN, self.unique_idunique_id)})
34  if (
35  device is not None
36  and self.registry_entry is not None
37  and self.registry_entry.config_entry_id is not None
38  ):
39  dev_registry.async_update_device(
40  device.id, remove_config_entry_id=self.registry_entry.config_entry_id
41  )
42 
43  await self.async_remove(force_remove=True)
44 
45  async def async_added_to_hass(self) -> None:
46  """Entity has been added to hass."""
47  self.rollerroller.callback_subscribe(self.notify_updatenotify_update)
48 
49  self.async_on_remove(
51  self.hass,
52  ACMEDA_ENTITY_REMOVE.format(self.rollerroller.id),
53  self.async_remove_and_unregisterasync_remove_and_unregister,
54  )
55  )
56 
57  async def async_will_remove_from_hass(self) -> None:
58  """Entity being removed from hass."""
59  self.rollerroller.callback_unsubscribe(self.notify_updatenotify_update)
60 
61  @callback
62  def notify_update(self) -> None:
63  """Write updated device state information."""
64  LOGGER.debug("Device update notification received: %s", self.name)
65  self.async_write_ha_state()
66 
67  @property
68  def unique_id(self) -> str:
69  """Return the unique ID of this roller."""
70  return str(self.rollerroller.id)
71 
72  @property
73  def device_id(self) -> str:
74  """Return the ID of this roller."""
75  return self.rollerroller.id # type: ignore[no-any-return]
76 
77  @property
78  def device_info(self) -> dr.DeviceInfo:
79  """Return the device info."""
80  return dr.DeviceInfo(
81  identifiers={(DOMAIN, self.unique_idunique_id)},
82  manufacturer="Rollease Acmeda",
83  name=self.rollerroller.name,
84  via_device=(DOMAIN, self.rollerroller.hub.id),
85  )
None __init__(self, aiopulse.Roller roller)
Definition: entity.py:20
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103