Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Qwikswitch devices."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.components.light import ATTR_BRIGHTNESS
6 from homeassistant.core import callback
7 from homeassistant.helpers.dispatcher import async_dispatcher_connect
8 from homeassistant.helpers.entity import Entity
9 
10 from . import DOMAIN
11 
12 
14  """Qwikswitch Entity base."""
15 
16  _attr_should_poll = False
17 
18  def __init__(self, qsid, name):
19  """Initialize the QSEntity."""
20  self._name_name = name
21  self.qsidqsid = qsid
22 
23  @property
24  def name(self):
25  """Return the name of the sensor."""
26  return self._name_name
27 
28  @property
29  def unique_id(self):
30  """Return a unique identifier for this sensor."""
31  return f"qs{self.qsid}"
32 
33  @callback
34  def update_packet(self, packet):
35  """Receive update packet from QSUSB. Match dispather_send signature."""
36  self.async_write_ha_stateasync_write_ha_state()
37 
38  async def async_added_to_hass(self):
39  """Listen for updates from QSUSb via dispatcher."""
40  self.async_on_removeasync_on_remove(
41  async_dispatcher_connect(self.hasshass, self.qsidqsid, self.update_packetupdate_packet)
42  )
43 
44 
46  """Representation of a Qwikswitch Toggle Entity.
47 
48  Implemented:
49  - QSLight extends QSToggleEntity and Light[2] (ToggleEntity[1])
50  - QSSwitch extends QSToggleEntity and SwitchEntity[3] (ToggleEntity[1])
51 
52  [1] /helpers/entity.py
53  [2] /components/light/__init__.py
54  [3] /components/switch/__init__.py
55  """
56 
57  def __init__(self, qsid, qsusb):
58  """Initialize the ToggleEntity."""
59  self.devicedevice = qsusb.devices[qsid]
60  super().__init__(qsid, self.devicedevice.name)
61 
62  @property
63  def is_on(self):
64  """Check if device is on (non-zero)."""
65  return self.devicedevice.value > 0
66 
67  async def async_turn_on(self, **kwargs):
68  """Turn the device on."""
69  new = kwargs.get(ATTR_BRIGHTNESS, 255)
70  self.hasshass.data[DOMAIN].devices.set_value(self.qsidqsid, new)
71 
72  async def async_turn_off(self, **_):
73  """Turn the device off."""
74  self.hasshass.data[DOMAIN].devices.set_value(self.qsidqsid, 0)
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103