Add therm (thm) as a native UnitOfEnergy unit (#179953)

Co-authored-by: Markus Tuominen <3738613+Markus98@users.noreply.github.com>
This commit is contained in:
Colin Haven
2026-09-16 12:44:35 +03:00
committed by GitHub
co-authored by Markus Tuominen
parent d68c9f55e2
commit d5aca4c291
8 changed files with 16 additions and 5 deletions
+2 -2
View File
@@ -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"
+2 -2
View File
@@ -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"
+1
View File
@@ -533,6 +533,7 @@ class UnitOfEnergy(StrEnum):
KILO_CALORIE = "kcal"
MEGA_CALORIE = "Mcal"
GIGA_CALORIE = "Gcal"
THERM = "thm"
# Reactive energy units
+4
View File
@@ -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"
],
+2
View File
@@ -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)
@@ -1940,6 +1940,7 @@
'psi',
's',
'st',
'thm',
'var',
'varh',
'yd',
@@ -2318,6 +2319,7 @@
'psi',
's',
'st',
'thm',
'var',
'varh',
'yd',
@@ -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'"
),
},
),
+2
View File
@@ -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: [
(