diff options
Diffstat (limited to 'src/entity_binary_sensor.rs')
| -rw-r--r-- | src/entity_binary_sensor.rs | 25 |
1 files changed, 20 insertions, 5 deletions
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 @@ | |||
| 1 | use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; | 1 | use crate::{BinarySensorState, BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; |
| 2 | 2 | ||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 4 | pub enum BinarySensorClass { | 4 | pub enum BinarySensorClass { |
| @@ -66,13 +66,28 @@ impl<'a> BinarySensor<'a> { | |||
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | pub fn set(&mut self, state: BinaryState) { | 68 | pub fn set(&mut self, state: BinaryState) { |
| 69 | self.0.publish(state.as_str().as_bytes()); | 69 | let publish = self.0.with_data(|data| { |
| 70 | let storage = data.storage.as_binary_sensor_mut(); | ||
| 71 | let publish = match &storage.state { | ||
| 72 | Some(s) => s.value != state, | ||
| 73 | None => true, | ||
| 74 | }; | ||
| 75 | storage.state = Some(BinarySensorState { | ||
| 76 | value: state, | ||
| 77 | timestamp: embassy_time::Instant::now(), | ||
| 78 | }); | ||
| 79 | publish | ||
| 80 | }); | ||
| 81 | if publish { | ||
| 82 | self.0.queue_publish(); | ||
| 83 | } | ||
| 70 | } | 84 | } |
| 71 | 85 | ||
| 72 | pub fn value(&self) -> Option<BinaryState> { | 86 | pub fn value(&self) -> Option<BinaryState> { |
| 73 | self.0 | 87 | self.0.with_data(|data| { |
| 74 | .with_data(|data| BinaryState::try_from(data.publish_value.as_slice())) | 88 | let storage = data.storage.as_binary_sensor_mut(); |
| 75 | .ok() | 89 | storage.state.as_ref().map(|s| s.value) |
| 90 | }) | ||
| 76 | } | 91 | } |
| 77 | 92 | ||
| 78 | pub fn toggle(&mut self) -> BinaryState { | 93 | pub fn toggle(&mut self) -> BinaryState { |
