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
impl EspHomeServer
Sourcepub fn builder() -> EspHomeServerBuilder<((Arc<AtomicBool>,), (Vec<u8>,), (), (), (), (), (), (), (), (), (), (), (), ())>
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.
impl EspHomeServer
Easier version of the API abstraction.
Manages entity keys internally.
Sourcepub async fn start(
&mut self,
tcp_stream: TcpStream,
) -> Result<(Sender<ProtoMessage>, Receiver<ProtoMessage>), Box<dyn Error>>
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?;Sourcepub fn add_entity(&mut self, entity_id: &str, entity: Entity)
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 entityentity- 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);