Add HomematicIP Cloud dimmer light device (#15456)

* Add dimmable light device

* Add imports

* Fix float and int conversion
This commit is contained in:
Mattias Welponer 2018-07-13 23:25:11 +02:00 committed by Martin Hjelmare
parent 538236de8f
commit ce5b4cd51e

View File

@ -7,7 +7,8 @@ https://home-assistant.io/components/light.homematicip_cloud/
import logging import logging
from homeassistant.components.light import Light from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS)
from homeassistant.components.homematicip_cloud import ( from homeassistant.components.homematicip_cloud import (
HomematicipGenericDevice, DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice, DOMAIN as HMIPC_DOMAIN,
HMIPC_HAPID) HMIPC_HAPID)
@ -30,13 +31,15 @@ async def async_setup_platform(hass, config, async_add_devices,
async def async_setup_entry(hass, config_entry, async_add_devices): async def async_setup_entry(hass, config_entry, async_add_devices):
"""Set up the HomematicIP lights from a config entry.""" """Set up the HomematicIP lights from a config entry."""
from homematicip.aio.device import ( from homematicip.aio.device import (
AsyncBrandSwitchMeasuring) AsyncBrandSwitchMeasuring, AsyncPluggableDimmer)
home = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]].home home = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]].home
devices = [] devices = []
for device in home.devices: for device in home.devices:
if isinstance(device, AsyncBrandSwitchMeasuring): if isinstance(device, AsyncBrandSwitchMeasuring):
devices.append(HomematicipLightMeasuring(home, device)) devices.append(HomematicipLightMeasuring(home, device))
elif isinstance(device, AsyncPluggableDimmer):
devices.append(HomematicipDimmer(home, device))
if devices: if devices:
async_add_devices(devices) async_add_devices(devices)
@ -79,3 +82,38 @@ class HomematicipLightMeasuring(HomematicipLight):
ATTR_ENERGIE_COUNTER: round(self._device.energyCounter, 2) ATTR_ENERGIE_COUNTER: round(self._device.energyCounter, 2)
}) })
return attr return attr
class HomematicipDimmer(HomematicipGenericDevice, Light):
"""MomematicIP dimmer light device."""
def __init__(self, home, device):
"""Initialize the dimmer light device."""
super().__init__(home, device)
@property
def is_on(self):
"""Return true if device is on."""
return self._device.dimLevel != 0
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return int(self._device.dimLevel*255)
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
async def async_turn_on(self, **kwargs):
"""Turn the light on."""
if ATTR_BRIGHTNESS in kwargs:
await self._device.set_dim_level(
kwargs[ATTR_BRIGHTNESS]/255.0)
else:
await self._device.set_dim_level(1)
async def async_turn_off(self, **kwargs):
"""Turn the light off."""
await self._device.set_dim_level(0)