diff options
| author | diogo464 <[email protected]> | 2025-12-06 14:31:54 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-06 14:31:54 +0000 |
| commit | e5416e848c8caeed59e8aca6fb3e191fbc621b1b (patch) | |
| tree | bd086e6d96f7acf2e051d29c5ce2fee3be4ea5f5 /src/lib.rs | |
| parent | 0fbce715a32f5e05c2a66e5cc401105f72083ff5 (diff) | |
Refactor sensor API to support generic sensor types with configurable display precision
Replaced TemperatureSensor with a generic Sensor type that accepts sensor class, state class, unit, and display precision to support various sensor types. Added suggested_display_precision field to control decimal places in Home Assistant UI.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 12 |
1 files changed, 8 insertions, 4 deletions
| @@ -115,6 +115,9 @@ struct EntityDiscovery<'a> { | |||
| 115 | #[serde(skip_serializing_if = "Option::is_none")] | 115 | #[serde(skip_serializing_if = "Option::is_none")] |
| 116 | mode: Option<&'a str>, | 116 | mode: Option<&'a str>, |
| 117 | 117 | ||
| 118 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 119 | suggested_display_precision: Option<u8>, | ||
| 120 | |||
| 118 | device: &'a DeviceDiscovery<'a>, | 121 | device: &'a DeviceDiscovery<'a>, |
| 119 | } | 122 | } |
| 120 | 123 | ||
| @@ -427,11 +430,11 @@ impl<'a> Device<'a> { | |||
| 427 | } | 430 | } |
| 428 | } | 431 | } |
| 429 | 432 | ||
| 430 | pub fn create_temperature_sensor( | 433 | pub fn create_sensor( |
| 431 | &self, | 434 | &self, |
| 432 | id: &'static str, | 435 | id: &'static str, |
| 433 | config: TemperatureSensorConfig, | 436 | config: SensorConfig, |
| 434 | ) -> TemperatureSensor<'a> { | 437 | ) -> Sensor<'a> { |
| 435 | let mut entity_config = EntityConfig::default(); | 438 | let mut entity_config = EntityConfig::default(); |
| 436 | entity_config.id = id; | 439 | entity_config.id = id; |
| 437 | config.populate(&mut entity_config); | 440 | config.populate(&mut entity_config); |
| @@ -440,7 +443,7 @@ impl<'a> Device<'a> { | |||
| 440 | entity_config, | 443 | entity_config, |
| 441 | EntityStorage::NumericSensor(Default::default()), | 444 | EntityStorage::NumericSensor(Default::default()), |
| 442 | ); | 445 | ); |
| 443 | TemperatureSensor::new(entity) | 446 | Sensor::new(entity) |
| 444 | } | 447 | } |
| 445 | 448 | ||
| 446 | pub fn create_button(&self, id: &'static str, config: ButtonConfig) -> Button<'a> { | 449 | pub fn create_button(&self, id: &'static str, config: ButtonConfig) -> Button<'a> { |
| @@ -573,6 +576,7 @@ impl<'a> Device<'a> { | |||
| 573 | max: entity_config.max, | 576 | max: entity_config.max, |
| 574 | step: entity_config.step, | 577 | step: entity_config.step, |
| 575 | mode: entity_config.mode, | 578 | mode: entity_config.mode, |
| 579 | suggested_display_precision: entity_config.suggested_display_precision, | ||
| 576 | device: &device_discovery, | 580 | device: &device_discovery, |
| 577 | }; | 581 | }; |
| 578 | crate::log::debug!( | 582 | crate::log::debug!( |
