Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get current RGBA value and set it #1

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
A bit better implementation
Now it sets RGBA value only if you turn ON the light,
but when you turn it off - it leaves old value in memory. So toggling works better.

Also some additions - added togling the light, because sometimes on fresh boot
the light just wouldnt turn on otherwise, as HA was sending turn on command with 0 brightness...
  • Loading branch information
quarcko committed Jul 18, 2019
commit 5b4cf8c6b88c2540778d35e64a0096874db8cf65
27 changes: 15 additions & 12 deletions light.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ def turn_on(self, **kwargs):
argb = (self._brightness,) + rgb
argbhex = binascii.hexlify(struct.pack("BBBB", *argb)).decode("ASCII")
argbhex = int(argbhex, 16)
self._send_to_hub({ "method": "set_rgb", "params": [argbhex] })

if argbhex <= 16777215:
self._send_to_hub({ "method": "toggle_light", "params": ["on"] })
else:
self._send_to_hub({ "method": "set_rgb", "params": [argbhex] })
self._state = True
self.schedule_update_ha_state()

Expand All @@ -64,8 +68,10 @@ def parse_incoming_data(self, params, event, model, sid):
if params is None:
return False

light = params.get("light")
rgba_raw = params.get("rgb")
if rgba_raw is not None:

if None not in (rgba_raw, light):
rgbhexstr = "%x" % rgba_raw
if len(rgbhexstr) <= 8:
rgbhexstr = rgbhexstr.zfill(8)
Expand All @@ -74,15 +80,12 @@ def parse_incoming_data(self, params, event, model, sid):
brightness = rgba[0]
rgb = rgba[1:]

self._brightness = brightness
self._hs = color_util.color_RGB_to_hs(*rgb)

light = params.get("light")
if light is not None:
if light == 'on':
self._state = True
elif light == 'off':
self._state = False
return True
if light == 'on' or rgba_raw > 0:
self._state = True
self._brightness = brightness
self._hs = color_util.color_RGB_to_hs(*rgb)
else:
self._state = False
return True

return False