diff options
Diffstat (limited to 'light.py')
-rw-r--r-- | light.py | 9 |
1 files changed, 9 insertions, 0 deletions
@@ -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 ) ) |