Created
April 3, 2018 03:12
-
-
Save shred86/834ac5bf0870efe69ce37b70a30098b4 to your computer and use it in GitHub Desktop.
Updated Abode light component for Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This component provides HA light support for Abode Security System. | |
For more details about this platform, please refer to the documentation at | |
https://home-assistant.io/components/light.abode/ | |
""" | |
import logging | |
from homeassistant.components.abode import AbodeDevice, DOMAIN as ABODE_DOMAIN | |
from homeassistant.components.light import ( | |
ATTR_BRIGHTNESS, ATTR_HS_COLOR, | |
SUPPORT_BRIGHTNESS, SUPPORT_COLOR, Light) | |
import homeassistant.util.color as color_util | |
DEPENDENCIES = ['abode'] | |
_LOGGER = logging.getLogger(__name__) | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
"""Set up Abode light devices.""" | |
import abodepy.helpers.constants as CONST | |
data = hass.data[ABODE_DOMAIN] | |
device_types = [CONST.TYPE_LIGHT, CONST.TYPE_SWITCH] | |
devices = [] | |
# Get all regular lights that are not excluded or switches marked as lights | |
for device in data.abode.get_devices(generic_type=device_types): | |
if data.is_excluded(device) or not data.is_light(device): | |
continue | |
devices.append(AbodeLight(data, device)) | |
data.devices.extend(devices) | |
add_devices(devices) | |
def to_abode_level(level): | |
"""Convert HASS light level (0-255) to Abode light level (0-100)""" | |
return int((level * 100) / 255) | |
def to_hass_level(level): | |
"""Convert Abode light level (0-100) to HASS light level (0-255)""" | |
return (int(level) * 255) / 100 | |
class AbodeLight(AbodeDevice, Light): | |
"""Representation of an Abode light.""" | |
def __init__(self, data, device): | |
super().__init__(data, device) | |
self._is_dimmer = self.is_dimmer | |
def turn_on(self, **kwargs): | |
"""Turn on the light.""" | |
if (ATTR_HS_COLOR in kwargs and | |
self._device.is_dimmable and self._device.has_color): | |
self._device.set_color(color_util.color_hs_to_RGB( | |
*kwargs[ATTR_HS_COLOR])) | |
# if ATTR_BRIGHTNESS in kwargs and self._device.is_dimmable: | |
if ATTR_BRIGHTNESS in kwargs and self._is_dimmer: | |
self._device.set_level(to_abode_level(kwargs[ATTR_BRIGHTNESS])) | |
else: | |
self._device.switch_on() | |
def turn_off(self, **kwargs): | |
"""Turn off the light.""" | |
self._device.switch_off() | |
@property | |
def is_dimmer(self): | |
"""Determines if device is a dimmer (instead of using is_dimmable)""" | |
if 'Dimmer' in self._device.get_value('type'): | |
return True | |
@property | |
def is_on(self): | |
"""Return true if device is on.""" | |
return self._device.is_on | |
@property | |
def brightness(self): | |
"""Return the brightness of the light.""" | |
if self._is_dimmer: | |
return to_hass_level(self._device.brightness) | |
@property | |
def hs_color(self): | |
"""Return the color of the light.""" | |
if self._device.is_dimmable and self._device.has_color: | |
return color_util.color_RGB_to_hs(*self._device.color) | |
@property | |
def supported_features(self): | |
"""Flag supported features.""" | |
if self._device.is_dimmable and self._device.has_color: | |
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR | |
elif self._is_dimmer: | |
return SUPPORT_BRIGHTNESS | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment