1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Handle to a live ESPHome API connection.
use ;
use crateError;
use crateProtoMessage;
/// A live connection to an ESPHome peer.
///
/// Returned by [`crate::esphomeapi::EspHomeApi::start`] and
/// [`crate::esphomeserver::EspHomeServer::start`], this handle owns the channels
/// used to talk to the peer and lets a consumer observe when — and why — the
/// connection ends.
///
/// # Observing termination
///
/// Unlike a bare `(Sender, Receiver)` pair, a `Connection` reports its terminal
/// outcome via [`Connection::wait`]. A [`Error::Disconnected`] result is the
/// normal way a session ends and is usually not treated as a failure:
///
/// ```rust,no_run
/// # use esphome_native_api::{esphomeapi::EspHomeApi, Error};
/// # use tokio::net::TcpStream;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let stream = TcpStream::connect("192.168.1.100:6053").await?;
/// let api = EspHomeApi::builder().name("client".to_string()).build()?;
/// let connection = api.start(stream).await?;
///
/// let sender = connection.sender();
/// let mut receiver = connection.receiver();
///
/// match connection.wait().await {
/// Ok(()) | Err(Error::Disconnected(_)) => { /* peer left — expected */ }
/// Err(e) => eprintln!("connection fault: {e}"),
/// }
/// # let _ = (sender, &mut receiver);
/// # Ok(())
/// # }
/// ```