2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HomematicIP Cloud lights."""
|
2018-05-08 07:57:51 +00:00
|
|
|
import logging
|
|
|
|
|
2019-04-24 11:27:45 +00:00
|
|
|
from homematicip.aio.device import (
|
2019-07-31 19:25:30 +00:00
|
|
|
AsyncBrandDimmer,
|
|
|
|
AsyncBrandSwitchMeasuring,
|
|
|
|
AsyncBrandSwitchNotificationLight,
|
|
|
|
AsyncDimmer,
|
|
|
|
AsyncFullFlushDimmer,
|
|
|
|
AsyncPluggableDimmer,
|
|
|
|
)
|
2019-04-25 22:13:07 +00:00
|
|
|
from homematicip.aio.home import AsyncHome
|
2019-04-23 23:47:31 +00:00
|
|
|
from homematicip.base.enums import RGBColorState
|
2019-04-25 22:13:07 +00:00
|
|
|
from homematicip.base.functionalChannels import NotificationLightChannel
|
2019-04-23 23:47:31 +00:00
|
|
|
|
2018-08-21 19:25:16 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_NAME,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
Light,
|
|
|
|
)
|
2019-04-25 22:13:07 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
|
|
|
|
2018-05-08 07:57:51 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ENERGY_COUNTER = "energy_counter_kwh"
|
|
|
|
ATTR_POWER_CONSUMPTION = "power_consumption"
|
2018-05-08 07:57:51 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Old way of setting up HomematicIP Cloud lights."""
|
2018-07-06 21:05:34 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities
|
|
|
|
) -> None:
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Set up the HomematicIP Cloud lights from a config entry."""
|
2018-07-06 21:05:34 +00:00
|
|
|
home = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]].home
|
2018-05-08 07:57:51 +00:00
|
|
|
devices = []
|
|
|
|
for device in home.devices:
|
2018-07-09 03:37:59 +00:00
|
|
|
if isinstance(device, AsyncBrandSwitchMeasuring):
|
2018-05-08 07:57:51 +00:00
|
|
|
devices.append(HomematicipLightMeasuring(home, device))
|
2019-02-10 18:49:16 +00:00
|
|
|
elif isinstance(device, AsyncBrandSwitchNotificationLight):
|
|
|
|
devices.append(HomematicipLight(home, device))
|
2019-07-31 19:25:30 +00:00
|
|
|
devices.append(
|
|
|
|
HomematicipNotificationLight(home, device, device.topLightChannelIndex)
|
|
|
|
)
|
|
|
|
devices.append(
|
|
|
|
HomematicipNotificationLight(
|
|
|
|
home, device, device.bottomLightChannelIndex
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif isinstance(
|
|
|
|
device,
|
|
|
|
(AsyncDimmer, AsyncPluggableDimmer, AsyncBrandDimmer, AsyncFullFlushDimmer),
|
|
|
|
):
|
2018-07-13 21:25:11 +00:00
|
|
|
devices.append(HomematicipDimmer(home, device))
|
2018-05-08 07:57:51 +00:00
|
|
|
|
|
|
|
if devices:
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(devices)
|
2018-05-08 07:57:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomematicipLight(HomematicipGenericDevice, Light):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Representation of a HomematicIP Cloud light device."""
|
2018-05-08 07:57:51 +00:00
|
|
|
|
2019-04-25 22:13:07 +00:00
|
|
|
def __init__(self, home: AsyncHome, device) -> None:
|
2018-05-08 07:57:51 +00:00
|
|
|
"""Initialize the light device."""
|
|
|
|
super().__init__(home, device)
|
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-05-08 07:57:51 +00:00
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._device.on
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the device on."""
|
|
|
|
await self._device.turn_on()
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn the device off."""
|
|
|
|
await self._device.turn_off()
|
|
|
|
|
|
|
|
|
|
|
|
class HomematicipLightMeasuring(HomematicipLight):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Representation of a HomematicIP Cloud measuring light device."""
|
2018-05-08 07:57:51 +00:00
|
|
|
|
|
|
|
@property
|
2018-07-09 03:37:59 +00:00
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the generic device."""
|
|
|
|
attr = super().device_state_attributes
|
|
|
|
if self._device.currentPowerConsumption > 0.05:
|
2019-07-31 19:25:30 +00:00
|
|
|
attr[ATTR_POWER_CONSUMPTION] = round(
|
|
|
|
self._device.currentPowerConsumption, 2
|
|
|
|
)
|
2019-03-31 19:18:45 +00:00
|
|
|
attr[ATTR_ENERGY_COUNTER] = round(self._device.energyCounter, 2)
|
2018-07-09 03:37:59 +00:00
|
|
|
return attr
|
2018-07-13 21:25:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomematicipDimmer(HomematicipGenericDevice, Light):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Representation of HomematicIP Cloud dimmer light device."""
|
2018-07-13 21:25:11 +00:00
|
|
|
|
2019-04-25 22:13:07 +00:00
|
|
|
def __init__(self, home: AsyncHome, device) -> None:
|
2018-07-13 21:25:11 +00:00
|
|
|
"""Initialize the dimmer light device."""
|
|
|
|
super().__init__(home, device)
|
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def is_on(self) -> bool:
|
2018-07-13 21:25:11 +00:00
|
|
|
"""Return true if device is on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._device.dimLevel is not None and self._device.dimLevel > 0.0
|
2018-07-13 21:25:11 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def brightness(self) -> int:
|
2018-07-13 21:25:11 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-04-23 23:47:31 +00:00
|
|
|
if self._device.dimLevel:
|
2019-07-31 19:25:30 +00:00
|
|
|
return int(self._device.dimLevel * 255)
|
2019-04-23 23:47:31 +00:00
|
|
|
return 0
|
2018-07-13 21:25:11 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def supported_features(self) -> int:
|
2018-07-13 21:25:11 +00:00
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BRIGHTNESS
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the light on."""
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
await self._device.set_dim_level(kwargs[ATTR_BRIGHTNESS] / 255.0)
|
2018-07-13 21:25:11 +00:00
|
|
|
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)
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HomematicipNotificationLight(HomematicipGenericDevice, Light):
|
|
|
|
"""Representation of HomematicIP Cloud dimmer light device."""
|
|
|
|
|
2019-04-25 22:13:07 +00:00
|
|
|
def __init__(self, home: AsyncHome, device, channel: int) -> None:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""Initialize the dimmer light device."""
|
2019-04-23 23:47:31 +00:00
|
|
|
self.channel = channel
|
|
|
|
if self.channel == 2:
|
2019-07-31 19:25:30 +00:00
|
|
|
super().__init__(home, device, "Top")
|
2019-02-10 18:49:16 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
super().__init__(home, device, "Bottom")
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
self._color_switcher = {
|
|
|
|
RGBColorState.WHITE: [0.0, 0.0],
|
|
|
|
RGBColorState.RED: [0.0, 100.0],
|
|
|
|
RGBColorState.YELLOW: [60.0, 100.0],
|
|
|
|
RGBColorState.GREEN: [120.0, 100.0],
|
|
|
|
RGBColorState.TURQUOISE: [180.0, 100.0],
|
|
|
|
RGBColorState.BLUE: [240.0, 100.0],
|
2019-07-31 19:25:30 +00:00
|
|
|
RGBColorState.PURPLE: [300.0, 100.0],
|
2019-02-10 18:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def _func_channel(self) -> NotificationLightChannel:
|
2019-04-23 23:47:31 +00:00
|
|
|
return self._device.functionalChannels[self.channel]
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def is_on(self) -> bool:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""Return true if device is on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return (
|
|
|
|
self._func_channel.dimLevel is not None
|
|
|
|
and self._func_channel.dimLevel > 0.0
|
|
|
|
)
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def brightness(self) -> int:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-04-23 23:47:31 +00:00
|
|
|
if self._func_channel.dimLevel:
|
|
|
|
return int(self._func_channel.dimLevel * 255)
|
|
|
|
return 0
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def hs_color(self) -> tuple:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""Return the hue and saturation color value [float, float]."""
|
2019-04-23 23:47:31 +00:00
|
|
|
simple_rgb_color = self._func_channel.simpleRGBColorState
|
2019-02-10 18:49:16 +00:00
|
|
|
return self._color_switcher.get(simple_rgb_color, [0.0, 0.0])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes of the generic device."""
|
|
|
|
attr = super().device_state_attributes
|
|
|
|
if self.is_on:
|
2019-04-23 23:47:31 +00:00
|
|
|
attr[ATTR_COLOR_NAME] = self._func_channel.simpleRGBColorState
|
2019-02-10 18:49:16 +00:00
|
|
|
return attr
|
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def name(self) -> str:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""Return the name of the generic device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "{} {}".format(super().name, "Notification")
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
@property
|
2019-04-25 22:13:07 +00:00
|
|
|
def supported_features(self) -> int:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_BRIGHTNESS | SUPPORT_COLOR
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique ID."""
|
2019-09-03 15:27:14 +00:00
|
|
|
return f"{self.__class__.__name__}_{self.post}_{self._device.id}"
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the light on."""
|
|
|
|
# Use hs_color from kwargs,
|
|
|
|
# if not applicable use current hs_color.
|
|
|
|
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
|
|
|
simple_rgb_color = _convert_color(hs_color)
|
|
|
|
|
|
|
|
# Use brightness from kwargs,
|
|
|
|
# if not applicable use current brightness.
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
|
|
|
|
|
|
|
# If no kwargs, use default value.
|
|
|
|
if not kwargs:
|
|
|
|
brightness = 255
|
|
|
|
|
|
|
|
# Minimum brightness is 10, otherwise the led is disabled
|
|
|
|
brightness = max(10, brightness)
|
|
|
|
dim_level = brightness / 255.0
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await self._device.set_rgb_dim_level(self.channel, simple_rgb_color, dim_level)
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn the light off."""
|
2019-04-23 23:47:31 +00:00
|
|
|
simple_rgb_color = self._func_channel.simpleRGBColorState
|
2019-07-31 19:25:30 +00:00
|
|
|
await self._device.set_rgb_dim_level(self.channel, simple_rgb_color, 0.0)
|
2019-02-10 18:49:16 +00:00
|
|
|
|
|
|
|
|
2019-04-23 23:47:31 +00:00
|
|
|
def _convert_color(color) -> RGBColorState:
|
2019-02-10 18:49:16 +00:00
|
|
|
"""
|
|
|
|
Convert the given color to the reduced RGBColorState color.
|
|
|
|
|
|
|
|
RGBColorStat contains only 8 colors including white and black,
|
|
|
|
so a conversion is required.
|
|
|
|
"""
|
|
|
|
if color is None:
|
|
|
|
return RGBColorState.WHITE
|
|
|
|
|
|
|
|
hue = int(color[0])
|
|
|
|
saturation = int(color[1])
|
|
|
|
if saturation < 5:
|
|
|
|
return RGBColorState.WHITE
|
|
|
|
if 30 < hue <= 90:
|
|
|
|
return RGBColorState.YELLOW
|
|
|
|
if 90 < hue <= 160:
|
|
|
|
return RGBColorState.GREEN
|
|
|
|
if 150 < hue <= 210:
|
|
|
|
return RGBColorState.TURQUOISE
|
|
|
|
if 210 < hue <= 270:
|
|
|
|
return RGBColorState.BLUE
|
|
|
|
if 270 < hue <= 330:
|
|
|
|
return RGBColorState.PURPLE
|
|
|
|
return RGBColorState.RED
|