Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teach UnitlessRatioConverter about ppm and ppb #92989

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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