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

Add radioactivity concentration units #134376

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
UnitOfPower,
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfRadioactivityConcentration,
UnitOfReactivePower,
UnitOfSoundPressure,
UnitOfSpeed,
Expand All @@ -55,6 +56,7 @@
MassConverter,
PowerConverter,
PressureConverter,
RadioactivityConcentrationConverter,
SpeedConverter,
TemperatureConverter,
UnitlessRatioConverter,
Expand Down Expand Up @@ -337,6 +339,17 @@ class SensorDeviceClass(StrEnum):
- `psi`
"""

RADIOACTIVITY_CONCENTRATION = "radioactivity_concentration"
"""Radioactivity concentration.

Use this device class for sensors measuring the the amount of radioactive decay
in a specific volume of air. For example, sensors measuring radon.

Unit of measurement:
- SI /metric: `Bq/m³`
- USCS / imperial: `pCi/L`
"""

REACTIVE_POWER = "reactive_power"
"""Reactive power.

Expand Down Expand Up @@ -507,6 +520,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.PRECIPITATION: DistanceConverter,
SensorDeviceClass.PRECIPITATION_INTENSITY: SpeedConverter,
SensorDeviceClass.PRESSURE: PressureConverter,
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: RadioactivityConcentrationConverter,
SensorDeviceClass.SPEED: SpeedConverter,
SensorDeviceClass.TEMPERATURE: TemperatureConverter,
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS: UnitlessRatioConverter,
Expand Down Expand Up @@ -571,6 +585,9 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.PRECIPITATION: set(UnitOfPrecipitationDepth),
SensorDeviceClass.PRECIPITATION_INTENSITY: set(UnitOfVolumetricFlux),
SensorDeviceClass.PRESSURE: set(UnitOfPressure),
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: set(
UnitOfRadioactivityConcentration
),
SensorDeviceClass.REACTIVE_POWER: {UnitOfReactivePower.VOLT_AMPERE_REACTIVE},
SensorDeviceClass.SIGNAL_STRENGTH: {
SIGNAL_STRENGTH_DECIBELS,
Expand Down Expand Up @@ -644,6 +661,7 @@ class SensorStateClass(StrEnum):
SensorDeviceClass.PRECIPITATION: set(SensorStateClass),
SensorDeviceClass.PRECIPITATION_INTENSITY: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.PRESSURE: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.REACTIVE_POWER: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.SIGNAL_STRENGTH: {SensorStateClass.MEASUREMENT},
SensorDeviceClass.SOUND_PRESSURE: {SensorStateClass.MEASUREMENT},
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/sensor/device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
CONF_IS_PRECIPITATION_INTENSITY = "is_precipitation_intensity"
CONF_IS_PRESSURE = "is_pressure"
CONF_IS_SPEED = "is_speed"
CONF_IS_RADIOACTIVITY_CONCENTRATION = "is_radioactivity_concentration"
CONF_IS_REACTIVE_POWER = "is_reactive_power"
CONF_IS_SIGNAL_STRENGTH = "is_signal_strength"
CONF_IS_SOUND_PRESSURE = "is_sound_pressure"
Expand Down Expand Up @@ -125,6 +126,9 @@
{CONF_TYPE: CONF_IS_PRECIPITATION_INTENSITY}
],
SensorDeviceClass.PRESSURE: [{CONF_TYPE: CONF_IS_PRESSURE}],
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: [
{CONF_TYPE: CONF_IS_RADIOACTIVITY_CONCENTRATION}
],
SensorDeviceClass.REACTIVE_POWER: [{CONF_TYPE: CONF_IS_REACTIVE_POWER}],
SensorDeviceClass.SIGNAL_STRENGTH: [{CONF_TYPE: CONF_IS_SIGNAL_STRENGTH}],
SensorDeviceClass.SOUND_PRESSURE: [{CONF_TYPE: CONF_IS_SOUND_PRESSURE}],
Expand Down Expand Up @@ -188,6 +192,7 @@
CONF_IS_PRECIPITATION,
CONF_IS_PRECIPITATION_INTENSITY,
CONF_IS_PRESSURE,
CONF_IS_RADIOACTIVITY_CONCENTRATION,
CONF_IS_REACTIVE_POWER,
CONF_IS_SIGNAL_STRENGTH,
CONF_IS_SOUND_PRESSURE,
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/sensor/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"is_precipitation": "Current {entity_name} precipitation",
"is_precipitation_intensity": "Current {entity_name} precipitation intensity",
"is_pressure": "Current {entity_name} pressure",
"is_radioactivity_concentration": "Current {entity_name} radioactivity concentration",
"is_reactive_power": "Current {entity_name} reactive power",
"is_signal_strength": "Current {entity_name} signal strength",
"is_sound_pressure": "Current {entity_name} sound pressure",
Expand Down Expand Up @@ -89,6 +90,7 @@
"precipitation": "{entity_name} precipitation changes",
"precipitation_intensity": "{entity_name} precipitation intensity changes",
"pressure": "{entity_name} pressure changes",
"radioactivity_concentration": "{entity_name} radioactivity concentration changes",
"reactive_power": "{entity_name} reactive power changes",
"signal_strength": "{entity_name} signal strength changes",
"sound_pressure": "{entity_name} sound pressure changes",
Expand Down Expand Up @@ -252,6 +254,9 @@
"reactive_power": {
"name": "Reactive power"
},
"radioactivity_concentration": {
"name": "Radioactivity concentration"
},
"signal_strength": {
"name": "Signal strength"
},
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,14 @@ class UnitOfDataRate(StrEnum):
GIBIBYTES_PER_SECOND = "GiB/s"


