aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--light.py23
1 files changed, 20 insertions, 3 deletions
diff --git a/light.py b/light.py
index 3af15bf..8d39860 100644
--- a/light.py
+++ b/light.py
@@ -3,6 +3,7 @@ import voluptuous as vol
import requests
import threading
from math import floor
+from threading import Thread
from homeassistant.const import CONF_MAC
from homeassistant.components.light import (
LightEntity,
@@ -35,6 +36,9 @@ class ESPLedStripLight(LightEntity):
self._on = False
self._brightness = 255
self._rgb = (255, 255, 255)
+ self._thread = Thread()
+ self._thread.start()
+ self._transitioning = False
@property
def color_mode(self):
@@ -81,25 +85,37 @@ class ESPLedStripLight(LightEntity):
rgb = kwargs.get(ATTR_RGB_COLOR)
if rgb != None: self._rgb = rgb
+ self.terminate_thread()
transition = kwargs.get(ATTR_TRANSITION)
if transition != None:
self.interpolate(brightness_old if on_old else 0, self._brightness, rgb_old if on_old else (0, 0, 0,), self._rgb, transition)
-
- self.update_espled()
+ else:
+ self.update_espled()
def turn_off(self, **kwargs):
+ self.terminate_thread()
self._on = False
self.update_espled()
def interpolate(self, brightness_old, brightness, rgb_old, rgb, transition):
+ self._thread = Thread(target=self.interpolate_thread, args=(brightness_old, brightness, rgb_old, rgb, transition, ))
+ self._thread.start()
+
+ def terminate_thread(self):
+ self._transitioning = False
+ self._thread.join()
+
+ def interpolate_thread(self, brightness_old, brightness, rgb_old, rgb, 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
+ self._transitioning = True
for x in range(steps):
- weight = x / steps
+ if not self._transitioning: break
+ weight = (x + 1) / 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
@@ -107,6 +123,7 @@ class ESPLedStripLight(LightEntity):
self._brightness = brightness_old * (1 - weight) + brightness * weight
self.update_espled()
sleep(step_duration)
+ self._transitioning = False
def update_espled(self):
r = int( int(self._on) * self._rgb[0] * ( self._brightness / 255 ) )