Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """The Z-Wave-Me WS integration."""
2 
3 from zwave_me_ws import ZWaveMeData
4 
5 from homeassistant.core import callback
6 from homeassistant.helpers.device_registry import DeviceInfo
7 from homeassistant.helpers.dispatcher import async_dispatcher_connect
8 from homeassistant.helpers.entity import Entity
9 
10 from .const import DOMAIN
11 
12 
14  """Representation of a ZWaveMe device."""
15 
16  def __init__(self, controller, device):
17  """Initialize the device."""
18  self.controllercontroller = controller
19  self.devicedevice = device
20  self._attr_name_attr_name = device.title
21  self._attr_unique_id: str = (
22  f"{self.controller.config.unique_id}-{self.device.id}"
23  )
24  self._attr_should_poll_attr_should_poll = False
25 
26  @property
27  def device_info(self) -> DeviceInfo:
28  """Return device specific attributes."""
29  return DeviceInfo(
30  identifiers={(DOMAIN, self.devicedevice.deviceIdentifier)},
31  name=self._attr_name_attr_name,
32  manufacturer=self.devicedevice.manufacturer,
33  sw_version=self.devicedevice.firmware,
34  suggested_area=self.devicedevice.locationName,
35  )
36 
37  async def async_added_to_hass(self) -> None:
38  """Connect to an updater."""
39  self.async_on_removeasync_on_remove(
41  self.hasshass, f"ZWAVE_ME_INFO_{self.device.id}", self.get_new_dataget_new_data
42  )
43  )
44  self.async_on_removeasync_on_remove(
46  self.hasshass,
47  f"ZWAVE_ME_UNAVAILABLE_{self.device.id}",
48  self.set_unavailable_statusset_unavailable_status,
49  )
50  )
51  self.async_on_removeasync_on_remove(
53  self.hasshass, f"ZWAVE_ME_DESTROY_{self.device.id}", self.delete_entitydelete_entity
54  )
55  )
56 
57  @callback
58  def get_new_data(self, new_data: ZWaveMeData) -> None:
59  """Update info in the HAss."""
60  self.devicedevice = new_data
61  self._attr_available_attr_available = not new_data.isFailed
62  self.async_write_ha_stateasync_write_ha_state()
63 
64  @callback
66  """Update status in the HAss."""
67  self._attr_available_attr_available = False
68  self.async_write_ha_stateasync_write_ha_state()
69 
70  @callback
71  def delete_entity(self) -> None:
72  """Remove this entity."""
73  self.hasshass.async_create_task(self.async_removeasync_remove(force_remove=True))
def __init__(self, controller, device)
Definition: entity.py:16
None get_new_data(self, ZWaveMeData new_data)
Definition: entity.py:58
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_remove(self, *bool force_remove=False)
Definition: entity.py:1387
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103