Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b25622f

Browse files
authored
Use SI constants in CO unit converter (home-assistant#153187)
1 parent e887d5e commit b25622f

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

‎homeassistant/util/unit_conversion.py‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@
9191
_FLUID_OUNCE_TO_CUBIC_METER = _GALLON_TO_CUBIC_METER / 128 # 128 fl. oz. in a US gallon
9292
_CUBIC_FOOT_TO_CUBIC_METER = pow(_FOOT_TO_M, 3)
9393

94+
# Gas concentration conversion constants
95+
_IDEAL_GAS_CONSTANT = 8.31446261815324 # m3⋅Pa⋅K−1⋅mol−1
96+
# Ambient constants based on European Commission recommendations (20 °C and 1013mb)
97+
_AMBIENT_TEMPERATURE = 293.15 # K (20 °C)
98+
_AMBIENT_PRESSURE = 101325 # Pa (1 atm)
99+
_AMBIENT_IDEAL_GAS_MOLAR_VOLUME = ( # m3⋅mol−1
100+
_IDEAL_GAS_CONSTANT * _AMBIENT_TEMPERATURE / _AMBIENT_PRESSURE
101+
)
102+
# Molar masses in g⋅mol−1
103+
_CARBON_MONOXIDE_MOLAR_MASS = 28.01
104+
94105

95106
class BaseUnitConverter:
96107
"""Define the format of a conversion utility."""
@@ -169,14 +180,17 @@ def _are_unit_inverses(cls, from_unit: str | None, to_unit: str | None) -> bool:
169180

170181

171182
class CarbonMonoxideConcentrationConverter(BaseUnitConverter):
172-
"""Convert carbon monoxide ratio to mass per volume."""
183+
"""Convert carbon monoxide ratio to mass per volume.
184+
185+
Using ambient temperature of 20°C and pressure of 1 ATM.
186+
"""
173187

174188
UNIT_CLASS = "carbon_monoxide"
175189
_UNIT_CONVERSION: dict[str | None, float] = {
176-
CONCENTRATION_PARTS_PER_MILLION: 1,
177-
# concentration (mg/m3) = 0.0409 x concentration (ppm) x molar mass
178-
# Carbon monoxide molar mass: 28.01 g/mol
179-
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: 0.0409*28.01,
190+
CONCENTRATION_PARTS_PER_MILLION: 1e6,
191+
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: (
192+
_CARBON_MONOXIDE_MOLAR_MASS/_AMBIENT_IDEAL_GAS_MOLAR_VOLUME*1e3
193+
),
180194
}
181195
VALID_UNITS = {
182196
CONCENTRATION_PARTS_PER_MILLION,

‎tests/components/sensor/test_recorder.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3626,7 +3626,7 @@ async def test_compile_hourly_statistics_changing_units_3(
36263626
13.050847,
36273627
-10,
36283628
30,
3629-
1.145609,
3629+
1.16441,
36303630
),
36313631
(
36323632
"carbon_monoxide",
@@ -3636,7 +3636,7 @@ async def test_compile_hourly_statistics_changing_units_3(
36363636
13.050847,
36373637
-10,
36383638
30,
3639-
1 / 1.145609,
3639+
1 / 1.16441,
36403640
),
36413641
(None, "%", None, "unitless", 13.050847, -10, 30, 0.01),
36423642
(None, "W", "kW", "power", 13.050847, -10, 30, 0.001),

‎tests/util/test_unit_conversion.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
CarbonMonoxideConcentrationConverter: (
120120
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
121121
CONCENTRATION_PARTS_PER_MILLION,
122-
1.145609,
122+
1.16441,
123123
),
124124
ConductivityConverter: (
125125
UnitOfConductivity.MICROSIEMENS_PER_CM,
@@ -291,13 +291,13 @@
291291
(
292292
1,
293293
CONCENTRATION_PARTS_PER_MILLION,
294-
1.145609,
294+
1.16441,
295295
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
296296
),
297297
(
298298
120,
299299
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
300-
104.74778,
300+
103.05655,
301301
CONCENTRATION_PARTS_PER_MILLION,
302302
),
303303
],

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /