diff options
| author | diogo464 <[email protected]> | 2025-12-05 12:17:01 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-05 12:17:01 +0000 |
| commit | 0c86da392af50c7588b087c3f72602e8368af65e (patch) | |
| tree | 894cd2f353298b83a56cde06eafd7b1e366aa6b3 /src/entity_sensor.rs | |
| parent | 1d2ee64d0ec917a2c2b66f8d58e1f37dd174a89d (diff) | |
reworked entity creation
Diffstat (limited to 'src/entity_sensor.rs')
| -rw-r--r-- | src/entity_sensor.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/entity_sensor.rs b/src/entity_sensor.rs new file mode 100644 index 0000000..5d7794f --- /dev/null +++ b/src/entity_sensor.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, TemperatureUnit, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default)] | ||
| 4 | pub struct TemperatureSensorConfig { | ||
| 5 | pub common: EntityCommonConfig, | ||
| 6 | pub unit: TemperatureUnit, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl TemperatureSensorConfig { | ||
| 10 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 11 | self.common.populate(config); | ||
| 12 | config.domain = constants::HA_DOMAIN_SENSOR; | ||
| 13 | config.device_class = Some(constants::HA_DEVICE_CLASS_SENSOR_TEMPERATURE); | ||
| 14 | config.measurement_unit = Some(self.unit.as_str()); | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | pub struct TemperatureSensor<'a>(Entity<'a>); | ||
| 19 | |||
| 20 | impl<'a> TemperatureSensor<'a> { | ||
| 21 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 22 | Self(entity) | ||
| 23 | } | ||
| 24 | |||
| 25 | pub fn publish(&mut self, temperature: f32) { | ||
| 26 | use core::fmt::Write; | ||
| 27 | self.0 | ||
| 28 | .publish_with(|view| write!(view, "{}", temperature).unwrap()); | ||
| 29 | } | ||
| 30 | } | ||
