diff options
| author | diogo464 <[email protected]> | 2025-12-05 19:29:44 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-05 19:29:44 +0000 |
| commit | b609a315e7921dcc712da6955890f4dc7c2c4b9f (patch) | |
| tree | 1f2b120b1beaf36c91fb74aba876f1812313307a /src | |
| parent | 52705a80a5012aa48cec9ddf61898596e96c9dfd (diff) | |
added publish_on_command option to switch
Diffstat (limited to 'src')
| -rw-r--r-- | src/entity_switch.rs | 22 | ||||
| -rw-r--r-- | src/lib.rs | 9 |
2 files changed, 28 insertions, 3 deletions
diff --git a/src/entity_switch.rs b/src/entity_switch.rs index 0277288..4bf77eb 100644 --- a/src/entity_switch.rs +++ b/src/entity_switch.rs | |||
| @@ -8,10 +8,21 @@ pub enum SwitchClass { | |||
| 8 | Switch, | 8 | Switch, |
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | #[derive(Debug, Default)] | 11 | #[derive(Debug)] |
| 12 | pub struct SwitchConfig { | 12 | pub struct SwitchConfig { |
| 13 | pub common: EntityCommonConfig, | 13 | pub common: EntityCommonConfig, |
| 14 | pub class: SwitchClass, | 14 | pub class: SwitchClass, |
| 15 | pub publish_on_command: bool, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl Default for SwitchConfig { | ||
| 19 | fn default() -> Self { | ||
| 20 | Self { | ||
| 21 | common: Default::default(), | ||
| 22 | class: Default::default(), | ||
| 23 | publish_on_command: true, | ||
| 24 | } | ||
| 25 | } | ||
| 15 | } | 26 | } |
| 16 | 27 | ||
| 17 | impl SwitchConfig { | 28 | impl SwitchConfig { |
| @@ -40,6 +51,13 @@ impl<'a> Switch<'a> { | |||
| 40 | }) | 51 | }) |
| 41 | } | 52 | } |
| 42 | 53 | ||
| 54 | pub fn command(&self) -> Option<BinaryState> { | ||
| 55 | self.0.with_data(|data| { | ||
| 56 | let storage = data.storage.as_switch_mut(); | ||
| 57 | storage.command.as_ref().map(|s| s.value) | ||
| 58 | }) | ||
| 59 | } | ||
| 60 | |||
| 43 | pub fn toggle(&mut self) -> BinaryState { | 61 | pub fn toggle(&mut self) -> BinaryState { |
| 44 | let new_state = self.state().unwrap_or(BinaryState::Off).flip(); | 62 | let new_state = self.state().unwrap_or(BinaryState::Off).flip(); |
| 45 | self.set(new_state); | 63 | self.set(new_state); |
| @@ -59,7 +77,7 @@ impl<'a> Switch<'a> { | |||
| 59 | pub async fn wait(&mut self) -> BinaryState { | 77 | pub async fn wait(&mut self) -> BinaryState { |
| 60 | loop { | 78 | loop { |
| 61 | self.0.wait_command().await; | 79 | self.0.wait_command().await; |
| 62 | if let Some(state) = self.state() { | 80 | if let Some(state) = self.command() { |
| 63 | return state; | 81 | return state; |
| 64 | } | 82 | } |
| 65 | } | 83 | } |
| @@ -219,6 +219,7 @@ pub struct SwitchState { | |||
| 219 | pub struct SwitchStorage { | 219 | pub struct SwitchStorage { |
| 220 | pub state: Option<SwitchState>, | 220 | pub state: Option<SwitchState>, |
| 221 | pub command: Option<SwitchCommand>, | 221 | pub command: Option<SwitchCommand>, |
| 222 | pub publish_on_command: bool, | ||
| 222 | } | 223 | } |
| 223 | 224 | ||
| 224 | #[derive(Debug)] | 225 | #[derive(Debug)] |
| @@ -460,7 +461,13 @@ impl<'a> Device<'a> { | |||
| 460 | entity_config.id = id; | 461 | entity_config.id = id; |
| 461 | config.populate(&mut entity_config); | 462 | config.populate(&mut entity_config); |
| 462 | 463 | ||
| 463 | let entity = self.create_entity(entity_config, EntityStorage::Switch(Default::default())); | 464 | let entity = self.create_entity( |
| 465 | entity_config, | ||
| 466 | EntityStorage::Switch(SwitchStorage { | ||
| 467 | publish_on_command: config.publish_on_command, | ||
| 468 | ..Default::default() | ||
| 469 | }), | ||
| 470 | ); | ||
| 464 | Switch::new(entity) | 471 | Switch::new(entity) |
| 465 | } | 472 | } |
| 466 | 473 | ||
