Skip to main content

EspHomeApi

Struct EspHomeApi 

Source
pub struct EspHomeApi { /* private fields */ }
Expand description

Low-level ESPHome native API client.

EspHomeApi provides direct access to the ESPHome native API protocol, handling connection setup, encryption, and message framing. This is the lower-level API that crate::esphomeserver::EspHomeServer builds upon.

This struct supports both encrypted and plaintext connections and uses the builder pattern for configuration via TypedBuilder.

§Builder Options

  • name: Device name (required)
  • encryption_key: Base64-encoded encryption key (optional, enables encryption)
  • api_version_major: API version major number (default: 1)
  • api_version_minor: API version minor number (default: 10)
  • server_info: Server identification string (default: “Rust: esphome-native-api”)
  • friendly_name: Human-readable device name (optional)
  • mac: MAC address (optional)
  • model: Device model (optional)
  • manufacturer: Device manufacturer (optional)
  • suggested_area: Suggested area for the device (optional)
  • bluetooth_mac_address: Bluetooth MAC address (optional)

§Examples

use esphome_native_api::esphomeapi::EspHomeApi;

let api = EspHomeApi::builder()
    .name("bedroom-light".to_string())
    .api_version_major(1)
    .api_version_minor(10)
    .friendly_name("Bedroom Light".to_string())
    .build();

Implementations§

Source§

impl EspHomeApi

Source

pub fn builder() -> EspHomeApiBuilder<((), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), (), ())>

Create a builder for building EspHomeApi. On the builder, call .first_message_received(...)(optional), .plaintext_communication(...)(optional), .name(...), .encryption_key(...)(optional), .api_version_major(...)(optional), .api_version_minor(...)(optional), .server_info(...)(optional), .friendly_name(...)(optional), .mac(...)(optional), .model(...)(optional), .manufacturer(...)(optional), .suggested_area(...)(optional), .bluetooth_mac_address(...)(optional), .project_name(...)(optional), .project_version(...)(optional), .compilation_time(...)(optional), .legacy_bluetooth_proxy_version(...)(optional), .bluetooth_proxy_feature_flags(...)(optional), .legacy_voice_assistant_version(...)(optional), .voice_assistant_feature_flags(...)(optional) to set the values of the fields. Finally, call .build() to create the instance of EspHomeApi.

Source§

impl EspHomeApi

Handles the ESPHome API protocol with encryption support.

Source

pub async fn start( &mut self, tcp_stream: TcpStream, ) -> Result<(Sender<ProtoMessage>, Receiver<ProtoMessage>), Box<dyn Error>>

Starts the API client and establishes communication with an ESPHome device.

This method performs the complete connection handshake, including:

  1. Detecting whether encryption is required
  2. Performing encryption handshake if needed
  3. Exchanging hello messages
  4. Setting up message routing
§Arguments
  • tcp_stream - An established TCP connection to the ESPHome device
§Returns

Returns a tuple containing:

  • An mpsc::Sender for sending messages to the device
  • A broadcast::Receiver for receiving messages from the device
§Errors

Returns an error if:

  • The connection fails
  • The encryption handshake fails
  • The hello exchange fails
  • The device requires encryption but no key was provided
§Examples
let stream = TcpStream::connect("192.168.1.100:6053").await?;
let mut api = EspHomeApi::builder().name("client".to_string()).build();
let (tx, mut rx) = api.start(stream).await?;

Trait Implementations§

Source§

impl Clone for EspHomeApi

Source§

fn clone(&self) -> EspHomeApi

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.