Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using API #1

Open
chrisoutwright opened this issue Jan 23, 2020 · 1 comment
Open

Using API #1

chrisoutwright opened this issue Jan 23, 2020 · 1 comment

Comments

@chrisoutwright
Copy link

chrisoutwright commented Jan 23, 2020

Could you explain how the API could be used, after creating an HttpCam instance?

The methods implement asyncio, how should one proceed? I tried this guide, but to no avail.

Either I get cannot 'yield from' a coroutine object in a non-coroutine generator or <_GatheringFuture pending>. When calling the method itself, I get a <coroutine object Wansview.async_get_night_mode at ...> but I fail to be able to close it and display the message I should get from the called method.

Would this work?

loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(cam.async_set_night_mode("auto"))]
loop.run_until_complete(asyncio.wait(tasks))

How does one call these methods correctly, I've never used asyncio before and can't figure it out how to use it in this case. Thank you.

@HelpfulScripts
Copy link
Owner

HelpfulScripts commented Mar 22, 2020

I wrote the library to connect some cameras I have to home-assistant. There, the library integrates quite nicely. E.g. I can override the async_camera_image method of the core Camera class and hand the call through to the libhttpcam as follows (self._cam holds a reference to the HttpCam instance):

    async def async_camera_image(self):
        """Return bytes of camera image."""
        (code, result) = await self._cam.async_snap_picture()
        return result

Of course, home-assistant handles the async part here, so that doesn't quite address your question on how to deal with the async errors.
I use the async/await patterns quite regularly in ECMAScript; unfortunately I don't have a deep familiarity with the equivalent Python mechanisms. Judging from the error you get, could it be that you expect a message back, where the async method is in fact returning a Future (or Promise in ECMAScript)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants