Skip to content

Commit

Permalink
Teach UnitlessRatioConverter about ppm and ppb (#92989)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed May 12, 2023
1 parent 7807df4 commit 9a70f47
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.PRESSURE: PressureConverter,
SensorDeviceClass.SPEED: SpeedConverter,
SensorDeviceClass.TEMPERATURE: TemperatureConverter,
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS: UnitlessRatioConverter,
SensorDeviceClass.VOLTAGE: ElectricPotentialConverter,
SensorDeviceClass.VOLUME: VolumeConverter,
SensorDeviceClass.VOLUME_STORAGE: VolumeConverter,
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from __future__ import annotations

from homeassistant.const import (
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
UNIT_NOT_RECOGNIZED_TEMPLATE,
UnitOfDataRate,
Expand Down Expand Up @@ -421,6 +423,8 @@ class UnitlessRatioConverter(BaseUnitConverter):
NORMALIZED_UNIT = None
_UNIT_CONVERSION: dict[str | None, float] = {
None: 1,
CONCENTRATION_PARTS_PER_BILLION: 1000000000,
CONCENTRATION_PARTS_PER_MILLION: 1000000,
PERCENTAGE: 100,
}
VALID_UNITS = {
Expand Down
4 changes: 4 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import pytest

from homeassistant.const import (
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
UnitOfDataRate,
UnitOfElectricCurrent,
Expand Down Expand Up @@ -365,6 +367,8 @@
],
UnitlessRatioConverter: [
(5, None, 500, PERCENTAGE),
(5, None, 5000000000, CONCENTRATION_PARTS_PER_BILLION),
(5, None, 5000000, CONCENTRATION_PARTS_PER_MILLION),
(5, PERCENTAGE, 0.05, None),
],
VolumeConverter: [
Expand Down

0 comments on commit 9a70f47

Please sign in to comment.