use esphome_client::discovery;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
println!("=== ESPHome Device Discovery ===");
println!("Searching for devices on the local network...\n");
let mut discovered_devices = std::collections::HashSet::new();
let mut result_stream = discovery::Client::default().discover()?;
println!("Listening for device announcements (press Ctrl+C to stop)...\n");
loop {
match result_stream.next().await {
Ok(device) => {
let device_id = format!(
"{}:{}",
device.hostname(),
device
.socket_address()
.map(|s| s.to_string())
.unwrap_or_default()
);
if discovered_devices.insert(device_id) {
println!("=== New Device Discovered ===");
println!("Hostname: {}", device.hostname());
if let Some(addr) = device.socket_address() {
println!("Address: {}", addr);
}
println!(
"Encrypted: {}",
if device.has_encryption() { "Yes" } else { "No" }
);
println!("Attributes:");
for (key, value) in device.attributes() {
println!(" {} = {}", key, value);
}
println!();
}
}
Err(e) => {
eprintln!("Discovery error: {}", e);
break;
}
}
}
println!("\nDiscovery stopped.");
Ok(())
}