1 """Support for Xiaomi Gateway Light."""
19 from .const
import DOMAIN, GATEWAYS_KEY
20 from .entity
import XiaomiDevice
22 _LOGGER = logging.getLogger(__name__)
27 config_entry: ConfigEntry,
28 async_add_entities: AddEntitiesCallback,
30 """Perform the setup for Xiaomi devices."""
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"):
43 """Representation of a XiaomiGatewayLight."""
45 _attr_color_mode = ColorMode.HS
46 _attr_supported_color_modes = {ColorMode.HS}
48 def __init__(self, device, name, xiaomi_hub, config_entry):
49 """Initialize the XiaomiGatewayLight."""
54 super().
__init__(device, name, xiaomi_hub, config_entry)
58 """Return true if it is on."""
62 """Parse data sent by gateway."""
71 rgbhexstr = f
"{value:x}"
72 if len(rgbhexstr) > 8:
74 "Light RGB data error. Can't be more than 8 characters. Received: %s",
79 rgbhexstr = rgbhexstr.zfill(8)
80 rgbhex = bytes.fromhex(rgbhexstr)
81 rgba = struct.unpack(
"BBBB", rgbhex)
86 self.
_hs_hs = color_util.color_RGB_to_hs(*rgb)
92 """Return the brightness of this light between 0..255."""
97 """Return the hs color value."""
101 """Turn the light on."""
102 if ATTR_HS_COLOR
in kwargs:
103 self.
_hs_hs = kwargs[ATTR_HS_COLOR]
105 if ATTR_BRIGHTNESS
in kwargs:
106 self.
_brightness_brightness =
int(100 * kwargs[ATTR_BRIGHTNESS] / 255)
108 rgb = color_util.color_hs_to_RGB(*self.
_hs_hs)
110 rgbhex = binascii.hexlify(struct.pack(
"BBBB", *rgba)).decode(
"ASCII")
111 rgbhex =
int(rgbhex, 16)
118 """Turn the light off."""
None turn_off(self, **Any kwargs)
def parse_data(self, data, raw_data)
def __init__(self, device, name, xiaomi_hub, config_entry)
def turn_on(self, **kwargs)
None schedule_update_ha_state(self, bool force_refresh=False)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)