3

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.

kill me
  • 61
  • 5

2 Answers2

3

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.

kill me
  • 61
  • 5
2

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))

EDIT: Addressing additional question in comments

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.

SpaceKatt
  • 976
  • 6
  • 12
  • I have tried adding an await directive, I then get the error, module 'asyncio' has no attribute 'create_task'. I believe this is because I am in python 3.6, but I do not know. – kill me Feb 25 '21 at 05:11
  • @killme , I have updated my answer above to address your addition error. If it ends up working for you, then please mark the answer as accepted n_____n – SpaceKatt Feb 25 '21 at 16:29