embassy_ha/
entity_device_tracker.rs

1use crate::{DeviceTrackerState, Entity, EntityCommonConfig, EntityConfig, constants};
2
3#[derive(Debug, Default)]
4pub struct DeviceTrackerConfig {
5    pub common: EntityCommonConfig,
6}
7
8impl DeviceTrackerConfig {
9    pub(crate) fn populate(&self, config: &mut EntityConfig) {
10        self.common.populate(config);
11        config.domain = constants::HA_DOMAIN_DEVICE_TRACKER;
12        config.platform = Some(constants::HA_DOMAIN_DEVICE_TRACKER);
13    }
14}
15
16#[derive(Debug, Clone, Copy)]
17pub struct DeviceTrackerLocation {
18    pub latitude: f32,
19    pub longitude: f32,
20    pub accuracy: Option<f32>,
21}
22
23pub struct DeviceTracker<'a>(Entity<'a>);
24
25impl<'a> DeviceTracker<'a> {
26    pub(crate) fn new(entity: Entity<'a>) -> Self {
27        Self(entity)
28    }
29
30    pub fn publish(&mut self, location: DeviceTrackerLocation) {
31        self.0.with_data(|data| {
32            let storage = data.storage.as_device_tracker_mut();
33            storage.state = Some(DeviceTrackerState {
34                latitude: location.latitude,
35                longitude: location.longitude,
36                gps_accuracy: location.accuracy,
37            });
38        });
39        self.0.queue_publish();
40    }
41}