I took the liberty to fork’s @Sbidy’s generous effort and fix the issues.: https://github.com/sbidy/wiz_light/pull/9
https://github.com/sbidy/pywizlight/pull/2
Till the above PRs get merged, my component is also available for direct use at: https://github.com/angadsingh/wiz_light (this component does not need a dependency on pywizlight
like @sbidy’s component)
Bug fixes:
Features:
- Now supports switching the light to rhythm mode! (rhythm is defined as a scene for HA)
- Implements a pattern of sending multiple command UDP datagrams until response is received
- Consolidates getPilot and setPilot calls using a PilotBuilder and PilotParser. Removes unnecessary UDP calls for each and every attribute (color, temperature, brightness, scene, etc.) and makes a combined getPilot/setPilot call
- enhanced debug logging for UDP
The problem with rhythm mode was that in @Sbidy’s component, he was making several different UDP calls in the turn_on
method for setting brightness, color temperature, scene, etc, each of which were sending their own UDP setPilot
s, and then a final setPilot
which had no params. Turns out, that if there is a rhythm defined in the room, it gets activated on the light by sending a setPilot with no params . I am using that to allow you to switch any light to a ‘Rhythm’ scene now (and it detects if its in a rhythm scene as well). It all works flawlessly now.
For async I/O I am using https://github.com/jsbronder/asyncio-dgram, which internally uses asyncio DatagramTransport, which allows completely non-blocking UDP transport:
https://github.com/angadsingh/wiz_light/blob/caa90cebd9f8ccb2d588c900e36fbf19277eda9c/wizlight.py#L293
Since UDP can loose packets, and your light might be a long distance away from the router, I am continuously sending the UDP command until there is a response from the light. This has in my tests worked way better than just sending once and just waiting for a timeout.
This is the result:
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.light] [wizlight 192.168.1.88] updating state
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.88, connid 384b0e6912316c] connecting to UDP port
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.7, connid 384b0e6911bdc4] listening for response datagram
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.7, connid 384b0e6911bdc4] sending command datagram 0: {"method":"getPilot","params":{}}
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.7, connid 384b0e6911bdc4] sending command datagram 1: {"method":"getPilot","params":{}}
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.7, connid 384b0e6911bdc4] sending command datagram 2: {"method":"getPilot","params":{}}
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.88, connid 384b0e6912316c] listening for response datagram
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.88, connid 384b0e6912316c] sending command datagram 0: {"method":"getPilot","params":{}}
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.7, connid 384b0e6911bdc4] response received: {'method': 'getPilot', 'env': 'pro', 'result': {'mac': 'a8bb50a4f94d', 'rssi': -64, 'src': '', 'state': True, 'sceneId': 0, 'temp': 3154, 'dimming': 58}}
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.light] [wizlight 192.168.1.7] updated state: True
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.wizlight] [wizlight 192.168.1.88, connid 384b0e6912316c] response received: {'method': 'getPilot', 'env': 'pro', 'result': {'mac': 'a8bb50a5806c', 'rssi': -53, 'src': '', 'state': True, 'sceneId': 13, 'speed': 100, 'temp': 6500, 'dimming': 100, 'schdPsetId': 11}}
2020-03-18 11:49:56 DEBUG (MainThread) [custom_components.wiz_light.light] [wizlight 192.168.1.88] updated state: True