From 5f9d6e4ce82339e168112b3eac6f89f53d8c417e Mon Sep 17 00:00:00 2001
From: jkerdreu <jkerdreu@b32b6428-25c9-4566-ad07-03861ab6144f>
Date: Mon, 30 May 2022 10:55:26 +0000
Subject: [PATCH] Fix brightness on hsv once again. This produce swings in
 home-assistant UI.

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2958 b32b6428-25c9-4566-ad07-03861ab6144f
---
 .../Yeelight/xaal/yeelight/devices.py         | 24 ++++++++++---------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/devices/protocols/Yeelight/xaal/yeelight/devices.py b/devices/protocols/Yeelight/xaal/yeelight/devices.py
index 8997d2ee..39a09332 100644
--- a/devices/protocols/Yeelight/xaal/yeelight/devices.py
+++ b/devices/protocols/Yeelight/xaal/yeelight/devices.py
@@ -178,17 +178,17 @@ class RGBW(YeelightDev):
         self.bulb.turn_on()
         self.bulb.duration = duration
 
-        rgb=tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h/360.0,s,v))
-        self.bulb.set_rgb(rgb[0],rgb[1],rgb[2])
+        rgb = tuple(round(i * 255) for i in colorsys.hsv_to_rgb(h / 360.0, s, v))
+        self.bulb.set_rgb(rgb[0], rgb[1], rgb[2])
 
         gevent.sleep(0.2)
         self._update_properties()
         target = round((duration / 1000.0), 1)
         # we schedule some get_properties at the end of the flow to update the current color
         # As time drift, don't expect to have it working for a 1 hour flow.
-        self.dev.engine.add_timer(self.get_properties, target, 1)
-        self.dev.engine.add_timer(self.get_properties, target+0.5, 1)
-
+        if duration > 1000:
+            self.dev.engine.add_timer(self.get_properties, target, 1)
+            self.dev.engine.add_timer(self.get_properties, target+0.5, 1)
 
     @spawn
     def set_white_temperature(self, _white_temperature):
@@ -227,11 +227,11 @@ class RGBW(YeelightDev):
         bright = props.get('current_brightness', None)
         rgb = props.get('rgb', None)
 
-        if bright and not rgb:
+        if bright:
             attrs['brightness'] = int(bright)
-            hsv = list(attrs['hsv'])
-            hsv[2] = round(int(bright)/100.0, 2)
-            attrs['hsv'] = hsv
+            #hsv = list(attrs['hsv'])
+            #hsv[2] = round(int(bright)/100.0, 2)
+            #attrs['hsv'] = hsv
         # Yeelight Python API provide both rgb and hsv values
         # we parse both, even if we don't' issue set_hsv
         # sat ? 
@@ -247,6 +247,7 @@ class RGBW(YeelightDev):
         #     hsv[0] = int(hue)
         #     attrs['hsv']=hsv
 
+
         if rgb:
             rgb = int(rgb)
             r = (rgb >> 16) / 255
@@ -255,6 +256,7 @@ class RGBW(YeelightDev):
 
             hsv = colorsys.rgb_to_hsv(r, g, b)
             h = round(hsv[0] * 360)
-            s = round(hsv[1], 2)
-            v = round(hsv[2], 2)
+            s = hsv[1]
+            v = hsv[2]
             attrs['hsv'] = [h, s, v]
+            #attrs['brightness'] = int(hsv[2] * 100)
-- 
GitLab