Skip to content

Commit

Permalink
Add color temp for Aqara Bulb
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Sep 11, 2020
1 parent 79c3294 commit fa3c899
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
10 changes: 7 additions & 3 deletions custom_components/xiaomi_gateway3/gateway3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']} => "
Expand Down
29 changes: 23 additions & 6 deletions custom_components/xiaomi_gateway3/light.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -29,28 +30,44 @@ 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):
if self._attr in data:
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})
4 changes: 2 additions & 2 deletions custom_components/xiaomi_gateway3/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
4 changes: 2 additions & 2 deletions custom_components/xiaomi_gateway3/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
9 changes: 8 additions & 1 deletion custom_components/xiaomi_gateway3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit fa3c899

Please sign in to comment.