diff options
| author | diogo464 <[email protected]> | 2025-12-04 23:59:21 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-04 23:59:21 +0000 |
| commit | 40a16d099c2bf2b3e5bf537aa14d37fdf9e52668 (patch) | |
| tree | fae2ec76ca797e1e55d678cabea743c91d30de5c /examples | |
| parent | abdcb51d7e9d6ebfacdf17b32e5296464e0aa2d4 (diff) | |
added basic binary sensor example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/binary_sensor.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/binary_sensor.rs b/examples/binary_sensor.rs new file mode 100644 index 0000000..2809255 --- /dev/null +++ b/examples/binary_sensor.rs | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | mod common; | ||
| 2 | |||
| 3 | use common::AsyncTcp; | ||
| 4 | use embassy_executor::{Executor, Spawner}; | ||
| 5 | use embassy_time::Timer; | ||
| 6 | use static_cell::StaticCell; | ||
| 7 | |||
| 8 | static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new(); | ||
| 9 | |||
| 10 | #[embassy_executor::task] | ||
| 11 | async fn main_task(spawner: Spawner) { | ||
| 12 | let mut stream = AsyncTcp::connect(std::env!("MQTT_ADDRESS")); | ||
| 13 | |||
| 14 | let mut device = embassy_ha::Device::new( | ||
| 15 | RESOURCES.init(Default::default()), | ||
| 16 | embassy_ha::DeviceConfig { | ||
| 17 | device_id: "example-device-id", | ||
| 18 | device_name: "Example Device Name", | ||
| 19 | manufacturer: "Example Device Manufacturer", | ||
| 20 | model: "Example Device Model", | ||
| 21 | }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | let sensor = device.create_binary_sensor( | ||
| 25 | "binary-sensor-id", | ||
| 26 | "Binary Sensor", | ||
| 27 | embassy_ha::constants::HA_DEVICE_CLASS_BINARY_SENSOR_SMOKE, | ||
| 28 | ); | ||
| 29 | |||
| 30 | spawner.must_spawn(binary_sensor_class(sensor)); | ||
| 31 | |||
| 32 | device.run(&mut stream).await; | ||
| 33 | } | ||
| 34 | |||
| 35 | #[embassy_executor::task] | ||
| 36 | async fn binary_sensor_class(mut switch: embassy_ha::BinarySensor<'static>) { | ||
| 37 | loop { | ||
| 38 | let state = switch.toggle(); | ||
| 39 | println!("state = {}", state); | ||
| 40 | Timer::after_secs(2).await; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | example_main!(); | ||
