aboutsummaryrefslogtreecommitdiff
path: root/src/entity_switch.rs
blob: 2b799a14c43dcdb6df26787eb02c6db58132e8a3 (plain)
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
use crate::{
    BinaryState, CommandPolicy, Entity, EntityCommonConfig, EntityConfig, SwitchCommand,
    SwitchState, constants,
};

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum SwitchClass {
    #[default]
    Generic,
    Outlet,
    Switch,
}

/// Configuration for a switch entity.
///
/// See [`CommandPolicy`] for details on how commands are handled.
#[derive(Debug)]
#[derive(Default)]
pub struct SwitchConfig {
    pub common: EntityCommonConfig,
    pub class: SwitchClass,
    pub command_policy: CommandPolicy,
}


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;
            }
        }
    }
}