From f578e77a380a426073cc64ae82374b3a17eac2d1 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 13 Aug 2022 16:36:13 +0200 Subject: shitty transition implemented --- light.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'light.py') diff --git a/light.py b/light.py index e719d85..2dc96bf 100644 --- a/light.py +++ b/light.py @@ -9,13 +9,16 @@ from homeassistant.components.light import ( SUPPORT_BRIGHTNESS, SUPPORT_COLOR, SUPPORT_WHITE_VALUE, + SUPPORT_TRANSITION, ATTR_BRIGHTNESS, ATTR_RGBW_COLOR, + ATTR_TRANSITION, COLOR_MODE_RGBW, PLATFORM_SCHEMA ) +from time import sleep PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required("name"): cv.string, @@ -49,7 +52,7 @@ class BekenLight(LightEntity): @property def supported_features(self): - return SUPPORT_BRIGHTNESS | SUPPORT_COLOR | SUPPORT_WHITE_VALUE + return SUPPORT_BRIGHTNESS | SUPPORT_COLOR | SUPPORT_WHITE_VALUE | SUPPORT_TRANSITION @property def unique_id(self): @@ -72,23 +75,51 @@ class BekenLight(LightEntity): return self._rgb + (self._w,) def turn_on(self, **kwargs): + on_old = self._on + w_old = self._w + brightness_old = self._brightness + rgb_old = self._rgb + self._on = True brightness = kwargs.get(ATTR_BRIGHTNESS) - if brightness != None: - self._brightness = brightness + if brightness != None: self._brightness = brightness rgbw = kwargs.get(ATTR_RGBW_COLOR) if rgbw != None: self._rgb = rgbw[0:3] self._w = rgbw[3] + transition = kwargs.get(ATTR_TRANSITION) + if transition != None: + self.interpolate(brightness_old if on_old else 0, brightness, rgb_old if on_old else (0, 0, 0,), rgbw[0:3], w_old if on_old else 0, rgbw[3], transition) + self.update_beken_lamp() def turn_off(self, **kwargs): self._on = False self.update_beken_lamp() + def interpolate(self, brightness_old, brightness, rgb_old, rgb, w_old, w, transition): + step_duration = 0.250 + steps = int(transition / step_duration) + if rgb_old == None: rgb_old = (0, 0, 0,) + if rgb == None: rgb = (0, 0, 0,) + if brightness_old == None: brightness_old = 0 + if brightness == None: brightness = 0 + if w_old == None: w_old = 0 + if w == None: w = 0 + for x in range(steps): + weight = x / steps + r = rgb_old[0] * (1 - weight) + rgb[0] * weight + g = rgb_old[1] * (1 - weight) + rgb[1] * weight + b = rgb_old[2] * (1 - weight) + rgb[2] * weight + self._rgb = (r, g, b,) + self._w = w_old * (1 - weight) + w * weight + self._brightness = brightness_old * (1 - weight) + brightness * weight + self.update_beken_lamp() + sleep(step_duration) + def update_beken_lamp(self): r = int( int(self._on) * self._rgb[0] * ( self._brightness / 255 ) ) g = int( int(self._on) * self._rgb[1] * ( self._brightness / 255 ) ) -- cgit v1.2.3