core/homeassistant/components/light/homematic.py

83 lines
2.3 KiB
Python
Raw Normal View History

2016-06-24 08:06:58 +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
from homeassistant.components.light import (ATTR_BRIGHTNESS,
SUPPORT_BRIGHTNESS, Light)
from homeassistant.components.homematic import HMDevice
2016-06-24 08:06:58 +00:00
from homeassistant.const import STATE_UNKNOWN
from homeassistant.loader import get_component
2016-06-24 08:06:58 +00:00
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['homematic']
SUPPORT_HOMEMATIC = SUPPORT_BRIGHTNESS
2016-06-24 08:06:58 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Homematic light platform."""
2016-06-28 20:53:53 +00:00
if discovery_info is None:
return
homematic = get_component("homematic")
return homematic.setup_hmdevice_discovery_helper(
hass,
HMLight,
discovery_info,
add_devices
)
2016-06-24 08:06:58 +00:00
class HMLight(HMDevice, Light):
"""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."""
if not self.available:
return None
# Is dimmer?
if self._state is "LEVEL":
return int(self._hm_get_state() * 255)
else:
return None
@property
def is_on(self):
"""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
@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."""
if not self.available:
return
if ATTR_BRIGHTNESS in kwargs and self._state is "LEVEL":
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."""
if self.available:
self._hmdevice.off(self._channel)
def _init_data_struct(self):
"""Generate a data dict (self._data) from the Homematic metadata."""
2016-06-24 08:06:58 +00:00
# Use LEVEL
self._state = "LEVEL"
self._data.update({self._state: STATE_UNKNOWN})