Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for IHC lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from ihcsdk.ihccontroller import IHCController
8 
9 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
13 
14 from .const import CONF_DIMMABLE, CONF_OFF_ID, CONF_ON_ID, DOMAIN, IHC_CONTROLLER
15 from .entity import IHCEntity
16 from .util import async_pulse, async_set_bool, async_set_int
17 
18 
20  hass: HomeAssistant,
21  config: ConfigType,
22  add_entities: AddEntitiesCallback,
23  discovery_info: DiscoveryInfoType | None = None,
24 ) -> None:
25  """Set up the IHC lights platform."""
26  if discovery_info is None:
27  return
28  devices = []
29  for name, device in discovery_info.items():
30  ihc_id = device["ihc_id"]
31  product_cfg = device["product_cfg"]
32  product = device["product"]
33  # Find controller that corresponds with device id
34  controller_id = device["ctrl_id"]
35  ihc_controller: IHCController = hass.data[DOMAIN][controller_id][IHC_CONTROLLER]
36  ihc_off_id = product_cfg.get(CONF_OFF_ID)
37  ihc_on_id = product_cfg.get(CONF_ON_ID)
38  dimmable = product_cfg[CONF_DIMMABLE]
39  light = IhcLight(
40  ihc_controller,
41  controller_id,
42  name,
43  ihc_id,
44  ihc_off_id,
45  ihc_on_id,
46  dimmable,
47  product,
48  )
49  devices.append(light)
50  add_entities(devices)
51 
52 
54  """Representation of a IHC light.
55 
56  For dimmable lights, the associated IHC resource should be a light
57  level (integer). For non dimmable light the IHC resource should be
58  an on/off (boolean) resource
59  """
60 
61  def __init__(
62  self,
63  ihc_controller: IHCController,
64  controller_id: str,
65  name: str,
66  ihc_id: int,
67  ihc_off_id: int,
68  ihc_on_id: int,
69  dimmable=False,
70  product=None,
71  ) -> None:
72  """Initialize the light."""
73  super().__init__(ihc_controller, controller_id, name, ihc_id, product)
74  self._ihc_off_id_ihc_off_id = ihc_off_id
75  self._ihc_on_id_ihc_on_id = ihc_on_id
76  self._brightness_brightness = 0
77  self._dimmable_dimmable = dimmable
78  self._state_state = False
79 
80  if self._dimmable_dimmable:
81  self._attr_color_mode_attr_color_mode = ColorMode.BRIGHTNESS
82  else:
83  self._attr_color_mode_attr_color_mode = ColorMode.ONOFF
84  self._attr_supported_color_modes_attr_supported_color_modes = {self._attr_color_mode_attr_color_mode}
85 
86  @property
87  def brightness(self) -> int:
88  """Return the brightness of this light between 0..255."""
89  return self._brightness_brightness
90 
91  @property
92  def is_on(self) -> bool:
93  """Return true if light is on."""
94  return self._state_state
95 
96  async def async_turn_on(self, **kwargs: Any) -> None:
97  """Turn the light on."""
98  if ATTR_BRIGHTNESS in kwargs:
99  brightness = kwargs[ATTR_BRIGHTNESS]
100  elif (brightness := self._brightness_brightness) == 0:
101  brightness = 255
102 
103  if self._dimmable_dimmable:
104  await async_set_int(
105  self.hasshass, self.ihc_controllerihc_controller, self.ihc_idihc_id, int(brightness * 100 / 255)
106  )
107  elif self._ihc_on_id_ihc_on_id:
108  await async_pulse(self.hasshass, self.ihc_controllerihc_controller, self._ihc_on_id_ihc_on_id)
109  else:
110  await async_set_bool(self.hasshass, self.ihc_controllerihc_controller, self.ihc_idihc_id, True)
111 
112  async def async_turn_off(self, **kwargs: Any) -> None:
113  """Turn the light off."""
114  if self._dimmable_dimmable:
115  await async_set_int(self.hasshass, self.ihc_controllerihc_controller, self.ihc_idihc_id, 0)
116  elif self._ihc_off_id_ihc_off_id:
117  await async_pulse(self.hasshass, self.ihc_controllerihc_controller, self._ihc_off_id_ihc_off_id)
118  else:
119  await async_set_bool(self.hasshass, self.ihc_controllerihc_controller, self.ihc_idihc_id, False)
120 
121  def on_ihc_change(self, ihc_id, value):
122  """Handle IHC notifications."""
123  if isinstance(value, bool):
124  self._dimmable_dimmable = False
125  self._state_state = value != 0
126  else:
127  self._dimmable_dimmable = True
128  self._state_state = value > 0
129  if self._state_state:
130  self._brightness_brightness = int(value * 255 / 100)
131  self.schedule_update_ha_stateschedule_update_ha_state()
def on_ihc_change(self, ihc_id, value)
Definition: light.py:121
None async_turn_off(self, **Any kwargs)
Definition: light.py:112
None async_turn_on(self, **Any kwargs)
Definition: light.py:96
None __init__(self, IHCController ihc_controller, str controller_id, str name, int ihc_id, int ihc_off_id, int ihc_on_id, dimmable=False, product=None)
Definition: light.py:71
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 setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:24
None async_pulse(HomeAssistant hass, IHCController ihc_controller, int ihc_id)
Definition: util.py:12
asyncio.Future[bool] async_set_int(HomeAssistant hass, IHCController ihc_controller, int ihc_id, int value)
Definition: util.py:32
asyncio.Future[bool] async_set_bool(HomeAssistant hass, IHCController ihc_controller, int ihc_id, bool value)
Definition: util.py:22