From 9696489d5f1807a507214d6fcdecac4d47e0356d Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 5 Dec 2025 15:09:09 +0000 Subject: reworked entity storage --- src/entity_sensor.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/entity_sensor.rs') 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 @@ -use crate::{Entity, EntityCommonConfig, EntityConfig, TemperatureUnit, constants}; +use crate::{ + Entity, EntityCommonConfig, EntityConfig, NumericSensorState, TemperatureUnit, constants, +}; #[derive(Debug, Default)] pub struct TemperatureSensorConfig { @@ -23,8 +25,19 @@ impl<'a> TemperatureSensor<'a> { } pub fn publish(&mut self, temperature: f32) { - use core::fmt::Write; - self.0 - .publish_with(|view| write!(view, "{}", temperature).unwrap()); + let publish = self.0.with_data(|data| { + let storage = data.storage.as_numeric_sensor_mut(); + let prev_state = storage.state.replace(NumericSensorState { + value: temperature, + timestamp: embassy_time::Instant::now(), + }); + match prev_state { + Some(state) => state.value != temperature, + None => true, + } + }); + if publish { + self.0.queue_publish(); + } } } -- cgit