From 583358de43f241a0f91dbc4ad0122324880080b9 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 23 Aug 2022 21:01:08 +0200 Subject: untested threading approach to cancellable transitions --- light.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/light.py b/light.py index e195ad7..330a13d 100644 --- a/light.py +++ b/light.py @@ -1,6 +1,7 @@ import homeassistant.helpers.config_validation as cv import voluptuous as vol from math import floor +from threading import Thread from homeassistant.const import CONF_MAC from .driver import BekenConnection, makemsg, BEKEN_CHARACTERISTIC_LAMP from homeassistant.components.light import ( @@ -41,6 +42,7 @@ class BekenLight(LightEntity): self._w = 255 self._connection = BekenConnection(self._address) self._connection.start_threads() + self._transitioning = False @property def color_mode(self): @@ -101,6 +103,11 @@ class BekenLight(LightEntity): self.update_beken_lamp() def interpolate(self, brightness_old, brightness, rgb_old, rgb, w_old, w, transition): + self._transitioning = True + thread = Thread(target=self.interpolate_thread, args=(brightness_old, brightness, rgb_old, rgb, w_old, w, transition, )) + thread.start() + + def interpolate_thread(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,) @@ -110,6 +117,7 @@ class BekenLight(LightEntity): if w_old == None: w_old = 0 if w == None: w = 0 for x in range(steps): + if not self._transitioning: break weight = x / steps r = rgb_old[0] * (1 - weight) + rgb[0] * weight g = rgb_old[1] * (1 - weight) + rgb[1] * weight @@ -119,6 +127,7 @@ class BekenLight(LightEntity): self._brightness = brightness_old * (1 - weight) + brightness * weight self.update_beken_lamp() sleep(step_duration) + self._transitioning = False def update_beken_lamp(self): r = int( int(self._on) * self._rgb[0] * ( self._brightness / 255 ) ) -- cgit v1.2.3