diff options
Diffstat (limited to 'src/entity_switch.rs')
| -rw-r--r-- | src/entity_switch.rs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/entity_switch.rs b/src/entity_switch.rs index 4d2efdb..0277288 100644 --- a/src/entity_switch.rs +++ b/src/entity_switch.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; | 1 | use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, SwitchState, constants}; |
| 2 | 2 | ||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 4 | pub enum SwitchClass { | 4 | pub enum SwitchClass { |
| @@ -34,8 +34,10 @@ impl<'a> Switch<'a> { | |||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | pub fn state(&self) -> Option<BinaryState> { | 36 | pub fn state(&self) -> Option<BinaryState> { |
| 37 | self.0 | 37 | self.0.with_data(|data| { |
| 38 | .with_data(|data| BinaryState::try_from(data.command_value.as_slice()).ok()) | 38 | let storage = data.storage.as_switch_mut(); |
| 39 | storage.state.as_ref().map(|s| s.value) | ||
| 40 | }) | ||
| 39 | } | 41 | } |
| 40 | 42 | ||
| 41 | pub fn toggle(&mut self) -> BinaryState { | 43 | pub fn toggle(&mut self) -> BinaryState { |
| @@ -45,7 +47,13 @@ impl<'a> Switch<'a> { | |||
| 45 | } | 47 | } |
| 46 | 48 | ||
| 47 | pub fn set(&mut self, state: BinaryState) { | 49 | pub fn set(&mut self, state: BinaryState) { |
| 48 | self.0.publish(state.as_str().as_bytes()); | 50 | self.0.with_data(|data| { |
| 51 | let storage = data.storage.as_switch_mut(); | ||
| 52 | storage.state = Some(SwitchState { | ||
| 53 | value: state, | ||
| 54 | timestamp: embassy_time::Instant::now(), | ||
| 55 | }); | ||
| 56 | }) | ||
| 49 | } | 57 | } |
| 50 | 58 | ||
| 51 | pub async fn wait(&mut self) -> BinaryState { | 59 | pub async fn wait(&mut self) -> BinaryState { |