# Radioactivity concentration units
class UnitOfRadioactivityConcentration(StrEnum):
"""Radioactivity concentration units."""

BECQUERELS_PER_CUBIC_METER = "Bq/m³"
PICOCURIES_PER_LITER = "pCi/L"


# States
COMPRESSED_STATE_STATE: Final = "s"
COMPRESSED_STATE_ATTRIBUTES: Final = "a"
Expand Down Expand Up @@ -1066,6 +1074,7 @@ class UnitOfDataRate(StrEnum):
WIND_SPEED: Final = "wind_speed"
ILLUMINANCE: Final = "illuminance"
ACCUMULATED_PRECIPITATION: Final = "accumulated_precipitation"
RADIOACTIVITY_CONCENTRATION: Final = "radioactivity_concentration"

WEEKDAYS: Final[list[str]] = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]

Expand Down
19 changes: 19 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
UnitOfMass,
UnitOfPower,
UnitOfPressure,
UnitOfRadioactivityConcentration,
UnitOfSpeed,
UnitOfTemperature,
UnitOfTime,
Expand Down Expand Up @@ -82,6 +83,9 @@
_FLUID_OUNCE_TO_CUBIC_METER = _GALLON_TO_CUBIC_METER / 128 # 128 fl. oz. in a US gallon
_CUBIC_FOOT_TO_CUBIC_METER = pow(_FOOT_TO_M, 3)

# Radioactivity concentration conversion constants
_BQ_PER_CUBIC_METER_TO_PCI_PER_LITER = 2.7027027e-2 # 100 Bq/m³ = 2.7027 pCi/L


class BaseUnitConverter:
"""Define the format of a conversion utility."""
Expand Down Expand Up @@ -703,3 +707,18 @@ class DurationConverter(BaseUnitConverter):
UnitOfTime.DAYS,
UnitOfTime.WEEKS,
}


class RadioactivityConcentrationConverter(BaseUnitConverter):
"""Utility to convert radioactivity concentration values."""

