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_binary_sensor.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/entity_binary_sensor.rs') diff --git a/src/entity_binary_sensor.rs b/src/entity_binary_sensor.rs index b80f718..ea270f2 100644 --- a/src/entity_binary_sensor.rs +++ b/src/entity_binary_sensor.rs @@ -1,4 +1,4 @@ -use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; +use crate::{BinarySensorState, BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub enum BinarySensorClass { @@ -66,13 +66,28 @@ impl<'a> BinarySensor<'a> { } pub fn set(&mut self, state: BinaryState) { - self.0.publish(state.as_str().as_bytes()); + let publish = self.0.with_data(|data| { + let storage = data.storage.as_binary_sensor_mut(); + let publish = match &storage.state { + Some(s) => s.value != state, + None => true, + }; + storage.state = Some(BinarySensorState { + value: state, + timestamp: embassy_time::Instant::now(), + }); + publish + }); + if publish { + self.0.queue_publish(); + } } pub fn value(&self) -> Option { - self.0 - .with_data(|data| BinaryState::try_from(data.publish_value.as_slice())) - .ok() + self.0.with_data(|data| { + let storage = data.storage.as_binary_sensor_mut(); + storage.state.as_ref().map(|s| s.value) + }) } pub fn toggle(&mut self) -> BinaryState { -- cgit