core/homeassistant/components/light/xiaomi_aqara.py

105 lines
3.3 KiB
Python
Raw Normal View History

"""Support for Xiaomi Gateway Light."""
import logging
import struct
import binascii
from homeassistant.components.xiaomi_aqara import (PY_XIAOMI_GATEWAY,
XiaomiDevice)
from homeassistant.components.light import (ATTR_BRIGHTNESS, ATTR_RGB_COLOR,
SUPPORT_BRIGHTNESS,
SUPPORT_RGB_COLOR, Light)
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Perform the setup for Xiaomi devices."""
devices = []
for (_, gateway) in hass.data[PY_XIAOMI_GATEWAY].gateways.items():
for device in gateway.devices['light']:
model = device['model']
if model == 'gateway':
devices.append(XiaomiGatewayLight(device, 'Gateway Light',
gateway))
add_devices(devices)
class XiaomiGatewayLight(XiaomiDevice, Light):
"""Representation of a XiaomiGatewayLight."""
def __init__(self, device, name, xiaomi_hub):
"""Initialize the XiaomiGatewayLight."""
self._data_key = 'rgb'
self._rgb = (255, 255, 255)
self._brightness = 180
XiaomiDevice.__init__(self, device, name, xiaomi_hub)
@property
def is_on(self):
"""Return true if it is on."""
return self._state
Checking Xiaomi Aqara devices unavailability states (#11631) * added unavailability tracker, updated sensor component * change hass argument position according to position in binary_sensor * added hass argument to binary_sensor, updated is_on(), it can be UNAVAILABLE now * updated switch component to support unavailability feature * updated light component to support unavailability feature * updated cover component to support unavailability feature * set _hass property * added unavailability tracker, updated sensor component * change hass argument position according to position in binary_sensor * added hass argument to binary_sensor, updated is_on(), it can be UNAVAILABLE now * updated switch component to support unavailability feature * updated light component to support unavailability feature * updated cover component to support unavailability feature * set _hass property * fixed error with wrong arguments number during callback call * reset unavailability state on new message received from device * use locks to fix race condition during managing _state property * overriden state() method for some components to check for STATE_UNAVAILABLE and return it instead e.g. STATE_OFF * fixed linter * removed blank line * use available() method instead of changing _state * filter motion sensors 'heartbeat', was removed from PyXiaomiGateway * remove self._hass, use self.hass set by HA on attach * self.push_data now running in the event loop, use async_schedule_update_ha_state() * merge fix * removed accidentally added home-assistant-polymer * bump PyXiaomiGateway version to 0.8.0 * bump PyXiaomiGateway to 0.8.0 * updated methods names and annotations
2018-01-23 09:22:43 +00:00
def parse_data(self, data, raw_data):
"""Parse data sent by gateway."""
value = data.get(self._data_key)
if value is None:
return False
if value == 0:
if self._state:
self._state = False
return True
rgbhexstr = "%x" % value
if len(rgbhexstr) > 8:
_LOGGER.error("Light RGB data error."
" Can't be more than 8 characters. Received: %s",
rgbhexstr)
return False
rgbhexstr = rgbhexstr.zfill(8)
rgbhex = bytes.fromhex(rgbhexstr)
rgba = struct.unpack('BBBB', rgbhex)
brightness = rgba[0]
rgb = rgba[1:]
self._brightness = int(255 * brightness / 100)
self._rgb = rgb
self._state = True
return True
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def rgb_color(self):
"""Return the RBG color value."""
return self._rgb
@property
def supported_features(self):
"""Return the supported features."""
return SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR
def turn_on(self, **kwargs):
"""Turn the light on."""
if ATTR_RGB_COLOR in kwargs:
self._rgb = kwargs[ATTR_RGB_COLOR]
if ATTR_BRIGHTNESS in kwargs:
self._brightness = int(100 * kwargs[ATTR_BRIGHTNESS] / 255)
rgba = (self._brightness,) + self._rgb
rgbhex = binascii.hexlify(struct.pack('BBBB', *rgba)).decode("ASCII")
rgbhex = int(rgbhex, 16)
if self._write_to_hub(self._sid, **{self._data_key: rgbhex}):
self._state = True
def turn_off(self, **kwargs):
"""Turn the light off."""
if self._write_to_hub(self._sid, **{self._data_key: 0}):
self._state = False