Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Linear garage door light."""
2 
3 from typing import Any
4 
5 from linear_garage_door import Linear
6 
7 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DOMAIN
13 from .coordinator import LinearUpdateCoordinator
14 from .entity import LinearEntity
15 
16 SUPPORTED_SUBDEVICES = ["Light"]
17 
18 
20  hass: HomeAssistant,
21  config_entry: ConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up Linear Garage Door cover."""
25  coordinator: LinearUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
26  data = coordinator.data
27 
30  device_id=device_id,
31  device_name=data[device_id].name,
32  sub_device_id=subdev,
33  coordinator=coordinator,
34  )
35  for device_id in data
36  for subdev in data[device_id].subdevices
37  if subdev in SUPPORTED_SUBDEVICES
38  )
39 
40 
42  """Light for Linear devices."""
43 
44  _attr_color_mode = ColorMode.BRIGHTNESS
45  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
46  _attr_translation_key = "light"
47 
48  @property
49  def is_on(self) -> bool:
50  """Return if the light is on or not."""
51  return bool(self.sub_devicesub_device["On_B"] == "true")
52 
53  @property
54  def brightness(self) -> int | None:
55  """Return the brightness of the light."""
56  return round(int(self.sub_devicesub_device["On_P"]) / 100 * 255)
57 
58  async def async_turn_on(self, **kwargs: Any) -> None:
59  """Turn on the light."""
60 
61  async def _turn_on(linear: Linear) -> None:
62  """Turn on the light."""
63  if not kwargs:
64  await linear.operate_device(self._device_id_device_id, self._sub_device_id_sub_device_id, "On")
65  elif ATTR_BRIGHTNESS in kwargs:
66  brightness = round((kwargs[ATTR_BRIGHTNESS] / 255) * 100)
67  await linear.operate_device(
68  self._device_id_device_id, self._sub_device_id_sub_device_id, f"DimPercent:{brightness}"
69  )
70 
71  await self.coordinator.execute(_turn_on)
72 
73  async def async_turn_off(self, **kwargs: Any) -> None:
74  """Turn off the light."""
75 
76  await self.coordinator.execute(
77  lambda linear: linear.operate_device(
78  self._device_id_device_id, self._sub_device_id_sub_device_id, "Off"
79  )
80  )
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:23
def execute(hass, filename, source, data=None, return_response=False)
Definition: __init__.py:194