aboutsummaryrefslogtreecommitdiff
path: root/src/entity_button.rs
blob: 35f787f06f2e6c9685f8df1a781234cf71fb9ea9 (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
use crate::{Entity, EntityCommonConfig, EntityConfig, constants};

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum ButtonClass {
    #[default]
    Generic,
    Identify,
    Restart,
    Update,
}

#[derive(Debug, Default)]
pub struct ButtonConfig {
    pub common: EntityCommonConfig,
    pub class: ButtonClass,
}

impl ButtonConfig {
    pub(crate) fn populate(&self, config: &mut EntityConfig) {
        self.common.populate(config);
        config.domain = constants::HA_DOMAIN_BUTTON;
        config.device_class = match self.class {
            ButtonClass::Generic => None,
            ButtonClass::Identify => Some(constants::HA_DEVICE_CLASS_BUTTON_IDENTIFY),
            ButtonClass::Restart => Some(constants::HA_DEVICE_CLASS_BUTTON_RESTART),
            ButtonClass::Update => Some(constants::HA_DEVICE_CLASS_BUTTON_UPDATE),
        };
    }
}

pub struct Button<'a>(Entity<'a>);

impl<'a> Button<'a> {
    pub(crate) fn new(entity: Entity<'a>) -> Self {
        Self(entity)
    }

    pub async fn pressed(&mut self) {
        loop {
            self.0.wait_command().await;
            let pressed = self.0.with_data(|data| {
                let storage = data.storage.as_button_mut();
                if !storage.consumed && storage.timestamp.is_some() {
                    storage.consumed = true;
                    true
                } else {
                    false
                }
            });

            if pressed {
                break;
            }
        }
    }
}