diff options
Diffstat (limited to 'src/entity_sensor.rs')
| -rw-r--r-- | src/entity_sensor.rs | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/entity_sensor.rs b/src/entity_sensor.rs index 702c9de..1a99754 100644 --- a/src/entity_sensor.rs +++ b/src/entity_sensor.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | use crate::{ | 1 | use crate::{ |
| 2 | Entity, EntityCommonConfig, EntityConfig, NumericSensorState, TemperatureUnit, constants, | 2 | Entity, EntityCommonConfig, EntityConfig, NumericSensorState, constants, |
| 3 | }; | 3 | }; |
| 4 | 4 | ||
| 5 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | 5 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| @@ -144,36 +144,41 @@ impl SensorClass { | |||
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | #[derive(Debug, Default)] | 146 | #[derive(Debug, Default)] |
| 147 | pub struct TemperatureSensorConfig { | 147 | pub struct SensorConfig { |
| 148 | pub common: EntityCommonConfig, | 148 | pub common: EntityCommonConfig, |
| 149 | pub unit: TemperatureUnit, | 149 | pub class: SensorClass, |
| 150 | pub state_class: StateClass, | ||
| 151 | pub unit: Option<&'static str>, | ||
| 152 | pub suggested_display_precision: Option<u8>, | ||
| 150 | } | 153 | } |
| 151 | 154 | ||
| 152 | impl TemperatureSensorConfig { | 155 | impl SensorConfig { |
| 153 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | 156 | pub(crate) fn populate(&self, config: &mut EntityConfig) { |
| 154 | self.common.populate(config); | 157 | self.common.populate(config); |
| 155 | config.domain = constants::HA_DOMAIN_SENSOR; | 158 | config.domain = constants::HA_DOMAIN_SENSOR; |
| 156 | config.device_class = Some(constants::HA_DEVICE_CLASS_SENSOR_TEMPERATURE); | 159 | config.device_class = self.class.as_str(); |
| 157 | config.measurement_unit = Some(self.unit.as_str()); | 160 | config.state_class = Some(self.state_class.as_str()); |
| 161 | config.measurement_unit = self.unit; | ||
| 162 | config.suggested_display_precision = self.suggested_display_precision; | ||
| 158 | } | 163 | } |
| 159 | } | 164 | } |
| 160 | 165 | ||
| 161 | pub struct TemperatureSensor<'a>(Entity<'a>); | 166 | pub struct Sensor<'a>(Entity<'a>); |
| 162 | 167 | ||
| 163 | impl<'a> TemperatureSensor<'a> { | 168 | impl<'a> Sensor<'a> { |
| 164 | pub(crate) fn new(entity: Entity<'a>) -> Self { | 169 | pub(crate) fn new(entity: Entity<'a>) -> Self { |
| 165 | Self(entity) | 170 | Self(entity) |
| 166 | } | 171 | } |
| 167 | 172 | ||
| 168 | pub fn publish(&mut self, temperature: f32) { | 173 | pub fn publish(&mut self, value: f32) { |
| 169 | let publish = self.0.with_data(|data| { | 174 | let publish = self.0.with_data(|data| { |
| 170 | let storage = data.storage.as_numeric_sensor_mut(); | 175 | let storage = data.storage.as_numeric_sensor_mut(); |
| 171 | let prev_state = storage.state.replace(NumericSensorState { | 176 | let prev_state = storage.state.replace(NumericSensorState { |
| 172 | value: temperature, | 177 | value, |
| 173 | timestamp: embassy_time::Instant::now(), | 178 | timestamp: embassy_time::Instant::now(), |
| 174 | }); | 179 | }); |
| 175 | match prev_state { | 180 | match prev_state { |
| 176 | Some(state) => state.value != temperature, | 181 | Some(state) => state.value != value, |
| 177 | None => true, | 182 | None => true, |
| 178 | } | 183 | } |
| 179 | }); | 184 | }); |
