2020-08-28 15:33:34 +00:00
|
|
|
"""Light for Shelly."""
|
2020-09-07 15:29:51 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-08-28 15:33:34 +00:00
|
|
|
from aioshelly import Block
|
|
|
|
|
2020-09-07 15:29:51 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
LightEntity,
|
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
from homeassistant.core import callback
|
2020-09-07 15:29:51 +00:00
|
|
|
from homeassistant.util.color import (
|
|
|
|
color_temperature_kelvin_to_mired,
|
|
|
|
color_temperature_mired_to_kelvin,
|
|
|
|
)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2020-09-07 12:13:20 +00:00
|
|
|
from . import ShellyDeviceWrapper
|
2020-11-11 19:13:14 +00:00
|
|
|
from .const import COAP, DATA_CONFIG_ENTRY, DOMAIN
|
2020-09-07 12:13:20 +00:00
|
|
|
from .entity import ShellyBlockEntity
|
2020-11-19 10:42:24 +00:00
|
|
|
from .utils import async_remove_shelly_entity
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up lights for device."""
|
2020-11-11 19:13:14 +00:00
|
|
|
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][COAP]
|
2020-11-05 17:38:53 +00:00
|
|
|
|
|
|
|
blocks = []
|
|
|
|
for block in wrapper.device.blocks:
|
|
|
|
if block.type == "light":
|
|
|
|
blocks.append(block)
|
2020-11-12 09:12:56 +00:00
|
|
|
elif block.type == "relay":
|
|
|
|
appliance_type = wrapper.device.settings["relays"][int(block.channel)].get(
|
2020-11-05 17:38:53 +00:00
|
|
|
"appliance_type"
|
|
|
|
)
|
2020-11-12 09:12:56 +00:00
|
|
|
if appliance_type and appliance_type.lower() == "light":
|
|
|
|
blocks.append(block)
|
|
|
|
unique_id = (
|
|
|
|
f'{wrapper.device.shelly["mac"]}-{block.type}_{block.channel}'
|
|
|
|
)
|
2020-11-19 10:42:24 +00:00
|
|
|
await async_remove_shelly_entity(hass, "switch", unique_id)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
if not blocks:
|
|
|
|
return
|
|
|
|
|
|
|
|
async_add_entities(ShellyLight(wrapper, block) for block in blocks)
|
|
|
|
|
|
|
|
|
|
|
|
class ShellyLight(ShellyBlockEntity, LightEntity):
|
|
|
|
"""Switch that controls a relay block on Shelly devices."""
|
|
|
|
|
|
|
|
def __init__(self, wrapper: ShellyDeviceWrapper, block: Block) -> None:
|
|
|
|
"""Initialize light."""
|
|
|
|
super().__init__(wrapper, block)
|
|
|
|
self.control_result = None
|
|
|
|
self._supported_features = 0
|
|
|
|
if hasattr(block, "brightness"):
|
|
|
|
self._supported_features |= SUPPORT_BRIGHTNESS
|
2020-09-07 15:29:51 +00:00
|
|
|
if hasattr(block, "colorTemp"):
|
|
|
|
self._supported_features |= SUPPORT_COLOR_TEMP
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Supported features."""
|
|
|
|
return self._supported_features
|
2020-08-28 15:33:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""If light is on."""
|
|
|
|
if self.control_result:
|
|
|
|
return self.control_result["ison"]
|
|
|
|
|
|
|
|
return self.block.output
|
|
|
|
|
|
|
|
@property
|
2020-09-07 15:29:51 +00:00
|
|
|
def brightness(self) -> Optional[int]:
|
2020-08-28 15:33:34 +00:00
|
|
|
"""Brightness of light."""
|
|
|
|
if self.control_result:
|
|
|
|
brightness = self.control_result["brightness"]
|
|
|
|
else:
|
|
|
|
brightness = self.block.brightness
|
|
|
|
return int(brightness / 100 * 255)
|
|
|
|
|
|
|
|
@property
|
2020-09-07 15:29:51 +00:00
|
|
|
def color_temp(self) -> Optional[float]:
|
|
|
|
"""Return the CT color value in mireds."""
|
|
|
|
if self.control_result:
|
|
|
|
color_temp = self.control_result["temp"]
|
|
|
|
else:
|
|
|
|
color_temp = self.block.colorTemp
|
|
|
|
|
|
|
|
# If you set DUO to max mireds in Shelly app, 2700K,
|
|
|
|
# It reports 0 temp
|
|
|
|
if color_temp == 0:
|
|
|
|
return self.max_mireds
|
|
|
|
|
|
|
|
return int(color_temperature_kelvin_to_mired(color_temp))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_mireds(self) -> float:
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
|
|
|
return color_temperature_kelvin_to_mired(6500)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self) -> float:
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
|
|
|
return color_temperature_kelvin_to_mired(2700)
|
2020-08-28 15:33:34 +00:00
|
|
|
|
2020-09-07 15:29:51 +00:00
|
|
|
async def async_turn_on(self, **kwargs) -> None:
|
2020-08-28 15:33:34 +00:00
|
|
|
"""Turn on light."""
|
|
|
|
params = {"turn": "on"}
|
2020-09-07 15:29:51 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
|
|
|
tmp_brightness = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
params["brightness"] = int(tmp_brightness / 255 * 100)
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
|
|
|
color_temp = color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP])
|
|
|
|
if color_temp > 6500:
|
|
|
|
color_temp = 6500
|
|
|
|
elif color_temp < 2700:
|
|
|
|
color_temp = 2700
|
|
|
|
params["temp"] = int(color_temp)
|
2020-08-28 15:33:34 +00:00
|
|
|
self.control_result = await self.block.set_state(**params)
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2020-09-07 15:29:51 +00:00
|
|
|
async def async_turn_off(self, **kwargs) -> None:
|
2020-08-28 15:33:34 +00:00
|
|
|
"""Turn off light."""
|
|
|
|
self.control_result = await self.block.set_state(turn="off")
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""When device updates, clear control result that overrides state."""
|
|
|
|
self.control_result = None
|
|
|
|
super()._update_callback()
|