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

#[derive(Debug, Default)]
pub struct TemperatureSensorConfig {
    pub common: EntityCommonConfig,
    pub unit: TemperatureUnit,
}

impl TemperatureSensorConfig {
    pub(crate) fn populate(&self, config: &mut EntityConfig) {
        self.common.populate(config);
        config.domain = constants::HA_DOMAIN_SENSOR;
        config.device_class = Some(constants::HA_DEVICE_CLASS_SENSOR_TEMPERATURE);
        config.measurement_unit = Some(self.unit.as_str());
    }
}

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

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

    pub fn publish(&mut self, temperature: f32) {
        let publish = self.0.with_data(|data| {
            let storage = data.storage.as_numeric_sensor_mut();
            let prev_state = storage.state.replace(NumericSensorState {
                value: temperature,
                timestamp: embassy_time::Instant::now(),
            });
            match prev_state {
                Some(state) => state.value != temperature,
                None => true,
            }
        });
        if publish {
            self.0.queue_publish();
        }
    }
}