UNIT_CLASS = "radioactivity_concentration"
# units in terms of Bq/m³
_UNIT_CONVERSION: dict[str | None, float] = {
UnitOfRadioactivityConcentration.BECQUERELS_PER_CUBIC_METER: 1,
UnitOfRadioactivityConcentration.PICOCURIES_PER_LITER: _BQ_PER_CUBIC_METER_TO_PCI_PER_LITER,
}
VALID_UNITS = {
UnitOfRadioactivityConcentration.BECQUERELS_PER_CUBIC_METER,
UnitOfRadioactivityConcentration.PICOCURIES_PER_LITER,
}
31 changes: 31 additions & 0 deletions homeassistant/util/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
LENGTH,
MASS,
PRESSURE,
RADIOACTIVITY_CONCENTRATION,
TEMPERATURE,
UNIT_NOT_RECOGNIZED_TEMPLATE,
VOLUME,
Expand All @@ -22,6 +23,7 @@
UnitOfMass,
UnitOfPrecipitationDepth,
UnitOfPressure,
UnitOfRadioactivityConcentration,
UnitOfSpeed,
UnitOfTemperature,
UnitOfVolume,
Expand All @@ -32,6 +34,7 @@
AreaConverter,
DistanceConverter,
PressureConverter,
RadioactivityConcentrationConverter,
SpeedConverter,
TemperatureConverter,
VolumeConverter,
Expand All @@ -57,6 +60,8 @@

PRESSURE_UNITS = PressureConverter.VALID_UNITS

RADIOACTIVITY_CONCENTRATION_UNITS = RadioactivityConcentrationConverter.VALID_UNITS

VOLUME_UNITS = VolumeConverter.VALID_UNITS

WIND_SPEED_UNITS = SpeedConverter.VALID_UNITS
Expand All @@ -72,6 +77,7 @@
VOLUME: VOLUME_UNITS,
PRESSURE: PRESSURE_UNITS,
AREA: AREA_UNITS,
RADIOACTIVITY_CONCENTRATION: RADIOACTIVITY_CONCENTRATION_UNITS,
}


Expand All @@ -95,6 +101,7 @@ def __init__(
length: UnitOfLength,
mass: UnitOfMass,
pressure: UnitOfPressure,
radioactivity_concentration: UnitOfRadioactivityConcentration,
temperature: UnitOfTemperature,
volume: UnitOfVolume,
wind_speed: UnitOfSpeed,
Expand All @@ -111,6 +118,7 @@ def __init__(
(volume, VOLUME),
(mass, MASS),
(pressure, PRESSURE),
(radioactivity_concentration, RADIOACTIVITY_CONCENTRATION),
)
if not _is_valid_unit(unit, unit_type)
)
Expand All @@ -124,6 +132,7 @@ def __init__(
self.length_unit = length
self.mass_unit = mass
self.pressure_unit = pressure
self.radioactivity_concentration_unit = radioactivity_concentration
self.temperature_unit = temperature
self.volume_unit = volume
self.wind_speed_unit = wind_speed
Expand Down Expand Up @@ -198,6 +207,20 @@ def volume(self, volume: float | None, from_unit: str) -> float:
volume, from_unit, self.volume_unit
)

def radioactivity_concentration(
self, radioactivity_concentration: float | None, from_unit: str
) -> float:
"""Convert the given radioactivity_concentration to this unit system."""
if not isinstance(radioactivity_concentration, Number):
raise TypeError(f"{radioactivity_concentration!s} is not a numeric value")

# type ignore: https://github.com/python/mypy/issues/7207
return RadioactivityConcentrationConverter.convert( # type: ignore[unreachable]
radioactivity_concentration,
from_unit,
self.radioactivity_concentration_unit,
)

