Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Hive light devices."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import TYPE_CHECKING, Any
7 
9  ATTR_BRIGHTNESS,
10  ATTR_COLOR_TEMP,
11  ATTR_HS_COLOR,
12  ColorMode,
13  LightEntity,
14 )
15 from homeassistant.config_entries import ConfigEntry
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 import homeassistant.util.color as color_util
19 
20 from . import refresh_system
21 from .const import ATTR_MODE, DOMAIN
22 from .entity import HiveEntity
23 
24 if TYPE_CHECKING:
25  from apyhiveapi import Hive
26 
27 PARALLEL_UPDATES = 0
28 SCAN_INTERVAL = timedelta(seconds=15)
29 
30 
32  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
33 ) -> None:
34  """Set up Hive thermostat based on a config entry."""
35 
36  hive: Hive = hass.data[DOMAIN][entry.entry_id]
37  devices = hive.session.deviceList.get("light")
38  if not devices:
39  return
40  async_add_entities((HiveDeviceLight(hive, dev) for dev in devices), True)
41 
42 
44  """Hive Active Light Device."""
45 
46  def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None:
47  """Initialise hive light."""
48  super().__init__(hive, hive_device)
49  if self.devicedevicedevice["hiveType"] == "warmwhitelight":
50  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
51  self._attr_color_mode_attr_color_mode = ColorMode.BRIGHTNESS
52  elif self.devicedevicedevice["hiveType"] == "tuneablelight":
53  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.COLOR_TEMP}
54  self._attr_color_mode_attr_color_mode = ColorMode.COLOR_TEMP
55  elif self.devicedevicedevice["hiveType"] == "colourtuneablelight":
56  self._attr_supported_color_modes_attr_supported_color_modes = {ColorMode.COLOR_TEMP, ColorMode.HS}
57  self._attr_color_mode_attr_color_mode = ColorMode.UNKNOWN
58 
59  self._attr_min_mireds_attr_min_mireds = 153
60  self._attr_max_mireds_attr_max_mireds = 370
61 
62  @refresh_system
63  async def async_turn_on(self, **kwargs: Any) -> None:
64  """Instruct the light to turn on."""
65  new_brightness = None
66  new_color_temp = None
67  new_color = None
68  if ATTR_BRIGHTNESS in kwargs:
69  tmp_new_brightness = kwargs[ATTR_BRIGHTNESS]
70  percentage_brightness = (tmp_new_brightness / 255) * 100
71  new_brightness = int(round(percentage_brightness / 5.0) * 5.0)
72  if new_brightness == 0:
73  new_brightness = 5
74  if ATTR_COLOR_TEMP in kwargs:
75  tmp_new_color_temp = kwargs[ATTR_COLOR_TEMP]
76  new_color_temp = round(1000000 / tmp_new_color_temp)
77  if ATTR_HS_COLOR in kwargs:
78  get_new_color = kwargs[ATTR_HS_COLOR]
79  hue = int(get_new_color[0])
80  saturation = int(get_new_color[1])
81  new_color = (hue, saturation, 100)
82 
83  await self.hivehive.light.turnOn(
84  self.devicedevicedevice, new_brightness, new_color_temp, new_color
85  )
86 
87  @refresh_system
88  async def async_turn_off(self, **kwargs: Any) -> None:
89  """Instruct the light to turn off."""
90  await self.hivehive.light.turnOff(self.devicedevicedevice)
91 
92  async def async_update(self) -> None:
93  """Update all Node data from Hive."""
94  await self.hivehive.session.updateData(self.devicedevicedevice)
95  self.devicedevicedevice = await self.hivehive.light.getLight(self.devicedevicedevice)
96  self.attributes.update(self.devicedevicedevice.get("attributes", {}))
97  self._attr_extra_state_attributes_attr_extra_state_attributes = {
98  ATTR_MODE: self.attributes.get(ATTR_MODE),
99  }
100  self._attr_available_attr_available = self.devicedevicedevice["deviceData"].get("online")
101  if self._attr_available_attr_available:
102  self._attr_is_on_attr_is_on = self.devicedevicedevice["status"]["state"]
103  self._attr_brightness_attr_brightness = self.devicedevicedevice["status"]["brightness"]
104  if self.devicedevicedevice["hiveType"] == "tuneablelight":
105  self._attr_color_temp_attr_color_temp = self.devicedevicedevice["status"].get("color_temp")
106  if self.devicedevicedevice["hiveType"] == "colourtuneablelight":
107  if self.devicedevicedevice["status"]["mode"] == "COLOUR":
108  rgb = self.devicedevicedevice["status"]["hs_color"]
109  self._attr_hs_color_attr_hs_color = color_util.color_RGB_to_hs(*rgb)
110  self._attr_color_mode_attr_color_mode = ColorMode.HS
111  else:
112  self._attr_color_temp_attr_color_temp = self.devicedevicedevice["status"].get("color_temp")
113  self._attr_color_mode_attr_color_mode = ColorMode.COLOR_TEMP
None __init__(self, Hive hive, dict[str, Any] hive_device)
Definition: light.py:46
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: light.py:33
IssData update(pyiss.ISS iss)
Definition: __init__.py:33