From 56a728b44d3523f6139d0b5e1442845bd73b81a2 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 9 Dec 2025 23:08:48 +0000 Subject: added dBm unit for signal strength MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/constants.rs | 3 +++ src/unit.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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"; // Light pub const HA_UNIT_LIGHT_LUX: &str = "lx"; + +// Signal Strength +pub 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 @@ -72,6 +72,19 @@ impl PressureUnit { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SignalStrengthUnit { + Dbm, +} + +impl SignalStrengthUnit { + pub fn as_str(&self) -> &'static str { + match self { + SignalStrengthUnit::Dbm => crate::constants::HA_UNIT_SIGNAL_STRENGTH_DBM, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum EnergyUnit { KiloWattHour, @@ -442,6 +455,7 @@ pub enum Unit { LightLux, PressureHectoPascal, EnergyKiloWattHour, + SignalStrengthDbm, TimeMilliseconds, TimeSeconds, TimeMinutes, @@ -571,6 +585,7 @@ impl Unit { Unit::LightLux => crate::constants::HA_UNIT_LIGHT_LUX, Unit::PressureHectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA, Unit::EnergyKiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH, + Unit::SignalStrengthDbm => crate::constants::HA_UNIT_SIGNAL_STRENGTH_DBM, Unit::TimeMilliseconds => crate::constants::HA_UNIT_TIME_MILLISECONDS, Unit::TimeSeconds => crate::constants::HA_UNIT_TIME_SECONDS, Unit::TimeMinutes => crate::constants::HA_UNIT_TIME_MINUTES, @@ -750,6 +765,14 @@ impl From for Unit { } } +impl From for Unit { + fn from(unit: SignalStrengthUnit) -> Self { + match unit { + SignalStrengthUnit::Dbm => Unit::SignalStrengthDbm, + } + } +} + impl From for Unit { fn from(unit: EnergyUnit) -> Self { match unit { @@ -986,6 +1009,17 @@ impl TryFrom for PressureUnit { } } +impl TryFrom for SignalStrengthUnit { + type Error = UnitConversionError; + + fn try_from(unit: Unit) -> Result { + match unit { + Unit::SignalStrengthDbm => Ok(SignalStrengthUnit::Dbm), + _ => Err(UnitConversionError), + } + } +} + impl TryFrom for EnergyUnit { type Error = UnitConversionError; -- cgit