diff options
| author | diogo464 <[email protected]> | 2025-12-05 15:09:09 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-05 15:09:09 +0000 |
| commit | 9696489d5f1807a507214d6fcdecac4d47e0356d (patch) | |
| tree | 4896eb452c7e410cb1166a1dbd13bcea45861516 /src/entity_sensor.rs | |
| parent | 0c86da392af50c7588b087c3f72602e8368af65e (diff) | |
reworked entity storage
Diffstat (limited to 'src/entity_sensor.rs')
| -rw-r--r-- | src/entity_sensor.rs | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/entity_sensor.rs b/src/entity_sensor.rs index 5d7794f..d70e80e 100644 --- a/src/entity_sensor.rs +++ b/src/entity_sensor.rs | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, TemperatureUnit, constants}; | 1 | use crate::{ |
| 2 | Entity, EntityCommonConfig, EntityConfig, NumericSensorState, TemperatureUnit, constants, | ||
| 3 | }; | ||
| 2 | 4 | ||
| 3 | #[derive(Debug, Default)] | 5 | #[derive(Debug, Default)] |
| 4 | pub struct TemperatureSensorConfig { | 6 | pub struct TemperatureSensorConfig { |
| @@ -23,8 +25,19 @@ impl<'a> TemperatureSensor<'a> { | |||
| 23 | } | 25 | } |
| 24 | 26 | ||
| 25 | pub fn publish(&mut self, temperature: f32) { | 27 | pub fn publish(&mut self, temperature: f32) { |
| 26 | use core::fmt::Write; | 28 | let publish = self.0.with_data(|data| { |
| 27 | self.0 | 29 | let storage = data.storage.as_numeric_sensor_mut(); |
| 28 | .publish_with(|view| write!(view, "{}", temperature).unwrap()); | 30 | let prev_state = storage.state.replace(NumericSensorState { |
| 31 | value: temperature, | ||
| 32 | timestamp: embassy_time::Instant::now(), | ||
| 33 | }); | ||
| 34 | match prev_state { | ||
| 35 | Some(state) => state.value != temperature, | ||
| 36 | None => true, | ||
| 37 | } | ||
| 38 | }); | ||
| 39 | if publish { | ||
| 40 | self.0.queue_publish(); | ||
| 41 | } | ||
| 29 | } | 42 | } |
| 30 | } | 43 | } |
