Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Modern Forms Fan lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiomodernforms.const import LIGHT_POWER_OFF, LIGHT_POWER_ON
8 import voluptuous as vol
9 
10 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers import entity_platform
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16  percentage_to_ranged_value,
17  ranged_value_to_percentage,
18 )
19 
20 from . import modernforms_exception_handler
21 from .const import (
22  ATTR_SLEEP_TIME,
23  CLEAR_TIMER,
24  DOMAIN,
25  OPT_BRIGHTNESS,
26  OPT_ON,
27  SERVICE_CLEAR_LIGHT_SLEEP_TIMER,
28  SERVICE_SET_LIGHT_SLEEP_TIMER,
29 )
30 from .coordinator import ModernFormsDataUpdateCoordinator
31 from .entity import ModernFormsDeviceEntity
32 
33 BRIGHTNESS_RANGE = (1, 255)
34 
35 
37  hass: HomeAssistant,
38  config_entry: ConfigEntry,
39  async_add_entities: AddEntitiesCallback,
40 ) -> None:
41  """Set up a Modern Forms platform from config entry."""
42 
43  coordinator: ModernFormsDataUpdateCoordinator = hass.data[DOMAIN][
44  config_entry.entry_id
45  ]
46 
47  # if no light unit installed no light entity
48  if not coordinator.data.info.light_type:
49  return
50 
51  platform = entity_platform.async_get_current_platform()
52 
53  platform.async_register_entity_service(
54  SERVICE_SET_LIGHT_SLEEP_TIMER,
55  {
56  vol.Required(ATTR_SLEEP_TIME): vol.All(
57  vol.Coerce(int), vol.Range(min=1, max=1440)
58  ),
59  },
60  "async_set_light_sleep_timer",
61  )
62 
63  platform.async_register_entity_service(
64  SERVICE_CLEAR_LIGHT_SLEEP_TIMER,
65  None,
66  "async_clear_light_sleep_timer",
67  )
68 
70  [
72  entry_id=config_entry.entry_id, coordinator=coordinator
73  )
74  ]
75  )
76 
77 
79  """Defines a Modern Forms light."""
80 
81  _attr_color_mode = ColorMode.BRIGHTNESS
82  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
83  _attr_translation_key = "light"
84 
85  def __init__(
86  self, entry_id: str, coordinator: ModernFormsDataUpdateCoordinator
87  ) -> None:
88  """Initialize Modern Forms light."""
89  super().__init__(
90  entry_id=entry_id,
91  coordinator=coordinator,
92  )
93  self._attr_unique_id_attr_unique_id = f"{self.coordinator.data.info.mac_address}"
94 
95  @property
96  def brightness(self) -> int | None:
97  """Return the brightness of this light between 1..255."""
98  return round(
100  BRIGHTNESS_RANGE, self.coordinator.data.state.light_brightness
101  )
102  )
103 
104  @property
105  def is_on(self) -> bool:
106  """Return the state of the light."""
107  return bool(self.coordinator.data.state.light_on)
108 
109  @modernforms_exception_handler
110  async def async_turn_off(self, **kwargs: Any) -> None:
111  """Turn off the light."""
112  await self.coordinator.modern_forms.light(on=LIGHT_POWER_OFF)
113 
114  @modernforms_exception_handler
115  async def async_turn_on(self, **kwargs: Any) -> None:
116  """Turn on the light."""
117  data = {OPT_ON: LIGHT_POWER_ON}
118 
119  if ATTR_BRIGHTNESS in kwargs:
120  data[OPT_BRIGHTNESS] = ranged_value_to_percentage(
121  BRIGHTNESS_RANGE, kwargs[ATTR_BRIGHTNESS]
122  )
123 
124  await self.coordinator.modern_forms.light(**data)
125 
126  @modernforms_exception_handler
128  self,
129  sleep_time: int,
130  ) -> None:
131  """Set a Modern Forms light sleep timer."""
132  await self.coordinator.modern_forms.light(sleep=sleep_time * 60)
133 
134  @modernforms_exception_handler
136  self,
137  ) -> None:
138  """Clear a Modern Forms light sleep timer."""
139  await self.coordinator.modern_forms.light(sleep=CLEAR_TIMER)
None __init__(self, str entry_id, ModernFormsDataUpdateCoordinator coordinator)
Definition: light.py:87
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:40
float percentage_to_ranged_value(tuple[float, float] low_high_range, float percentage)
Definition: percentage.py:81
int ranged_value_to_percentage(tuple[float, float] low_high_range, float value)
Definition: percentage.py:64