diff options
| author | diogo464 <[email protected]> | 2025-12-12 12:05:58 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-12 12:05:58 +0000 |
| commit | e041614b0607a0950be1446f595d85c93b501418 (patch) | |
| tree | 54a69e34ad53bf7c8a844dba0f6585d52f31d2f4 /src/entity_device_tracker.rs | |
| parent | feb5ae59654bb11365b939ee871adb9760229355 (diff) | |
added device tracker entity
Diffstat (limited to 'src/entity_device_tracker.rs')
| -rw-r--r-- | src/entity_device_tracker.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/entity_device_tracker.rs b/src/entity_device_tracker.rs new file mode 100644 index 0000000..6c82d6a --- /dev/null +++ b/src/entity_device_tracker.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | use crate::{DeviceTrackerState, Entity, EntityCommonConfig, EntityConfig, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default)] | ||
| 4 | pub struct DeviceTrackerConfig { | ||
| 5 | pub common: EntityCommonConfig, | ||
| 6 | } | ||
| 7 | |||
| 8 | impl 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)] | ||
| 17 | pub struct DeviceTrackerLocation { | ||
| 18 | pub latitude: f32, | ||
| 19 | pub longitude: f32, | ||
| 20 | pub accuracy: Option<f32>, | ||
| 21 | } | ||
| 22 | |||
| 23 | pub struct DeviceTracker<'a>(Entity<'a>); | ||
| 24 | |||
| 25 | impl<'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 | } | ||
