Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Binary sensor platform for integration_blueprint."""
2 
3 from typing import Any
4 
5 from pydeako import Deako
6 
7 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from . import DeakoConfigEntry
13 from .const import DOMAIN
14 
15 # Model names
16 MODEL_SMART = "smart"
17 MODEL_DIMMER = "dimmer"
18 
19 
21  hass: HomeAssistant,
22  config: DeakoConfigEntry,
23  add_entities: AddEntitiesCallback,
24 ) -> None:
25  """Configure the platform."""
26  client = config.runtime_data
27 
28  add_entities([DeakoLightEntity(client, uuid) for uuid in client.get_devices()])
29 
30 
32  """Deako LightEntity class."""
33 
34  _attr_has_entity_name = True
35  _attr_name = None
36  _attr_is_on = False
37  _attr_available = True
38 
39  client: Deako
40 
41  def __init__(self, client: Deako, uuid: str) -> None:
42  """Save connection reference."""
43  self.clientclient = client
44  self._attr_unique_id_attr_unique_id = uuid
45 
46  dimmable = client.is_dimmable(uuid)
47 
48  model = MODEL_SMART
49  self._attr_color_mode_attr_color_mode = ColorMode.ONOFF
50  if dimmable:
51  model = MODEL_DIMMER
52  self._attr_color_mode_attr_color_mode = ColorMode.BRIGHTNESS
53 
54  self._attr_supported_color_modes_attr_supported_color_modes = {self._attr_color_mode_attr_color_mode}
55 
56  self._attr_device_info_attr_device_info = DeviceInfo(
57  identifiers={(DOMAIN, uuid)},
58  name=client.get_name(uuid),
59  manufacturer="Deako",
60  model=model,
61  )
62 
63  client.set_state_callback(uuid, self.on_updateon_update)
64  self.updateupdate() # set initial state
65 
66  def on_update(self) -> None:
67  """State update callback."""
68  self.updateupdate()
69  self.schedule_update_ha_stateschedule_update_ha_state()
70 
71  async def control_device(self, power: bool, dim: int | None = None) -> None:
72  """Control entity state via client."""
73  assert self._attr_unique_id_attr_unique_id is not None
74  await self.clientclient.control_device(self._attr_unique_id_attr_unique_id, power, dim)
75 
76  async def async_turn_on(self, **kwargs: Any) -> None:
77  """Turn on the light."""
78  dim = None
79  if ATTR_BRIGHTNESS in kwargs:
80  dim = round(kwargs[ATTR_BRIGHTNESS] / 2.55, 0)
81  await self.control_devicecontrol_device(True, dim)
82 
83  async def async_turn_off(self, **kwargs: Any) -> None:
84  """Turn off the device."""
85  await self.control_devicecontrol_device(False)
86 
87  def update(self) -> None:
88  """Call to update state."""
89  assert self._attr_unique_id_attr_unique_id is not None
90  state = self.clientclient.get_state(self._attr_unique_id_attr_unique_id) or {}
91  self._attr_is_on_attr_is_on_attr_is_on = bool(state.get("power", False))
92  if (
93  self._attr_supported_color_modes_attr_supported_color_modes is not None
94  and ColorMode.BRIGHTNESS in self._attr_supported_color_modes_attr_supported_color_modes
95  ):
96  self._attr_brightness_attr_brightness = int(round(state.get("dim", 0) * 2.55))
None control_device(self, bool power, int|None dim=None)
Definition: light.py:71
None __init__(self, Deako client, str uuid)
Definition: light.py:41
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None async_setup_entry(HomeAssistant hass, DeakoConfigEntry config, AddEntitiesCallback add_entities)
Definition: light.py:24
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26