Skip to main content

Module esphomeapi

Module esphomeapi 

Source
Expand description

Low-level ESPHome native API implementation.

This module provides EspHomeApi, which handles the core protocol communication with ESPHome devices. It manages connection establishment, encryption handshakes, message framing, and protocol state.

§Examples

§Plaintext Connection

use esphome_native_api::esphomeapi::EspHomeApi;
use tokio::net::TcpStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream = TcpStream::connect("192.168.1.100:6053").await?;
     
    let mut api = EspHomeApi::builder()
        .name("my-client".to_string())
        .build()?;
     
    let connection = api.start(stream).await?;
    let tx = connection.sender();
    let mut rx = connection.receiver();
    Ok(())
}

§Encrypted Connection

use esphome_native_api::esphomeapi::EspHomeApi;
use tokio::net::TcpStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let stream = TcpStream::connect("192.168.1.100:6053").await?;
     
    let mut api = EspHomeApi::builder()
        .name("my-client".to_string())
        .encryption_key("your-base64-encoded-key".to_string())
        .build()?;
     
    let connection = api.start(stream).await?;
    let tx = connection.sender();
    let mut rx = connection.receiver();
    Ok(())
}

Structs§

EspHomeApi
Low-level ESPHome native API client.

Type Aliases§

EspHomeApiBuildResult
Fallible output of EspHomeApi’s builder. An Err means the configuration was invalid — currently, an encryption key that is not valid base64.