Skip to main content

Python API for interacting with ESPHome devices.

Project description

https://github.com/esphome/aioesphomeapi/workflows/CI/badge.svg https://img.shields.io/pypi/v/aioesphomeapi.svg https://codecov.io/gh/esphome/aioesphomeapi/branch/main/graph/badge.svg

aioesphomeapi allows you to interact with devices flashed with ESPHome.

Installation

The module is available from the Python Package Index.

$ pip3 install aioesphomeapi

An optional cython extension is available for better performance, and the module will try to build it automatically.

The extension requires a C compiler and Python development headers. The module will fall back to the pure Python implementation if they are unavailable.

Building the extension can be forcefully disabled by setting the environment variable SKIP_CYTHON to 1.

Usage

It’s required that you enable the Native API component for the device.

# Example configuration entry
api:
  password: 'MyPassword'

Check the output to get the local address of the device or use the name:``under ``esphome: from the device configuration.

[17:56:38][C][api:095]: API Server:
[17:56:38][C][api:096]:   Address: api_test.local:6053

The sample code below will connect to the device and retrieve details.

import aioesphomeapi
import asyncio

async def main():
    """Connect to an ESPHome device and get details."""

    # Establish connection
    api = aioesphomeapi.APIClient("api_test.local", 6053, "MyPassword")
    await api.connect(login=True)

    # Get API version of the device's firmware
    print(api.api_version)

    # Show device details
    device_info = await api.device_info()
    print(device_info)

    # List all entities of the device
    entities = await api.list_entities_services()
    print(entities)

 loop = asyncio.get_event_loop()
 loop.run_until_complete(main())

Subscribe to state changes of an ESPHome device.

import aioesphomeapi
import asyncio

async def main():
    """Connect to an ESPHome device and wait for state changes."""
    cli = aioesphomeapi.APIClient("api_test.local", 6053, "MyPassword")

    await cli.connect(login=True)

    def change_callback(state):
        """Print the state changes of the device.."""
        print(state)

    # Subscribe to the state changes
    await cli.subscribe_states(change_callback)

loop = asyncio.get_event_loop()
try:
    asyncio.ensure_future(main())
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    loop.close()

Other examples:

Development

For development is recommended to use a Python virtual environment (venv).

# Setup virtualenv (optional)
$ python3 -m venv .
$ source bin/activate
# Install aioesphomeapi and development depenencies
$ pip3 install -e .
$ pip3 install -r requirements_test.txt

# Run linters & test
$ script/lint
# Update protobuf _pb2.py definitions (requires a protobuf compiler installation)
$ script/gen-protoc

A cli tool is also available for watching logs:

aioesphomeapi-logs --help

A cli tool is also available to discover devices:

aioesphomeapi-discover

License

aioesphomeapi is licensed under MIT, for more details check LICENSE.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioesphomeapi-20.0.0.tar.gz (93.9 kB view details)

Uploaded Source

Built Distributions

aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.2 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (2.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

File details

Details for the file aioesphomeapi-20.0.0.tar.gz.

File metadata

  • Download URL: aioesphomeapi-20.0.0.tar.gz
  • Upload date:
  • Size: 93.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for aioesphomeapi-20.0.0.tar.gz
Algorithm Hash digest
SHA256 1b42a7cc37d1565f3330b93abee259afb73e80fca370296ba6d434d2bb48db05
MD5 509069d62c130bddb8c68507249917f2
BLAKE2b-256 1b14cf3a4ec4fd2985b2c6f8760007d6718616725ae9d106c9908416ae2994f5

See more details on using hashes here.

File details

Details for the file aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d38b150cdec06661d3f47050e7c96e98227153d5b8ad93326ade5521f16f8ecb
MD5 255d5bc812adbbe02b136eb05e5c6585
BLAKE2b-256 db4722c2aa2439ca88a38216a24eaeb5cd2a4ed86ab10e492ffe584fb9b30e42

See more details on using hashes here.

File details

Details for the file aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9a25f38ee6ae62680a640b6b9f55012f8ec3b09e7855fab8ecb4431712f307cb
MD5 986b73b870e63a96489a20a2205e47a6
BLAKE2b-256 48b88d5441095d18abf69c6649b6b5e700829abc87f60478d637a5710e86b374

See more details on using hashes here.

File details

Details for the file aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for aioesphomeapi-20.0.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 e9efe83fa3e0d60cc73cad1aa2648e898fa96b249485e76388e1955d090c4592
MD5 c60dbac8e55f530151c58d3bb22d22c4
BLAKE2b-256 5a350bb432710cfd80e5ae4f487ad28e3adb32af6e9ad8850405205269a7cebc

See more details on using hashes here.

File details

Details for the file aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a5ca3e28b6bd77f08759fde3b6665b8263a5c7a6034ddcf78311176a39759ac7
MD5 e1d5cf57f84320deaaba930a865007b7
BLAKE2b-256 e9f93a60154d2c23e4dfa97f841402dbfff81195dfa64fa585d6df2bfc143526

See more details on using hashes here.

File details

Details for the file aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 26352221b37ca7a0f5ee1605389a1390061836e5c7895d0458b129a9efbf7b78
MD5 ab526c5c99d7d836dedab68a868661a8
BLAKE2b-256 45913680fef2859ca97792a1b54a971825255c3daf8e59275158efbb6866e0ed

See more details on using hashes here.

File details

Details for the file aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for aioesphomeapi-20.0.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 df16dd2bede190340ab3bed00c0b0c0fa9f26c50a60352686949143dad34c24a
MD5 486088ad0c830e1a6d83a357a5ac5265
BLAKE2b-256 917e1bd0de292398a92b0adf59f88fc9bf712753bd31e72efe6a95f7bba88b95

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page