Support color temperature (#39743)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Tomasz 2020-09-07 17:29:51 +02:00 committed by GitHub
parent 90d574e521
commit ae5d8f4d64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 12 deletions

View file

@ -145,7 +145,7 @@ class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity):
name_parts = [self.wrapper.name] name_parts = [self.wrapper.name]
if same_type_count > 1: if same_type_count > 1:
name_parts.append(str(block.index)) name_parts.append(str(block.channel))
name_parts.append(self.description.name) name_parts.append(self.description.name)
self._name = " ".join(name_parts) self._name = " ".join(name_parts)

View file

@ -1,8 +1,20 @@
"""Light for Shelly.""" """Light for Shelly."""
from typing import Optional
from aioshelly import Block from aioshelly import Block
from homeassistant.components.light import SUPPORT_BRIGHTNESS, LightEntity from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR_TEMP,
LightEntity,
)
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.util.color import (
color_temperature_kelvin_to_mired,
color_temperature_mired_to_kelvin,
)
from . import ShellyDeviceWrapper from . import ShellyDeviceWrapper
from .const import DOMAIN from .const import DOMAIN
@ -30,6 +42,13 @@ class ShellyLight(ShellyBlockEntity, LightEntity):
self._supported_features = 0 self._supported_features = 0
if hasattr(block, "brightness"): if hasattr(block, "brightness"):
self._supported_features |= SUPPORT_BRIGHTNESS self._supported_features |= SUPPORT_BRIGHTNESS
if hasattr(block, "colorTemp"):
self._supported_features |= SUPPORT_COLOR_TEMP
@property
def supported_features(self) -> int:
"""Supported features."""
return self._supported_features
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
@ -40,7 +59,7 @@ class ShellyLight(ShellyBlockEntity, LightEntity):
return self.block.output return self.block.output
@property @property
def brightness(self): def brightness(self) -> Optional[int]:
"""Brightness of light.""" """Brightness of light."""
if self.control_result: if self.control_result:
brightness = self.control_result["brightness"] brightness = self.control_result["brightness"]
@ -49,21 +68,47 @@ class ShellyLight(ShellyBlockEntity, LightEntity):
return int(brightness / 100 * 255) return int(brightness / 100 * 255)
@property @property
def supported_features(self): def color_temp(self) -> Optional[float]:
"""Supported features.""" """Return the CT color value in mireds."""
return self._supported_features if self.control_result:
color_temp = self.control_result["temp"]
else:
color_temp = self.block.colorTemp
async def async_turn_on( # If you set DUO to max mireds in Shelly app, 2700K,
self, brightness=None, **kwargs # It reports 0 temp
): # pylint: disable=arguments-differ 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)
async def async_turn_on(self, **kwargs) -> None:
"""Turn on light.""" """Turn on light."""
params = {"turn": "on"} params = {"turn": "on"}
if brightness is not None: if ATTR_BRIGHTNESS in kwargs:
params["brightness"] = int(brightness / 255 * 100) 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)
self.control_result = await self.block.set_state(**params) self.control_result = await self.block.set_state(**params)
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs) -> None:
"""Turn off light.""" """Turn off light."""
self.control_result = await self.block.set_state(turn="off") self.control_result = await self.block.set_state(turn="off")
self.async_write_ha_state() self.async_write_ha_state()