2016-06-24 08:06:58 +00:00
|
|
|
"""
|
2016-06-30 08:33:34 +00:00
|
|
|
Support for Homematic lighs.
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/light.homematic/
|
|
|
|
"""
|
|
|
|
import logging
|
2017-02-20 16:07:33 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
|
|
|
|
from homeassistant.components.homematic import HMDevice, ATTR_DISCOVER_DEVICES
|
2016-06-24 08:06:58 +00:00
|
|
|
from homeassistant.const import STATE_UNKNOWN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = ['homematic']
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_HOMEMATIC = SUPPORT_BRIGHTNESS
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2016-09-12 13:52:22 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up the Homematic light platform."""
|
2016-06-28 20:53:53 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2017-02-20 16:07:33 +00:00
|
|
|
devices = []
|
2017-07-06 03:02:16 +00:00
|
|
|
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
|
|
|
|
new_device = HMLight(hass, conf)
|
2017-02-20 16:07:33 +00:00
|
|
|
new_device.link_homematic()
|
|
|
|
devices.append(new_device)
|
|
|
|
|
|
|
|
add_devices(devices)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
|
2016-11-29 19:53:02 +00:00
|
|
|
class HMLight(HMDevice, Light):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Representation of a Homematic light."""
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
|
|
|
# Is dimmer?
|
2017-07-06 03:02:16 +00:00
|
|
|
if self._state == "LEVEL":
|
2016-06-24 08:06:58 +00:00
|
|
|
return int(self._hm_get_state() * 255)
|
2017-07-06 03:02:16 +00:00
|
|
|
return None
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Return true if light is on."""
|
2016-06-24 08:06:58 +00:00
|
|
|
try:
|
|
|
|
return self._hm_get_state() > 0
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_HOMEMATIC
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the light on."""
|
2017-07-06 03:02:16 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs and self._state == "LEVEL":
|
2016-06-24 08:06:58 +00:00
|
|
|
percent_bright = float(kwargs[ATTR_BRIGHTNESS]) / 255
|
|
|
|
self._hmdevice.set_level(percent_bright, self._channel)
|
|
|
|
else:
|
|
|
|
self._hmdevice.on(self._channel)
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
2017-02-14 22:19:57 +00:00
|
|
|
self._hmdevice.off(self._channel)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
def _init_data_struct(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Generate a data dict (self._data) from the Homematic metadata."""
|
2016-06-24 08:06:58 +00:00
|
|
|
# Use LEVEL
|
2016-09-09 17:33:12 +00:00
|
|
|
self._state = "LEVEL"
|
|
|
|
self._data.update({self._state: STATE_UNKNOWN})
|