Skip to content
Snippets Groups Projects
Commit 4b9af2f7 authored by jkerdreu's avatar jkerdreu
Browse files

Fix brightness parsing w/ hsv value


git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2957 b32b6428-25c9-4566-ad07-03861ab6144f
parent 9911eb12
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ from decorator import decorator
logger = logging.getLogger(__name__)
def run(func, *args, **kwargs):
self = args[0]
if not self.lock.ready():
......@@ -23,11 +24,13 @@ def run(func,*args,**kwargs):
logger.warning(f"{self.bulb} {e} calling {func}")
self.lock.release()
@decorator
def spawn(func, *args, **kwargs):
ptr = functools.partial(run,func,*args,**kwargs)
gevent.spawn(ptr)
def properties_compare(orig, new):
r = {}
for k in new.keys():
......@@ -36,6 +39,7 @@ def properties_compare(orig,new):
r.update({k:new[k]})
return r
class YeelightDev(object):
def __init__(self,bulb,cfg):
self.bulb = bulb
......@@ -48,7 +52,7 @@ class YeelightDev(object):
# It's safer to use a lock to avoid the socket to be used w/ 2 greenlets at the same time.
# This can occurs on the device refresh
self.lock = gevent.lock.BoundedSemaphore(1)
self.bulb.start_music()
#self.bulb.start_music()
def debug_properties(self,properties):
if not hasattr(self,'last_properties'):
......@@ -109,16 +113,12 @@ class RGBW(YeelightDev):
dev.methods['set_hsv'] = self.set_hsv
dev.methods['set_white_temperature'] = self.set_white_temperature
dev.methods['set_mode'] = self.set_mode
dev.methods['debug'] = self.debug
dev.info = 'RGBW / %s' % self.addr
dev.attributes['hsv'] = [0, 0, 0]
dev.unsupported_attributes = ['scene']
dev.unsupported_methods = ['get_scene', 'set_scene']
self.dev = dev
def debug(self):
import pdb;pdb.set_trace()
@spawn
def set_brightness(self, _brightness, _smooth=None):
val = int(_brightness)
......@@ -131,6 +131,35 @@ class RGBW(YeelightDev):
self.bulb.set_brightness(val)
self._update_properties()
@spawn
def set_hsv_(self, _hsv, _smooth=None):
# FIXME
if isinstance(_hsv, str):
hsv = [float(k) for k in list(_hsv.split(','))]
else:
hsv = _hsv
h, s, v = hsv
v = int(v * 100)
s = int(s * 100)
h = int(h)
if _smooth:
duration = int(_smooth)
else:
duration = int(self.cfg.get('smooth_default', 500))
if duration < 50:
duration = 50
self.bulb.turn_on()
self.bulb.duration = duration
self.bulb.set_hsv(h, s, v)
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)
@spawn
def set_hsv(self, _hsv, _smooth=None):
# FIXME
......@@ -194,12 +223,14 @@ class RGBW(YeelightDev):
ct = props.get('ct', None)
if ct:
attrs['white_temperature'] = int(ct)
# dimmer ?
# color / dimmer ?
bright = props.get('current_brightness', None)
if bright:
rgb = props.get('rgb', None)
if bright and not rgb:
attrs['brightness'] = int(bright)
hsv = list(attrs['hsv'])
hsv[2] = int(bright)/100.0
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
......@@ -216,7 +247,6 @@ class RGBW(YeelightDev):
# hsv[0] = int(hue)
# attrs['hsv']=hsv
rgb = props.get('rgb',None)
if rgb:
rgb = int(rgb)
r = (rgb >> 16) / 255
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment