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-19.3.0.tar.gz (93.7 kB view details)

Uploaded Source

Built Distributions

aioesphomeapi-19.3.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-19.3.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-19.3.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-19.3.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-19.3.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-19.3.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-19.3.0.tar.gz.

File metadata

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

File hashes

Hashes for aioesphomeapi-19.3.0.tar.gz
Algorithm Hash digest
SHA256 f7e77cb2e319dc729967bf7624ab9d7d6f659cbcf3bad350c8b1215a3f48015b
MD5 9299fe1312bfa03576ba7d24b127f371
BLAKE2b-256 b3f6ef358927d553671106dad0dbed00f3d2f04bacb709cd87bf25d995989f7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioesphomeapi-19.3.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 528c1bc39facb1820e9185d75f5bd4fdbdefff34737b9aad9bd9ca9d5f15e1a2
MD5 9bd5295d7d00420aa495789d99042a82
BLAKE2b-256 fcd9e306a77ac561f0e7e93644ffd46b62819874ff67ae8f6aa3690b4445c761

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioesphomeapi-19.3.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6f4fcea0bf124f3e1c2068c2e7add812ecbf99008ddc8c6c2f2082cbaaa3bff1
MD5 51a6077e5f3299d31adb6a6df1b01c26
BLAKE2b-256 29784ebaeed46d84f8ed51f52e0fcff7c44775d75c1f6ff9b5a46ab68f36b167

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioesphomeapi-19.3.0-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 f1edc2a1668771f9d35017019eaf7e8ac8d1540b7dd4c16ab124c144c36d5ba5
MD5 5fb6dcca7e1a970fd5b8716ca6a32a8d
BLAKE2b-256 07ae2845ff7fbf504b4dfbac993c239f4b5d79a215fc03cf09741c37f5004c43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioesphomeapi-19.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5154d9fbe829c09bc5f1321242241f9921755aea36268c76dbe5999538f0fc2e
MD5 e8dcd7cc26fdfedc2cb82180cc96d83b
BLAKE2b-256 9a164ac82731e7a4eb8ae9cf04b1961fbad6ea12c01338f17836c27d20816c22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioesphomeapi-19.3.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4aea13ae402467d631aa28d07e406632cd81773a0869c749c5fd1320dcdafe22
MD5 2348de9b0b4d495b740b17b862401532
BLAKE2b-256 b1741ebb5fbe63fe01eb924f14ed1e3d1f2c3392ba2b284e37e0d0e0f46b0654

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aioesphomeapi-19.3.0-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 0abf1d341c9f72c532b49d0b9874fa702cd191c9f3da57780746f45acb12f607
MD5 9e330753bceb9817d4c2c1260a67e95a
BLAKE2b-256 0f5e52039a8600d4505ff5cc4237ed25580f0075ca44ddac8fce6896023fb9ca

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