aioesphomeapi
aioesphomeapi is an asynchronous Python API client designed for interacting with devices running ESPHome firmware. It provides a way for Python applications, notably Home Assistant, to communicate with ESPHome devices using their native API for real-time control and monitoring. The library is actively maintained with frequent updates, often aligning with ESPHome and Home Assistant releases. The current version is 44.13.3.
Warnings
- breaking Password authentication was removed in ESPHome 2026.1.0. Devices running this version or newer will only accept connections using a `noise_psk` (encryption key). Attempting to connect with only a password will fail.
- deprecated The `password` argument in `aioesphomeapi.APIClient` is deprecated. While it still works for older ESPHome devices, it's recommended to switch to `noise_psk` for security and future compatibility.
- gotcha The ESPHome device *must* have the Native API component enabled in its YAML configuration for `aioesphomeapi` to connect. Without it, the device will not be listening for API connections.
- gotcha For optimal performance, `aioesphomeapi` can use an optional Cython extension. If this extension cannot be built (e.g., due to missing C compiler or Python development headers), the library will silently fall back to a pure Python implementation, which may be slower.
- gotcha `aioesphomeapi` uses Protocol Buffers for communication and must be kept consistent with the ESPHome firmware version on your devices. Breaking changes in the ESPHome API protocol require corresponding updates in `aioesphomeapi` to maintain compatibility.
Install
-
pip install aioesphomeapi -
pip install aioesphomeapi --prefer-binary
Imports
- APIClient
from aioesphomeapi import APIClient
- DeviceInfo
from aioesphomeapi.model import DeviceInfo
Quickstart
import aioesphomeapi
import asyncio
import os
async def main():
host = os.environ.get('ESPHOME_HOST', 'device.local')
port = int(os.environ.get('ESPHOME_PORT', 6053))
noise_psk = os.environ.get('ESPHOME_NOISE_PSK', '') # Recommended for secure communication
password = os.environ.get('ESPHOME_PASSWORD', '') # Deprecated, use noise_psk
if not noise_psk and not password:
print("Warning: No encryption key or password provided. Connection might fail or be insecure.")
# Establish connection
api = aioesphomeapi.APIClient(
host,
port,
noise_psk=noise_psk if noise_psk else None,
password=password if password else None
)
try:
await api.connect(login=True)
print(f"Successfully connected to {host}:{port}")
# Get API version of the device's firmware
print(f"API Version: {api.api_version}")
# Show device details
device_info = await api.device_info()
print(f"Device Info: {device_info.name} ({device_info.mac_address})")
# List all entities of the device
entities = await api.list_entities_services()
print(f"Number of entities: {len(entities.entities)}")
# for entity in entities.entities:
# print(f" - {entity.name} ({entity.key})")
except aioesphomeapi.APIConnectionError as e:
print(f"Connection error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
if api.is_connected:
await api.disconnect()
print("Disconnected.")
if __name__ == "__main__":
asyncio.run(main())