Hi all, I’ve been using the python3 library from zranger:
and despite my minimal python background, I’ve blundered cursing and learning through a zillion inconsistent tab and space errors.
I’ve been running into a bit of a challenge getting a fade in / fade out function running without timeout errors. Perhaps I’m asking too much of the websocket API, but I was hoping for a way to have the current pattern simply fade out, then I suppose it could be set to a blank pattern in the background while ‘off’.
I have been doing the fade over 3 seconds, and I find that if I use just 10 steps (very choppy), there are no issues, but if I do 50 steps (smoother) there is usually a timeout error that is thrown.
My code:
#!/usr/bin/env python3
“”"
uses the pixelblaze library from, and based partially on the example scripy by JEM(ZRanger1)
Requires the pixelblaze, which also requires websocket-client
“”"
from pixelblaze import *
import sysARG_IP = sys.argv[1]
ARG_ACTION = sys.argv[2]def listPB():
pbList = PixelblazeEnumerator()
print(“Testing: PixelblazeEnumerator object created – listening for Pixelblazes”)
time.sleep(2)
print("Available Pixelblazes: ", pbList.getPixelblazeList())def setOasis():
pb = Pixelblaze(ARG_IP)
pb.stopSequencer() # make sure the sequencer isn’t running
pb.setActivePattern(‘Oasis’)
pb.waitForEmptyQueue(1000)
pb.setControl(‘aura’, color, False)
pb.close()def setSound():
pb = Pixelblaze(ARG_IP)
pb.stopSequencer() # make sure the sequencer isn’t running
pb.setActivePattern(‘sound - pew-pew-pew!’)
pb.close()def setBrightness(brightness):
pb = Pixelblaze(ARG_IP)
pb.setBrightness(brightness)
pb.close()def fadeIn():
steps = 50
totalduration = 3 #number of seconds over which to fade in
brightness = 0
intervald = totalduration/steps
pb = Pixelblaze(ARG_IP)
pb.waitForEmptyQueue(1000)
for i in range(steps):
brightness = (i/steps)
pb.setBrightness(brightness)
print(brightness)
time.sleep(intervald)
time.sleep(1) # error handling, attempting to prevent timeout errors, and ensure final on/off setting correct
pb.setBrightness(1)
if pb.waitForEmptyQueue(2000):
print(‘complete’)
else:
print(‘timed out’)
pb.close()def fadeOut():
steps = 50
totalduration = 3 #number of seconds over which to fade out
brightness = 1
intervald = totalduration/steps
pb = Pixelblaze(ARG_IP)
pb.waitForEmptyQueue(1000)
for i in range(steps):
brightness = 1-(i/steps)
pb.setBrightness(brightness)
print(brightness)
time.sleep(intervald)
time.sleep(1) # error handling, attempting to prevent timeout errors, and ensure final on/off setting correct
pb.setBrightness(0)
if pb.waitForEmptyQueue(2000):
print(‘complete’)
else:
print(‘timed out’)
pb.close()def ListPatterns():
pb = Pixelblaze(ARG_IP)
pb.stopSequencer() # make sure the sequencer isn’t running
result = pb.getPatternList()
for key, value in result.items():
print(key, ’ : ', value)
time.sleep(1)
pb.waitForEmptyQueue(1000)
pb.close()def ListVariables():
pb = Pixelblaze(ARG_IP)
pb.waitForEmptyQueue(1000)
print("Variables: ",pb.getVars())
pb.close()def setColor(color):
pb = Pixelblaze(ARG_IP)
pb.waitForEmptyQueue(1000)
pb.setControl(‘aura’, color, False)if ARG_ACTION == ‘fadein’:
fadeIn()
elif ARG_ACTION == ‘fadeout’:
fadeOut()
elif ARG_ACTION == ‘on’:
setBrightness(1)
elif ARG_ACTION == ‘off’:
setBrightness(0)
elif ARG_ACTION == ‘list’:
listPB()
elif ARG_ACTION == ‘brightness’:
newBrightness = int(sys.argv[3])
setBrightness(newBrightness)
elif ARG_ACTION == ‘oasis’:
setOasis()
elif ARG_ACTION == ‘sound’:
setSound()
elif ARG_ACTION == ‘pattern’:
pb = Pixelblaze(ARG_IP)
pb.stopSequencer() # make sure the sequencer isn’t running
print("Attempting to set pattern to: ", sys.argv[3])
pb.setActivePattern(‘sys.argv[3]’)
pb.close()
elif ARG_ACTION == ‘listvariables’:
ListVariables()
elif ARG_ACTION == ‘listpatterns’:
ListPatterns()
elif ARG_ACTION == ‘aura’:
setColor(float(sys.argv[3]))
Yes, I am aware there are probably a myriad of problems with the above script, and I know that the setting color (targeting Oasis for now, which uses aura as the variable) doesn’t work, neither does the setting pattern. Currently setting brightness is working, which was my main (initial) goal.
Errors thrown when I attempt to run : python3 PBScript.py 192.168.1.234 fadein
Traceback (most recent call last):
File "/home/openhabian/.local/lib/python3.7/site-packages/websocket/socket.py", line 113, in recv
bytes = _recv()
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_socket.py”, line 95, in _recv
return sock.recv(bufsize)
socket.timeout: timed outDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “PBScript.py”, line 100, in
fadeIn()
File “PBScript.py”, line 52, in fadeIn
if pb.waitForEmptyQueue(2000):
File “/var/lib/openhab2/pixelblaze.py”, line 170, in waitForEmptyQueue
result = self.ws.recv()
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_core.py”, line 353, in recv
opcode, data = self.recv_data()
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_core.py”, line 376, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_core.py”, line 395, in recv_data_frame
frame = self.recv_frame()
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_core.py”, line 431, in recv_frame
return self.frame_buffer.recv_frame()
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_abnf.py”, line 372, in recv_frame
self.recv_header()
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_abnf.py”, line 320, in recv_header
header = self.recv_strict(2)
File "/home/openhabian/.local/lib/python3.7/site-packages/websocket/abnf.py", line 407, in recv_strict
bytes = self.recv(min(16384, shortage))
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_core.py”, line 514, in _recv
return recv(self.sock, bufsize)
File “/home/openhabian/.local/lib/python3.7/site-packages/websocket/_socket.py”, line 116, in recv
raise WebSocketTimeoutException(message)
websocket._exceptions.WebSocketTimeoutException: timed out
Any suggestions on ways to get a smoother timeout? Is this simply overloading the API, and I should expect to run into a wall like this?
Thanks