From fa3c8993b73a04be798c55e27e9a77e689d29e2c Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Sat, 12 Sep 2020 00:06:11 +0300 Subject: [PATCH] Add color temp for Aqara Bulb --- custom_components/xiaomi_gateway3/gateway3.py | 10 +++++-- custom_components/xiaomi_gateway3/light.py | 29 +++++++++++++++---- custom_components/xiaomi_gateway3/remote.py | 4 +-- custom_components/xiaomi_gateway3/switch.py | 4 +-- custom_components/xiaomi_gateway3/utils.py | 9 +++++- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/custom_components/xiaomi_gateway3/gateway3.py b/custom_components/xiaomi_gateway3/gateway3.py index 1a49d0cb..c0296766 100644 --- a/custom_components/xiaomi_gateway3/gateway3.py +++ b/custom_components/xiaomi_gateway3/gateway3.py @@ -467,13 +467,17 @@ def process_ble_event(self, raw: bytes): for handler in self.updates[did]: handler(payload) - def send(self, device: dict, param: str, value): + def send(self, device: dict, data: dict): # convert hass prop to lumi prop - prop = next(p[0] for p in device['params'] if p[2] == param) + params = [{ + 'res_name': next(p[0] for p in device['params'] if p[2] == k), + 'value': v + } for k, v in data.items()] + payload = { 'cmd': 'write', 'did': device['did'], - 'params': [{'res_name': prop, 'value': value}], + 'params': params, } _LOGGER.debug(f"{self.host} | {device['did']} {device['model']} => " diff --git a/custom_components/xiaomi_gateway3/light.py b/custom_components/xiaomi_gateway3/light.py index baff170c..5e6e5c92 100644 --- a/custom_components/xiaomi_gateway3/light.py +++ b/custom_components/xiaomi_gateway3/light.py @@ -1,7 +1,7 @@ import logging from homeassistant.components.light import LightEntity, SUPPORT_BRIGHTNESS, \ - ATTR_BRIGHTNESS + ATTR_BRIGHTNESS, SUPPORT_COLOR_TEMP, ATTR_COLOR_TEMP from . import DOMAIN, Gateway3Device from .gateway3 import Gateway3 @@ -19,6 +19,7 @@ def setup(gateway: Gateway3, device: dict, attr: str): class Gateway3Light(Gateway3Device, LightEntity): _brightness = None + _color_temp = None @property def is_on(self) -> bool: @@ -29,12 +30,18 @@ def brightness(self): """Return the brightness of this light between 0..255.""" return self._brightness + @property + def color_temp(self): + return self._color_temp + @property def supported_features(self): """Flag supported features.""" features = 0 if self._brightness is not None: features |= SUPPORT_BRIGHTNESS + if self._color_temp is not None: + features |= SUPPORT_COLOR_TEMP return features def update(self, data: dict = None): @@ -42,15 +49,25 @@ def update(self, data: dict = None): self._state = data[self._attr] == 1 if 'brightness' in data: self._brightness = data['brightness'] / 100.0 * 255.0 + if 'color_temp' in data: + self._color_temp = data['color_temp'] self.schedule_update_ha_state() def turn_on(self, **kwargs): + payload = {} + if ATTR_BRIGHTNESS in kwargs: - br = int(kwargs[ATTR_BRIGHTNESS] / 255.0 * 100.0) - self.gw.send(self.device, 'brightness', br) - else: - self.gw.send(self.device, self._attr, 1) + payload['brightness'] = \ + int(kwargs[ATTR_BRIGHTNESS] / 255.0 * 100.0) + + if ATTR_COLOR_TEMP in kwargs: + payload['color_temp'] = kwargs[ATTR_COLOR_TEMP] + + if not payload: + payload[self._attr] = 1 + + self.gw.send(self.device, payload) def turn_off(self): - self.gw.send(self.device, self._attr, 0) + self.gw.send(self.device, {self._attr: 0}) diff --git a/custom_components/xiaomi_gateway3/remote.py b/custom_components/xiaomi_gateway3/remote.py index 0133cda5..a92a91be 100644 --- a/custom_components/xiaomi_gateway3/remote.py +++ b/custom_components/xiaomi_gateway3/remote.py @@ -36,7 +36,7 @@ def update(self, data: dict = None): self.schedule_update_ha_state() def turn_on(self): - self.gw.send(self.device, 'pairing_start', 60) + self.gw.send(self.device, {'pairing_start': 60}) def turn_off(self): - self.gw.send(self.device, 'pairing_stop', 0) + self.gw.send(self.device, {'pairing_stop': 0}) diff --git a/custom_components/xiaomi_gateway3/switch.py b/custom_components/xiaomi_gateway3/switch.py index e24193e6..1e5792be 100644 --- a/custom_components/xiaomi_gateway3/switch.py +++ b/custom_components/xiaomi_gateway3/switch.py @@ -28,7 +28,7 @@ def update(self, data: dict = None): self.schedule_update_ha_state() def turn_on(self): - self.gw.send(self.device, self._attr, 1) + self.gw.send(self.device, {self._attr: 1}) def turn_off(self): - self.gw.send(self.device, self._attr, 0) + self.gw.send(self.device, {self._attr: 0}) diff --git a/custom_components/xiaomi_gateway3/utils.py b/custom_components/xiaomi_gateway3/utils.py index 63eabe7b..8de325cd 100644 --- a/custom_components/xiaomi_gateway3/utils.py +++ b/custom_components/xiaomi_gateway3/utils.py @@ -104,10 +104,17 @@ ['8.0.2001', 'battery', 'battery', 'sensor'], ] }, { - # light with brightness + # light with brightness and color temp 'lumi.light.aqcn02': ["Aqara", "Bulb", "ZNLDP12LM"], 'lumi.light.cwopcn02': ["Aqara", "Opple MX650", "XDD12LM"], 'lumi.light.cwopcn03': ["Aqara", "Opple MX480", "XDD13LM"], + 'params': [ + ['4.1.85', 'power_status', 'light', 'light'], + ['14.1.85', 'light_level', 'brightness', None], + ['14.2.85', 'colour_temperature', 'color_temp', None], + ] +},{ + # light with brightness 'ikea.light.led1649c5': ["IKEA", "Bulb E14"], # tested 'params': [ ['4.1.85', 'power_status', 'light', 'light'],