Skip to content

Instantly share code, notes, and snippets.

@micw
Created February 6, 2020 19:24
Show Gist options
  • Save micw/202f9dee5c990f0b0f7e7c36b567d92b to your computer and use it in GitHub Desktop.
Save micw/202f9dee5c990f0b0f7e7c36b567d92b to your computer and use it in GitHub Desktop.
Getting camera images from esp32cam with esphome and python
#!/usr/bin/env python3
import aioesphomeapi
import asyncio
async def main():
loop = asyncio.get_running_loop()
cli = aioesphomeapi.APIClient(loop, "HOST-OR-IP", 6053, "API-PASSWORD-OR_EMPTY")
await cli.connect(login=True)
def cb(state):
if type(state) == aioesphomeapi.CameraState:
try:
with open('out/x.jpg','wb') as out:
out.write(state.image)
print("Image written")
except Exception as e:
print(e)
await cli.subscribe_states(cb)
l = await cli.list_entities_services()
print(l)
loop = asyncio.get_event_loop()
try:
asyncio.ensure_future(main())
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.close()
@lululombard
Copy link

This is no longer working and needs some updates

@a-w
Copy link

a-w commented Feb 20, 2025

This is no longer working and needs some updates

The member of CameraState in which to find the image is called data, not image. If you write

    def cb(state):
        if isinstance(state, aioesphomeapi.CameraState):
        try:
            with open('out/x.jpg', 'wb') as out:
                out.write(state.data)
            print("Image written")
        except Exception as e:
            print(e)

it should work

@wilfredallyn
Copy link

subscribe_states is no longer async so await should be removed from Line 21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment