Reputation: 61
So basically I am trying to write a program that can control a wiz light through a python script. I am using python 3.6 because of other limitations. Here is the link to the pywizlight project that I am using to connect to the wizlight.
https://pypi.org/project/pywizlight/
I understand that the code says that it will only work in python 3.7 and above, however I am wondering if it is possible to make it work in 3.6. I know that it might be hard to test this if you do not have a wiz light (maybe). I get the following error message when I run it:
RuntimeWarning:
coroutine 'wizlight.turn_on' was never awaited`
light.turn_on(PilotBuilder(brightness = 255))
And here is the code:
import pywizlight
import asyncio
from pywizlight import wizlight, PilotBuilder, discovery
async def main():
loop = asyncio.get_event_loop()
task = loop.create_task(turn_off())
await task
async def turn_off():
light = wizlight("ip address of wiz bulb")
light.turn_on(PilotBuilder(brightness = 255))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Can anyone make it work? I have about a total of 2 braincells, so I cannot.
Upvotes: 3
Views: 1767
Reputation: 61
Ok, I managed to figure out what was going on, and I am leaving this question here in case anyone else is trying to do a similar thing with pywizlight in python 3.6. My issue was that I kept getting a multitude of errors, but mainly I ran into the problem that I could not create a task, this was because it is python 3.6. The create task module was implemented in python 3.7. The way to fix this was in the package itself, specifically in the python file "bulb.py". Upon changing "asyncio.create_task
" to "asyncio.ensure_future
" in lines 210, 217, 295, and 306, run the following code:
async def turn_off():
light = wizlight("your wiz light ip address here")
await light.turn_on(PilotBuilder(brightness = 255))
await light.turn_off()
await light.turn_on(PilotBuilder(rgb = (0, 128, 255)))
await light.turn_off()
loop = asyncio.get_event_loop()
loop.run_until_complete(turn_off())
The light would turn on, turn off, turn on again but as a color, and then turn off. I hope that this helps people who are trying to accomplish a similar thing that I did.
Upvotes: 3
Reputation: 1411
Have you tried adding an await
directive?
async def turn_off():
light = wizlight("ip address of wiz bulb")
await light.turn_on(PilotBuilder(brightness = 255))
I cannot test it since I do not have a wizlight, but you may find success in only creating one event loop and using it to perform the async task from turn_off
.
async def turn_off():
light = wizlight("ip address of wiz bulb")
await light.turn_on(PilotBuilder(brightness = 255))
loop = asyncio.get_event_loop()
loop.run_until_complete(turn_off())
This other SO answer explains the event loop being used in further detail.
Upvotes: 2