Extend UnitOfApparentPower with 'mVA' (#150422)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Ludovic BOUÉ
2025-08-13 15:56:04 +02:00
committed by GitHub
parent b40f381164
commit eb4b75a9a7
8 changed files with 40 additions and 3 deletions

View File

@@ -88,7 +88,7 @@ class NumberDeviceClass(StrEnum):
APPARENT_POWER = "apparent_power"
"""Apparent power.
Unit of measurement: `VA`
Unit of measurement: `mVA`, `VA`
"""
AQI = "aqi"

View File

@@ -42,6 +42,7 @@ from homeassistant.util import dt as dt_util
from homeassistant.util.collection import chunked_or_all
from homeassistant.util.enum import try_parse_enum
from homeassistant.util.unit_conversion import (
ApparentPowerConverter,
AreaConverter,
BaseUnitConverter,
BloodGlucoseConcentrationConverter,
@@ -193,6 +194,7 @@ QUERY_STATISTICS_SUMMARY_SUM = (
STATISTIC_UNIT_TO_UNIT_CONVERTER: dict[str | None, type[BaseUnitConverter]] = {
**dict.fromkeys(ApparentPowerConverter.VALID_UNITS, ApparentPowerConverter),
**dict.fromkeys(AreaConverter.VALID_UNITS, AreaConverter),
**dict.fromkeys(
BloodGlucoseConcentrationConverter.VALID_UNITS,

View File

@@ -16,6 +16,7 @@ from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.json import json_bytes
from homeassistant.util import dt as dt_util
from homeassistant.util.unit_conversion import (
ApparentPowerConverter,
AreaConverter,
BloodGlucoseConcentrationConverter,
ConductivityConverter,
@@ -59,6 +60,7 @@ UPDATE_STATISTICS_METADATA_TIME_OUT = 10
UNIT_SCHEMA = vol.Schema(
{
vol.Optional("apparent_power"): vol.In(ApparentPowerConverter.VALID_UNITS),
vol.Optional("area"): vol.In(AreaConverter.VALID_UNITS),
vol.Optional("blood_glucose_concentration"): vol.In(
BloodGlucoseConcentrationConverter.VALID_UNITS

View File

@@ -46,6 +46,7 @@ from homeassistant.const import (
UnitOfVolumetricFlux,
)
from homeassistant.util.unit_conversion import (
ApparentPowerConverter,
AreaConverter,
BaseUnitConverter,
BloodGlucoseConcentrationConverter,
@@ -117,7 +118,7 @@ class SensorDeviceClass(StrEnum):
APPARENT_POWER = "apparent_power"
"""Apparent power.
Unit of measurement: `VA`
Unit of measurement: `mVA`, `VA`
"""
AQI = "aqi"
@@ -528,6 +529,7 @@ STATE_CLASSES_SCHEMA: Final = vol.All(vol.Lower, vol.Coerce(SensorStateClass))
STATE_CLASSES: Final[list[str]] = [cls.value for cls in SensorStateClass]
UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] = {
SensorDeviceClass.APPARENT_POWER: ApparentPowerConverter,
SensorDeviceClass.ABSOLUTE_HUMIDITY: MassVolumeConcentrationConverter,
SensorDeviceClass.AREA: AreaConverter,
SensorDeviceClass.ATMOSPHERIC_PRESSURE: PressureConverter,

View File

@@ -588,6 +588,7 @@ ATTR_PERSONS: Final = "persons"
class UnitOfApparentPower(StrEnum):
"""Apparent power units."""
MILLIVOLT_AMPERE = "mVA"
VOLT_AMPERE = "VA"

View File

@@ -14,6 +14,7 @@ from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
UNIT_NOT_RECOGNIZED_TEMPLATE,
UnitOfApparentPower,
UnitOfArea,
UnitOfBloodGlucoseConcentration,
UnitOfConductivity,
@@ -382,6 +383,20 @@ class MassConverter(BaseUnitConverter):
}
class ApparentPowerConverter(BaseUnitConverter):
"""Utility to convert apparent power values."""
UNIT_CLASS = "apparent_power"
_UNIT_CONVERSION: dict[str | None, float] = {
UnitOfApparentPower.MILLIVOLT_AMPERE: 1 * 1000,
UnitOfApparentPower.VOLT_AMPERE: 1,
}
VALID_UNITS = {
UnitOfApparentPower.MILLIVOLT_AMPERE,
UnitOfApparentPower.VOLT_AMPERE,
}
class PowerConverter(BaseUnitConverter):
"""Utility to convert power values."""

View File

@@ -2958,7 +2958,6 @@ def test_device_class_units_are_complete() -> None:
def test_device_class_converters_are_complete() -> None:
"""Test that the device class converters enum is complete."""
no_converter_device_classes = {
SensorDeviceClass.APPARENT_POWER,
SensorDeviceClass.AQI,
SensorDeviceClass.BATTERY,
SensorDeviceClass.CO,

View File

@@ -14,6 +14,7 @@ from homeassistant.const import (
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
PERCENTAGE,
UnitOfApparentPower,
UnitOfArea,
UnitOfBloodGlucoseConcentration,
UnitOfConductivity,
@@ -38,6 +39,7 @@ from homeassistant.const import (
from homeassistant.exceptions import HomeAssistantError
from homeassistant.util import unit_conversion
from homeassistant.util.unit_conversion import (
ApparentPowerConverter,
AreaConverter,
BaseUnitConverter,
BloodGlucoseConcentrationConverter,
@@ -83,6 +85,7 @@ _ALL_CONVERTERS: dict[type[BaseUnitConverter], list[str | None]] = {
EnergyConverter,
InformationConverter,
MassConverter,
ApparentPowerConverter,
PowerConverter,
PressureConverter,
ReactiveEnergyConverter,
@@ -138,6 +141,11 @@ _GET_UNIT_RATIO: dict[type[BaseUnitConverter], tuple[str | None, str | None, flo
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
1000,
),
ApparentPowerConverter: (
UnitOfApparentPower.MILLIVOLT_AMPERE,
UnitOfApparentPower.VOLT_AMPERE,
1000,
),
PowerConverter: (UnitOfPower.WATT, UnitOfPower.KILO_WATT, 1000),
PressureConverter: (UnitOfPressure.HPA, UnitOfPressure.INHG, 33.86389),
ReactiveEnergyConverter: (
@@ -615,6 +623,14 @@ _CONVERTED_VALUE: dict[
(1, UnitOfMass.STONES, 14, UnitOfMass.POUNDS),
(1, UnitOfMass.STONES, 224, UnitOfMass.OUNCES),
],
ApparentPowerConverter: [
(
10,
UnitOfApparentPower.MILLIVOLT_AMPERE,
0.01,
UnitOfApparentPower.VOLT_AMPERE,
),
],
PowerConverter: [
(10, UnitOfPower.KILO_WATT, 10000, UnitOfPower.WATT),
(10, UnitOfPower.MEGA_WATT, 10e6, UnitOfPower.WATT),