Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await and async, coroutine was never awaited

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.

like image 859
kill me Avatar asked Jan 20 '26 09:01

kill me


2 Answers

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.

like image 163
kill me Avatar answered Jan 23 '26 00:01

kill me


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.

like image 38
SpaceKatt Avatar answered Jan 23 '26 01:01

SpaceKatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!