EspHomeServer

Struct EspHomeServer 

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

High-level ESPHome server implementation.

EspHomeServer provides an easier-to-use abstraction over the ESPHome native API by managing entity keys internally. It handles entity registration, message routing, and maintains state for all registered entities.

This struct uses the builder pattern via the TypedBuilder derive macro, allowing for flexible configuration.

§Examples

use esphome_native_api::esphomeserver::EspHomeServer;

let server = EspHomeServer::builder()
    .name("my-device".to_string())
    .api_version_major(1)
    .api_version_minor(10)
    .encryption_key("your-base64-key".to_string())
    .build();

Implementations§

Source§

impl EspHomeServer

Source

pub fn builder() -> EspHomeServerBuilder<((Arc<AtomicBool>,), (Vec<u8>,), (), (), (), (), (), (), (), (), (), (), (), ())>

Create a builder for building EspHomeServer. On the builder, call .name(...), .password(...)(optional), .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) to set the values of the fields. Finally, call .build() to create the instance of EspHomeServer.

Source§

impl EspHomeServer

Easier version of the API abstraction.

Manages entity keys internally.

Source

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

Starts the ESPHome server and begins communication over the provided TCP stream.

This method initializes the underlying EspHomeApi, establishes the connection, and spawns a background task to handle message routing between the API and registered entities.

§Arguments
  • tcp_stream - An established TCP connection to an ESPHome device
§Returns

Returns a tuple containing:

  • A sender for outgoing messages to the ESPHome device
  • A receiver for incoming messages from the ESPHome device
§Errors

Returns an error if the connection cannot be established or if the initial handshake fails.

§Examples
let stream = TcpStream::connect("192.168.1.100:6053").await?;
let mut server = EspHomeServer::builder().name("client".to_string()).build();
let (tx, mut rx) = server.start(stream).await?;
Source

pub fn add_entity(&mut self, entity_id: &str, entity: Entity)

Adds an entity to the server’s internal registry.

Each entity is assigned a unique key that is managed internally. The entity can be referenced by its string identifier in subsequent operations.

§Arguments
  • entity_id - A unique string identifier for the entity
  • entity - The entity to register
§Examples
let mut server = EspHomeServer::builder().name("server".to_string()).build();
let sensor = Entity::BinarySensor(BinarySensor {
    object_id: "motion_sensor".to_string(),
});
server.add_entity("motion", sensor);

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> 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, 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.