diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index 525a99b86c21..99aae56b18cd 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -208,7 +208,7 @@ class NumberDeviceClass(StrEnum): Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, - `Mcal`, `Gcal` + `Mcal`, `Gcal`, `thm` """ ENERGY_DISTANCE = "energy_distance" @@ -231,7 +231,7 @@ class NumberDeviceClass(StrEnum): Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, - `Mcal`, `Gcal` + `Mcal`, `Gcal`, `thm` """ FREQUENCY = "frequency" diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 9f396e99d4fc..72d306728742 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -240,7 +240,7 @@ class SensorDeviceClass(StrEnum): electric energy consumption. Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, - `Mcal`, `Gcal` + `Mcal`, `Gcal`, `thm` """ ENERGY_DISTANCE = "energy_distance" @@ -262,7 +262,7 @@ class SensorDeviceClass(StrEnum): Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, - `Mcal`, `Gcal` + `Mcal`, `Gcal`, `thm` """ FREQUENCY = "frequency" diff --git a/homeassistant/const.py b/homeassistant/const.py index 6379cc6a5a62..17eae8bcb397 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -533,6 +533,7 @@ class UnitOfEnergy(StrEnum): KILO_CALORIE = "kcal" MEGA_CALORIE = "Mcal" GIGA_CALORIE = "Gcal" + THERM = "thm" # Reactive energy units diff --git a/homeassistant/generated/sensor.json b/homeassistant/generated/sensor.json index 836e8b624f5f..8b7327e7b917 100644 --- a/homeassistant/generated/sensor.json +++ b/homeassistant/generated/sensor.json @@ -123,6 +123,7 @@ "MJ", "MWh", "mWh", + "thm", "TWh", "Wh" ], @@ -145,6 +146,7 @@ "MJ", "MWh", "mWh", + "thm", "TWh", "Wh" ], @@ -465,6 +467,7 @@ "MJ", "MWh", "mWh", + "thm", "TWh", "Wh" ], @@ -487,6 +490,7 @@ "MJ", "MWh", "mWh", + "thm", "TWh", "Wh" ], diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 13c342860980..e4981acda037 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -72,6 +72,7 @@ _DAYS_TO_SECS = _DAYS_TO_HRS * _HRS_TO_SECS # 1 day = 24 hours = 86400 seconds # Energy conversion constants _WH_TO_J = 3600 # 1 Wh = 3600 J _WH_TO_CAL = _WH_TO_J / 4.184 # 1 Wh = 860.42065 cal +_WH_TO_THERM_US = _WH_TO_J / 1.054804e8 # 1 therm (U.S.) = 1.054804e8 J (NIST SP 811) # Mass conversion constants _POUND_TO_G = 453.59237 @@ -414,6 +415,7 @@ class EnergyConverter(BaseUnitConverter): UnitOfEnergy.KILO_CALORIE: _WH_TO_CAL, UnitOfEnergy.MEGA_CALORIE: _WH_TO_CAL / 1e3, UnitOfEnergy.GIGA_CALORIE: _WH_TO_CAL / 1e6, + UnitOfEnergy.THERM: _WH_TO_THERM_US * 1e3, } VALID_UNITS = set(UnitOfEnergy) diff --git a/tests/components/knx/snapshots/test_websocket.ambr b/tests/components/knx/snapshots/test_websocket.ambr index 07c5794ba60c..f5edfa9f0cf6 100644 --- a/tests/components/knx/snapshots/test_websocket.ambr +++ b/tests/components/knx/snapshots/test_websocket.ambr @@ -1940,6 +1940,7 @@ 'psi', 's', 'st', + 'thm', 'var', 'varh', 'yd', @@ -2318,6 +2319,7 @@ 'psi', 's', 'st', + 'thm', 'var', 'varh', 'yd', diff --git a/tests/components/template/test_config_flow.py b/tests/components/template/test_config_flow.py index 5024ad9ec833..5991a94ef8c7 100644 --- a/tests/components/template/test_config_flow.py +++ b/tests/components/template/test_config_flow.py @@ -1256,7 +1256,7 @@ EARLY_END_ERROR = "invalid template (TemplateSyntaxError: unexpected 'end of tem "'None' is not a valid unit for device class 'energy'; " "expected one of 'cal', 'Gcal', 'GJ', 'GWh', 'J'," " 'kcal', 'kJ', 'kWh', 'Mcal', 'MJ', 'MWh'," - " 'mWh', 'TWh', 'Wh'" + " 'mWh', 'thm', 'TWh', 'Wh'" ), }, ), diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index 09c85a208f95..662b3e72262c 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -692,6 +692,8 @@ _CONVERTED_VALUE: dict[ (10, UnitOfEnergy.MEGA_CALORIE, 0.011622222, UnitOfEnergy.MEGA_WATT_HOUR), (10, UnitOfEnergy.GIGA_CALORIE, 10000, UnitOfEnergy.MEGA_CALORIE), (10, UnitOfEnergy.GIGA_CALORIE, 11.622222, UnitOfEnergy.MEGA_WATT_HOUR), + (10, UnitOfEnergy.KILO_WATT_HOUR, 0.3412956, UnitOfEnergy.THERM), + (10, UnitOfEnergy.THERM, 293.001111, UnitOfEnergy.KILO_WATT_HOUR), ], EnergyDistanceConverter: [ (