1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
use crate::{
BinaryState, Entity, EntityCommonConfig, EntityConfig, SwitchCommand, SwitchState, constants,
};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum SwitchClass {
#[default]
Generic,
Outlet,
Switch,
}
#[derive(Debug)]
pub struct SwitchConfig {
pub common: EntityCommonConfig,
pub class: SwitchClass,
pub publish_on_command: bool,
}
impl Default for SwitchConfig {
fn default() -> Self {
Self {
common: Default::default(),
class: Default::default(),
publish_on_command: true,
}
}
}
impl SwitchConfig {
pub(crate) fn populate(&self, config: &mut EntityConfig) {
self.common.populate(config);
config.domain = constants::HA_DOMAIN_SWITCH;
config.device_class = match self.class {
SwitchClass::Generic => None,
SwitchClass::Outlet => Some(constants::HA_DEVICE_CLASS_SWITCH_OUTLET),
SwitchClass::Switch => Some(constants::HA_DEVICE_CLASS_SWITCH_SWITCH),
};
}
}
pub struct Switch<'a>(Entity<'a>);
impl<'a> Switch<'a> {
pub(crate) fn new(entity: Entity<'a>) -> Self {
Self(entity)
}
pub fn state(&self) -> Option<BinaryState> {
self.0.with_data(|data| {
let storage = data.storage.as_switch_mut();
storage.state.as_ref().map(|s| s.value)
})
}
pub fn command(&self) -> Option<BinaryState> {
self.0.with_data(|data| {
let storage = data.storage.as_switch_mut();
storage.command.as_ref().map(|s| s.value)
})
}
pub fn toggle(&mut self) -> BinaryState {
let new_state = self.state().unwrap_or(BinaryState::Off).flip();
self.set(new_state);
new_state
}
pub fn set(&mut self, state: BinaryState) {
let publish = self.0.with_data(|data| {
let storage = data.storage.as_switch_mut();
let timestamp = embassy_time::Instant::now();
let publish = match &storage.command {
Some(command) => command.value != state,
None => true,
};
storage.state = Some(SwitchState {
value: state,
timestamp,
});
storage.command = Some(SwitchCommand {
value: state,
timestamp,
});
publish
});
if publish {
self.0.queue_publish();
}
}
pub async fn wait(&mut self) -> BinaryState {
loop {
self.0.wait_command().await;
if let Some(state) = self.command() {
return state;
}
}
}
}
|