diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/sensor.rs (renamed from examples/temperature.rs) | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/examples/temperature.rs b/examples/sensor.rs index d449cd4..69bd87a 100644 --- a/examples/temperature.rs +++ b/examples/sensor.rs | |||
| @@ -21,46 +21,52 @@ async fn main_task(spawner: Spawner) { | |||
| 21 | }, | 21 | }, |
| 22 | ); | 22 | ); |
| 23 | 23 | ||
| 24 | let constant_temperature_sensor = device.create_temperature_sensor( | 24 | let temperature_sensor = device.create_sensor( |
| 25 | "constant-temperature-sensor-id", | 25 | "random-temperature-sensor-id", |
| 26 | embassy_ha::TemperatureSensorConfig { | 26 | embassy_ha::SensorConfig { |
| 27 | common: embassy_ha::EntityCommonConfig { | 27 | common: embassy_ha::EntityCommonConfig { |
| 28 | name: Some("Constant Temperature Sensor"), | 28 | name: Some("Random Temperature Sensor"), |
| 29 | ..Default::default() | 29 | ..Default::default() |
| 30 | }, | 30 | }, |
| 31 | unit: embassy_ha::TemperatureUnit::Celcius, | 31 | class: embassy_ha::SensorClass::Temperature, |
| 32 | state_class: embassy_ha::StateClass::Measurement, | ||
| 33 | unit: Some(embassy_ha::constants::HA_UNIT_TEMPERATURE_CELSIUS), | ||
| 34 | suggested_display_precision: Some(1), | ||
| 32 | }, | 35 | }, |
| 33 | ); | 36 | ); |
| 34 | 37 | ||
| 35 | let random_temperature_sensor = device.create_temperature_sensor( | 38 | let humidity_sensor = device.create_sensor( |
| 36 | "random-temperature-sensor-id", | 39 | "random-humidity-sensor-id", |
| 37 | embassy_ha::TemperatureSensorConfig { | 40 | embassy_ha::SensorConfig { |
| 38 | common: embassy_ha::EntityCommonConfig { | 41 | common: embassy_ha::EntityCommonConfig { |
| 39 | name: Some("Random Temperature Sensor"), | 42 | name: Some("Random Humidity Sensor"), |
| 40 | ..Default::default() | 43 | ..Default::default() |
| 41 | }, | 44 | }, |
| 42 | unit: embassy_ha::TemperatureUnit::Celcius, | 45 | class: embassy_ha::SensorClass::Humidity, |
| 46 | state_class: embassy_ha::StateClass::Measurement, | ||
| 47 | unit: Some(embassy_ha::constants::HA_UNIT_PERCENTAGE), | ||
| 48 | suggested_display_precision: Some(0), | ||
| 43 | }, | 49 | }, |
| 44 | ); | 50 | ); |
| 45 | 51 | ||
| 46 | spawner.must_spawn(constant_temperature_task(constant_temperature_sensor)); | 52 | spawner.must_spawn(random_temperature_task(temperature_sensor)); |
| 47 | spawner.must_spawn(random_temperature_task(random_temperature_sensor)); | 53 | spawner.must_spawn(random_humidity_task(humidity_sensor)); |
| 48 | 54 | ||
| 49 | device.run(&mut stream).await.unwrap(); | 55 | device.run(&mut stream).await.unwrap(); |
| 50 | } | 56 | } |
| 51 | 57 | ||
| 52 | #[embassy_executor::task] | 58 | #[embassy_executor::task] |
| 53 | async fn constant_temperature_task(mut sensor: embassy_ha::TemperatureSensor<'static>) { | 59 | async fn random_temperature_task(mut sensor: embassy_ha::Sensor<'static>) { |
| 54 | loop { | 60 | loop { |
| 55 | sensor.publish(42.0); | 61 | sensor.publish(rand::random_range(0.0..50.0)); |
| 56 | Timer::after_secs(1).await; | 62 | Timer::after_secs(1).await; |
| 57 | } | 63 | } |
| 58 | } | 64 | } |
| 59 | 65 | ||
| 60 | #[embassy_executor::task] | 66 | #[embassy_executor::task] |
| 61 | async fn random_temperature_task(mut sensor: embassy_ha::TemperatureSensor<'static>) { | 67 | async fn random_humidity_task(mut sensor: embassy_ha::Sensor<'static>) { |
| 62 | loop { | 68 | loop { |
| 63 | sensor.publish(rand::random_range(0.0..50.0)); | 69 | sensor.publish(rand::random_range(0.0..100.0)); |
| 64 | Timer::after_secs(1).await; | 70 | Timer::after_secs(1).await; |
| 65 | } | 71 | } |
| 66 | } | 72 | } |
