Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Xiaomi Gateway Light."""
2 
3 import binascii
4 import logging
5 import struct
6 from typing import Any
7 
9  ATTR_BRIGHTNESS,
10  ATTR_HS_COLOR,
11  ColorMode,
12  LightEntity,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 import homeassistant.util.color as color_util
18 
19 from .const import DOMAIN, GATEWAYS_KEY
20 from .entity import XiaomiDevice
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  hass: HomeAssistant,
27  config_entry: ConfigEntry,
28  async_add_entities: AddEntitiesCallback,
29 ) -> None:
30  """Perform the setup for Xiaomi devices."""
31  entities = []
32  gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id]
33  for device in gateway.devices["light"]:
34  model = device["model"]
35  if model in ("gateway", "gateway.v3"):
36  entities.append(
37  XiaomiGatewayLight(device, "Gateway Light", gateway, config_entry)
38  )
39  async_add_entities(entities)
40 
41 
43  """Representation of a XiaomiGatewayLight."""
44 
45  _attr_color_mode = ColorMode.HS
46  _attr_supported_color_modes = {ColorMode.HS}
47 
48  def __init__(self, device, name, xiaomi_hub, config_entry):
49  """Initialize the XiaomiGatewayLight."""
50  self._data_key_data_key = "rgb"
51  self._hs_hs = (0, 0)
52  self._brightness_brightness = 100
53 
54  super().__init__(device, name, xiaomi_hub, config_entry)
55 
56  @property
57  def is_on(self):
58  """Return true if it is on."""
59  return self._state_state_state
60 
61  def parse_data(self, data, raw_data):
62  """Parse data sent by gateway."""
63  value = data.get(self._data_key_data_key)
64  if value is None:
65  return False
66 
67  if value == 0:
68  self._state_state_state = False
69  return True
70 
71  rgbhexstr = f"{value:x}"
72  if len(rgbhexstr) > 8:
73  _LOGGER.error(
74  "Light RGB data error. Can't be more than 8 characters. Received: %s",
75  rgbhexstr,
76  )
77  return False
78 
79  rgbhexstr = rgbhexstr.zfill(8)
80  rgbhex = bytes.fromhex(rgbhexstr)
81  rgba = struct.unpack("BBBB", rgbhex)
82  brightness = rgba[0]
83  rgb = rgba[1:]
84 
85  self._brightness_brightness = brightness
86  self._hs_hs = color_util.color_RGB_to_hs(*rgb)
87  self._state_state_state = True
88  return True
89 
90  @property
91  def brightness(self):
92  """Return the brightness of this light between 0..255."""
93  return int(255 * self._brightness_brightness / 100)
94 
95  @property
96  def hs_color(self):
97  """Return the hs color value."""
98  return self._hs_hs
99 
100  def turn_on(self, **kwargs):
101  """Turn the light on."""
102  if ATTR_HS_COLOR in kwargs:
103  self._hs_hs = kwargs[ATTR_HS_COLOR]
104 
105  if ATTR_BRIGHTNESS in kwargs:
106  self._brightness_brightness = int(100 * kwargs[ATTR_BRIGHTNESS] / 255)
107 
108  rgb = color_util.color_hs_to_RGB(*self._hs_hs)
109  rgba = (self._brightness_brightness, *rgb)
110  rgbhex = binascii.hexlify(struct.pack("BBBB", *rgba)).decode("ASCII")
111  rgbhex = int(rgbhex, 16)
112 
113  if self._write_to_hub_write_to_hub(self._sid_sid, **{self._data_key_data_key: rgbhex}):
114  self._state_state_state = True
115  self.schedule_update_ha_stateschedule_update_ha_state()
116 
117  def turn_off(self, **kwargs: Any) -> None:
118  """Turn the light off."""
119  if self._write_to_hub_write_to_hub(self._sid_sid, **{self._data_key_data_key: 0}):
120  self._state_state_state = False
121  self.schedule_update_ha_stateschedule_update_ha_state()
def __init__(self, device, name, xiaomi_hub, config_entry)
Definition: light.py:48
None schedule_update_ha_state(self, bool force_refresh=False)
Definition: entity.py:1244
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: light.py:29