aboutsummaryrefslogtreecommitdiff
path: root/src/entity_switch.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/entity_switch.rs')
-rw-r--r--src/entity_switch.rs22
1 files changed, 20 insertions, 2 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)]
12pub struct SwitchConfig { 12pub 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
18impl 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
17impl SwitchConfig { 28impl 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 }