Skip to content

Commit

Permalink
Add transition support for lights
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Oct 7, 2023
1 parent f30a6bc commit 485b11c
Showing 1 changed file with 53 additions and 9 deletions.
62 changes: 53 additions & 9 deletions custom_components/sonoff/light.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import time

from homeassistant.components.light import (
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_ONOFF,
COLOR_MODE_RGB,
SUPPORT_EFFECT,
SUPPORT_TRANSITION,
LightEntity,
)
from homeassistant.util import color
Expand Down Expand Up @@ -45,6 +48,7 @@ class XLight(XEntity, LightEntity):
# support on/off and brightness
_attr_color_mode = COLOR_MODE_BRIGHTNESS
_attr_supported_color_modes = {COLOR_MODE_BRIGHTNESS}
_attr_supported_features = SUPPORT_TRANSITION

def set_state(self, params: dict):
if self.param in params:
Expand All @@ -61,17 +65,22 @@ async def async_turn_on(
xy_color=None,
hs_color=None,
effect: str = None,
**kwargs
transition: float = None,
**kwargs,
) -> None:
if brightness == 0:
await self.async_turn_off()
return

if xy_color:
rgb_color = color.color_xy_to_RGB(*xy_color)
elif hs_color:
rgb_color = color.color_hs_to_RGB(*hs_color)

if transition:
await self.transiton(brightness, color_temp, rgb_color, transition)
return

if brightness == 0:
await self.async_turn_off()
return

if brightness or color_temp or rgb_color or effect:
params = self.get_params(brightness, color_temp, rgb_color, effect)
else:
Expand All @@ -83,15 +92,50 @@ async def async_turn_on(
await self.ewelink.send(
self.device, {self.param: "on"}, query_cloud=False
)

await self.ewelink.send(
self.device, params, {"cmd": "dimmable", **params}, cmd_lan="dimmable"
self.device,
params,
{"cmd": "dimmable", **params},
cmd_lan="dimmable",
query_cloud=kwargs.get("query_cloud", True),
)
else:
await self.ewelink.send(self.device, {self.param: "on"})

async def async_turn_off(self, **kwargs) -> None:
await self.ewelink.send(self.device, {self.param: "off"})

async def transiton(
self,
brightness: int,
color_temp: int,
rgb_color,
transition: float,
):
br0 = self.brightness or 0
br1 = brightness
ct0 = self.color_temp or self.min_mireds
ct1 = color_temp
rgb0 = self.rgb_color or [0, 0, 0]
rgb1 = rgb_color

t0 = time.time()

while (k := (time.time() - t0) / transition) < 1:
if br1 is not None:
brightness = br0 + round((br1 - br0) * k)
if ct1 is not None:
color_temp = ct0 + round((ct1 - ct0) * k)
if rgb1 is not None:
rgb_color = [rgb0[i] + round((rgb1[i] - rgb0[i]) * k) for i in range(3)]

await self.async_turn_on(
brightness, color_temp, rgb_color, query_cloud=False
)

await self.async_turn_on(br1, ct1, rgb1)


# noinspection PyAbstractClass, UIID36
class XDimmer(XLight):
Expand Down Expand Up @@ -199,7 +243,7 @@ class XLightB1(XLight):
_attr_effect_list = list(UIID22_MODES.keys())
# support on/off, brightness, color_temp and RGB
_attr_supported_color_modes = {COLOR_MODE_COLOR_TEMP, COLOR_MODE_RGB}
_attr_supported_features = SUPPORT_EFFECT
_attr_supported_features = SUPPORT_EFFECT | SUPPORT_TRANSITION

def set_state(self, params: dict):
XLight.set_state(self, params)
Expand Down Expand Up @@ -295,7 +339,7 @@ class XLightL1(XLight):

# support on/off, brightness, RGB
_attr_supported_color_modes = {COLOR_MODE_RGB}
_attr_supported_features = SUPPORT_EFFECT
_attr_supported_features = SUPPORT_EFFECT | SUPPORT_TRANSITION

def set_state(self, params: dict):
XLight.set_state(self, params)
Expand Down Expand Up @@ -729,7 +773,7 @@ class XLightB02(XLight):
_attr_effect_list = list(B02_MODE_PAYLOADS.keys())
# support on/off, brightness and color_temp
_attr_supported_color_modes = {COLOR_MODE_COLOR_TEMP}
_attr_supported_features = SUPPORT_EFFECT
_attr_supported_features = SUPPORT_EFFECT | SUPPORT_TRANSITION

# ewelink specs
min_br = 1
Expand Down

0 comments on commit 485b11c

Please sign in to comment.