def as_dict(self) -> dict[str, str]:
"""Convert the unit system to a dictionary."""
return {
Expand All @@ -206,6 +229,7 @@ def as_dict(self) -> dict[str, str]:
AREA: self.area_unit,
MASS: self.mass_unit,
PRESSURE: self.pressure_unit,
RADIOACTIVITY_CONCENTRATION: self.radioactivity_concentration_unit,
TEMPERATURE: self.temperature_unit,
VOLUME: self.volume_unit,
WIND_SPEED: self.wind_speed_unit,
Expand Down Expand Up @@ -314,6 +338,7 @@ def _deprecated_unit_system(value: str) -> str:
length=UnitOfLength.KILOMETERS,
mass=UnitOfMass.GRAMS,
pressure=UnitOfPressure.PA,
radioactivity_concentration=UnitOfRadioactivityConcentration.BECQUERELS_PER_CUBIC_METER,
temperature=UnitOfTemperature.CELSIUS,
volume=UnitOfVolume.LITERS,
wind_speed=UnitOfSpeed.METERS_PER_SECOND,
Expand Down Expand Up @@ -362,6 +387,11 @@ def _deprecated_unit_system(value: str) -> str:
("pressure", UnitOfPressure.HPA): UnitOfPressure.PSI,
("pressure", UnitOfPressure.KPA): UnitOfPressure.PSI,
("pressure", UnitOfPressure.MMHG): UnitOfPressure.INHG,
# Convert non-USCS radioactivity concentration
(
"radioactivity concentration",
UnitOfRadioactivityConcentration.BECQUERELS_PER_CUBIC_METER,
): UnitOfRadioactivityConcentration.PICOCURIES_PER_LITER,
# Convert non-USCS speeds, except knots, to mph
("speed", UnitOfSpeed.METERS_PER_SECOND): UnitOfSpeed.MILES_PER_HOUR,
("speed", UnitOfSpeed.MILLIMETERS_PER_SECOND): UnitOfSpeed.INCHES_PER_SECOND,
Expand Down Expand Up @@ -392,6 +422,7 @@ def _deprecated_unit_system(value: str) -> str:
length=UnitOfLength.MILES,
mass=UnitOfMass.POUNDS,
pressure=UnitOfPressure.PSI,
radioactivity_concentration=UnitOfRadioactivityConcentration.PICOCURIES_PER_LITER,
temperature=UnitOfTemperature.FAHRENHEIT,
volume=UnitOfVolume.GALLONS,
wind_speed=UnitOfSpeed.MILES_PER_HOUR,
Expand Down
2 changes: 2 additions & 0 deletions tests/components/sensor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
UnitOfApparentPower,
UnitOfFrequency,
UnitOfPressure,
UnitOfRadioactivityConcentration,
UnitOfReactivePower,
UnitOfVolume,
)
Expand Down Expand Up @@ -48,6 +49,7 @@
SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS: CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, # µg/m³ of vocs
SensorDeviceClass.VOLTAGE: "V", # voltage (V)
SensorDeviceClass.GAS: UnitOfVolume.CUBIC_METERS, # gas (m³)
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: UnitOfRadioactivityConcentration.BECQUERELS_PER_CUBIC_METER, # radioactivity concentration (Bq/m³)
}


Expand Down
4 changes: 3 additions & 1 deletion tests/components/sensor/test_device_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_matches_device_classes(device_class: SensorDeviceClass) -> None:
SensorDeviceClass.CO2: "CONF_IS_CO2",
SensorDeviceClass.ENERGY_STORAGE: "CONF_IS_ENERGY",
SensorDeviceClass.VOLUME_STORAGE: "CONF_IS_VOLUME",
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: "CONF_IS_RADIOACTIVITY_CONCENTRATION",
}.get(device_class, f"CONF_IS_{device_class.value.upper()}")
assert hasattr(device_condition, constant_name), f"Missing constant {constant_name}"

Expand All @@ -61,6 +62,7 @@ def test_matches_device_classes(device_class: SensorDeviceClass) -> None:
SensorDeviceClass.BATTERY: "is_battery_level",
SensorDeviceClass.ENERGY_STORAGE: "is_energy",
SensorDeviceClass.VOLUME_STORAGE: "is_volume",
SensorDeviceClass.RADIOACTIVITY_CONCENTRATION: "is_radioactivity_concentration",
}.get(device_class, f"is_{device_class.value}")
assert getattr(device_condition, constant_name) == constant_value

Expand Down Expand Up @@ -119,7 +121,7 @@ async def test_get_conditions(
conditions = await async_get_device_automations(
hass, DeviceAutomationType.CONDITION, device_entry.id
)
assert len(conditions) == 27
assert len(conditions) == 28
assert conditions == unordered(expected_conditions)


Expand Down
Loading