Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Light platform for Advantage Air integration."""
2 
3 from typing import Any
4 
5 from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
6 from homeassistant.core import HomeAssistant
7 from homeassistant.helpers.device_registry import DeviceInfo
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 
10 from . import AdvantageAirDataConfigEntry
11 from .const import ADVANTAGE_AIR_STATE_ON, DOMAIN as ADVANTAGE_AIR_DOMAIN
12 from .entity import AdvantageAirEntity, AdvantageAirThingEntity
13 from .models import AdvantageAirData
14 
15 
17  hass: HomeAssistant,
18  config_entry: AdvantageAirDataConfigEntry,
19  async_add_entities: AddEntitiesCallback,
20 ) -> None:
21  """Set up AdvantageAir light platform."""
22 
23  instance = config_entry.runtime_data
24 
25  entities: list[LightEntity] = []
26  if my_lights := instance.coordinator.data.get("myLights"):
27  for light in my_lights["lights"].values():
28  if light.get("relay"):
29  entities.append(AdvantageAirLight(instance, light))
30  else:
31  entities.append(AdvantageAirLightDimmable(instance, light))
32  if things := instance.coordinator.data.get("myThings"):
33  for thing in things["things"].values():
34  if thing["channelDipState"] == 4: # 4 = "Light (on/off)""
35  entities.append(AdvantageAirThingLight(instance, thing))
36  elif thing["channelDipState"] == 5: # 5 = "Light (Dimmable)""
37  entities.append(AdvantageAirThingLightDimmable(instance, thing))
38  async_add_entities(entities)
39 
40 
42  """Representation of Advantage Air Light."""
43 
44  _attr_color_mode = ColorMode.ONOFF
45  _attr_supported_color_modes = {ColorMode.ONOFF}
46  _attr_name = None
47 
48  def __init__(self, instance: AdvantageAirData, light: dict[str, Any]) -> None:
49  """Initialize an Advantage Air Light."""
50  super().__init__(instance)
51 
52  self._id: str = light["id"]
53  self._attr_unique_id += f"-{self._id}"
54  self._attr_device_info_attr_device_info = DeviceInfo(
55  identifiers={(ADVANTAGE_AIR_DOMAIN, self._attr_unique_id)},
56  via_device=(ADVANTAGE_AIR_DOMAIN, self.coordinator.data["system"]["rid"]),
57  manufacturer="Advantage Air",
58  model=light.get("moduleType"),
59  name=light["name"],
60  )
61  self.async_update_stateasync_update_state = self.update_handle_factoryupdate_handle_factory(
62  instance.api.lights.async_update_state, self._id
63  )
64 
65  @property
66  def _data(self) -> dict[str, Any]:
67  """Return the light object."""
68  return self.coordinator.data["myLights"]["lights"][self._id]
69 
70  @property
71  def is_on(self) -> bool:
72  """Return if the light is on."""
73  return self._data_data["state"] == ADVANTAGE_AIR_STATE_ON
74 
75  async def async_turn_on(self, **kwargs: Any) -> None:
76  """Turn the light on."""
77  await self.async_update_stateasync_update_state(True)
78 
79  async def async_turn_off(self, **kwargs: Any) -> None:
80  """Turn the light off."""
81  await self.async_update_stateasync_update_state(False)
82 
83 
85  """Representation of Advantage Air Dimmable Light."""
86 
87  _attr_color_mode = ColorMode.BRIGHTNESS
88  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
89 
90  def __init__(self, instance: AdvantageAirData, light: dict[str, Any]) -> None:
91  """Initialize an Advantage Air Dimmable Light."""
92  super().__init__(instance, light)
93  self.async_update_valueasync_update_value = self.update_handle_factoryupdate_handle_factory(
94  instance.api.lights.async_update_value, self._id
95  )
96 
97  @property
98  def brightness(self) -> int:
99  """Return the brightness of this light between 0..255."""
100  return round(self._data_data["value"] * 255 / 100)
101 
102  async def async_turn_on(self, **kwargs: Any) -> None:
103  """Turn the light on and optionally set the brightness."""
104  if ATTR_BRIGHTNESS in kwargs:
105  return await self.async_update_valueasync_update_value(round(kwargs[ATTR_BRIGHTNESS] / 2.55))
106  return await self.async_update_stateasync_update_state(True)
107 
108 
110  """Representation of Advantage Air Light controlled by myThings."""
111 
112  _attr_color_mode = ColorMode.ONOFF
113  _attr_supported_color_modes = {ColorMode.ONOFF}
114 
115 
117  """Representation of Advantage Air Dimmable Light controlled by myThings."""
118 
119  _attr_color_mode = ColorMode.BRIGHTNESS
120  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
121 
122  @property
123  def brightness(self) -> int:
124  """Return the brightness of this light between 0..255."""
125  return round(self._data_data["value"] * 255 / 100)
126 
127  async def async_turn_on(self, **kwargs: Any) -> None:
128  """Turn the light on by setting the brightness."""
129  await self.async_update_valueasync_update_value(round(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55))
None __init__(self, AdvantageAirData instance, dict[str, Any] light)
Definition: light.py:90
None __init__(self, AdvantageAirData instance, dict[str, Any] light)
Definition: light.py:48
None async_setup_entry(HomeAssistant hass, AdvantageAirDataConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:20