aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-12-09 23:08:48 +0000
committerdiogo464 <[email protected]>2025-12-09 23:08:48 +0000
commit56a728b44d3523f6139d0b5e1442845bd73b81a2 (patch)
tree6a1b321fd15236a765d4bd8450535f0cd62cc447
parent4687daaf620e0d0951e4657d052eaa713d18fa71 (diff)
added dBm unit for signal strength
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
-rw-r--r--src/constants.rs3
-rw-r--r--src/unit.rs34
2 files changed, 37 insertions, 0 deletions
diff --git a/src/constants.rs b/src/constants.rs
index 30ba4f1..09636ea 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -302,3 +302,6 @@ pub const HA_UNIT_WEIGHT_STONE: &str = "st";
302 302
303// Light 303// Light
304pub const HA_UNIT_LIGHT_LUX: &str = "lx"; 304pub const HA_UNIT_LIGHT_LUX: &str = "lx";
305
306// Signal Strength
307pub const HA_UNIT_SIGNAL_STRENGTH_DBM: &str = "dBm";
diff --git a/src/unit.rs b/src/unit.rs
index 90eecb4..cf3c5a2 100644
--- a/src/unit.rs
+++ b/src/unit.rs
@@ -73,6 +73,19 @@ impl PressureUnit {
73} 73}
74 74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)] 75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum SignalStrengthUnit {
77 Dbm,
78}
79
80impl SignalStrengthUnit {
81 pub fn as_str(&self) -> &'static str {
82 match self {
83 SignalStrengthUnit::Dbm => crate::constants::HA_UNIT_SIGNAL_STRENGTH_DBM,
84 }
85 }
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub enum EnergyUnit { 89pub enum EnergyUnit {
77 KiloWattHour, 90 KiloWattHour,
78} 91}
@@ -442,6 +455,7 @@ pub enum Unit {
442 LightLux, 455 LightLux,
443 PressureHectoPascal, 456 PressureHectoPascal,
444 EnergyKiloWattHour, 457 EnergyKiloWattHour,
458 SignalStrengthDbm,
445 TimeMilliseconds, 459 TimeMilliseconds,
446 TimeSeconds, 460 TimeSeconds,
447 TimeMinutes, 461 TimeMinutes,
@@ -571,6 +585,7 @@ impl Unit {
571 Unit::LightLux => crate::constants::HA_UNIT_LIGHT_LUX, 585 Unit::LightLux => crate::constants::HA_UNIT_LIGHT_LUX,
572 Unit::PressureHectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA, 586 Unit::PressureHectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA,
573 Unit::EnergyKiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH, 587 Unit::EnergyKiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH,
588 Unit::SignalStrengthDbm => crate::constants::HA_UNIT_SIGNAL_STRENGTH_DBM,
574 Unit::TimeMilliseconds => crate::constants::HA_UNIT_TIME_MILLISECONDS, 589 Unit::TimeMilliseconds => crate::constants::HA_UNIT_TIME_MILLISECONDS,
575 Unit::TimeSeconds => crate::constants::HA_UNIT_TIME_SECONDS, 590 Unit::TimeSeconds => crate::constants::HA_UNIT_TIME_SECONDS,
576 Unit::TimeMinutes => crate::constants::HA_UNIT_TIME_MINUTES, 591 Unit::TimeMinutes => crate::constants::HA_UNIT_TIME_MINUTES,
@@ -750,6 +765,14 @@ impl From<PressureUnit> for Unit {
750 } 765 }
751} 766}
752 767
768impl From<SignalStrengthUnit> for Unit {
769 fn from(unit: SignalStrengthUnit) -> Self {
770 match unit {
771 SignalStrengthUnit::Dbm => Unit::SignalStrengthDbm,
772 }
773 }
774}
775
753impl From<EnergyUnit> for Unit { 776impl From<EnergyUnit> for Unit {
754 fn from(unit: EnergyUnit) -> Self { 777 fn from(unit: EnergyUnit) -> Self {
755 match unit { 778 match unit {
@@ -986,6 +1009,17 @@ impl TryFrom<Unit> for PressureUnit {
986 } 1009 }
987} 1010}
988 1011
1012impl TryFrom<Unit> for SignalStrengthUnit {
1013 type Error = UnitConversionError;
1014
1015 fn try_from(unit: Unit) -> Result<Self, Self::Error> {
1016 match unit {
1017 Unit::SignalStrengthDbm => Ok(SignalStrengthUnit::Dbm),
1018 _ => Err(UnitConversionError),
1019 }
1020 }
1021}
1022
989impl TryFrom<Unit> for EnergyUnit { 1023impl TryFrom<Unit> for EnergyUnit {
990 type Error = UnitConversionError; 1024 type Error = UnitConversionError;
991 1025