1 """Base class for deCONZ devices."""
3 from __future__
import annotations
5 from pydeconz.models.deconz_device
import DeconzDevice
as PydeconzDevice
6 from pydeconz.models.group
import Group
as PydeconzGroup
7 from pydeconz.models.light
import LightBase
as PydeconzLightBase
8 from pydeconz.models.scene
import Scene
as PydeconzScene
9 from pydeconz.models.sensor
import SensorBase
as PydeconzSensorBase
16 from .const
import DOMAIN
as DECONZ_DOMAIN
17 from .hub
import DeconzHub
18 from .util
import serial_from_unique_id
21 PydeconzGroup | PydeconzLightBase | PydeconzSensorBase | PydeconzScene
26 """Common base for deconz entities and events."""
28 unique_id_suffix: str |
None =
None
35 """Set up device and add update callback to get data from websocket."""
36 self._device: _DeviceT = device
41 """Return a unique identifier for this device."""
42 assert isinstance(self._device, PydeconzDevice)
43 if self.unique_id_suffix
is not None:
44 return f
"{self._device.unique_id}-{self.unique_id_suffix}"
45 return self._device.unique_id
49 """Return a serial number for this device."""
50 assert isinstance(self._device, PydeconzDevice)
55 """Return a device description for device registry."""
56 assert isinstance(self._device, PydeconzDevice)
57 if self.serial
is None:
61 connections={(CONNECTION_ZIGBEE, self.serial)},
62 identifiers={(DECONZ_DOMAIN, self.serial)},
63 manufacturer=self._device.manufacturer,
64 model=self._device.model_id,
65 name=self._device.name,
66 sw_version=self._device.software_version,
67 via_device=(DECONZ_DOMAIN, self.hub.api.config.bridge_id),
71 class DeconzDevice[_DeviceT: _DeviceType](DeconzBase[_DeviceT], Entity):
72 """Representation of a deCONZ device."""
74 _attr_should_poll =
False
76 _name_suffix: str |
None =
None
77 _update_key: str |
None =
None
78 _update_keys: set[str] |
None =
None
87 """Set up device and add update callback to get data from websocket."""
89 self.hub.entities[self.TYPE].
add(self.unique_id)
91 self._attr_name = self._device.name
92 if self._name_suffix
is not None:
93 self._attr_name += f
" {self._name_suffix}"
95 if self._update_key
is not None:
96 self._update_keys = {self._update_key}
97 if self._update_keys
is not None:
98 self._update_keys |= {
"reachable"}
101 """Subscribe to device events."""
102 self._device.register_callback(self.async_update_callback)
103 self.hub.deconz_ids[self.entity_id] = self._device.deconz_id
104 self.async_on_remove(
107 self.hub.signal_reachable,
108 self.async_update_connection_state,
113 """Disconnect device object when removed."""
114 self._device.remove_callback(self.async_update_callback)
115 del self.hub.deconz_ids[self.entity_id]
116 self.hub.entities[self.TYPE].
remove(self.unique_id)
120 """Update the device's available state."""
121 self.async_write_ha_state()
125 """Update the device's state."""
126 if self.hub.ignore_state_updates:
129 if self._update_keys
is not None and not self._device.changed_keys.intersection(
134 self.async_write_ha_state()
138 """Return True if device is available."""
139 if isinstance(self._device, PydeconzScene):
140 return self.hub.available
141 return self.hub.available
and self._device.reachable
145 """Representation of a deCONZ scene."""
147 _attr_has_entity_name =
True
151 device: PydeconzScene,
154 """Set up a scene."""
157 self.
groupgroup = self.hub.api.groups[device.group_id]
163 """Describe a unique identifier for this scene."""
164 return f
"{self.hub.bridgeid}{self._device.deconz_id}"
167 """Describe a unique identifier for group this scene belongs to."""
168 return f
"{self.hub.bridgeid}-{self.group.deconz_id}"
172 """Return a unique identifier for this scene."""
177 """Return a device description for device registry."""
180 manufacturer=
"Dresden Elektronik",
181 model=
"deCONZ group",
182 name=self.
groupgroup.name,
183 via_device=(DECONZ_DOMAIN, self.hub.api.config.bridge_id),
str get_device_identifier(self)
DeviceInfo device_info(self)
str get_parent_identifier(self)
None __init__(self, PydeconzScene device, DeconzHub hub)
bool add(self, _T matcher)
bool remove(self, _T matcher)
DeviceInfo|None device_info(self)
None async_update_connection_state(self)
None async_will_remove_from_hass(self)
None async_added_to_hass(self)
None async_update_callback(self)
None __init__(self, _DeviceT device, DeconzHub hub)
str|None serial_from_unique_id(str|None unique_id)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)