Add support for MW/GW/TW and GWh/TWh (#130089)
This commit is contained in:
parent
742eca5927
commit
cd11f01ace
8 changed files with 41 additions and 12 deletions
|
@ -162,7 +162,7 @@ class NumberDeviceClass(StrEnum):
|
|||
ENERGY = "energy"
|
||||
"""Energy.
|
||||
|
||||
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
|
||||
Unit of measurement: `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `MJ`, `GJ`
|
||||
"""
|
||||
|
||||
ENERGY_STORAGE = "energy_storage"
|
||||
|
@ -171,7 +171,7 @@ class NumberDeviceClass(StrEnum):
|
|||
Use this device class for sensors measuring stored energy, for example the amount
|
||||
of electric energy currently stored in a battery or the capacity of a battery.
|
||||
|
||||
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
|
||||
Unit of measurement: `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `MJ`, `GJ`
|
||||
"""
|
||||
|
||||
FREQUENCY = "frequency"
|
||||
|
@ -279,7 +279,7 @@ class NumberDeviceClass(StrEnum):
|
|||
POWER = "power"
|
||||
"""Power.
|
||||
|
||||
Unit of measurement: `W`, `kW`
|
||||
Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW`
|
||||
"""
|
||||
|
||||
PRECIPITATION = "precipitation"
|
||||
|
|
|
@ -182,7 +182,7 @@ class SensorDeviceClass(StrEnum):
|
|||
|
||||
Use this device class for sensors measuring energy consumption, for example
|
||||
electric energy consumption.
|
||||
Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `Wh`, `kWh`, `MWh`, `cal`, `kcal`, `Mcal`, `Gcal`
|
||||
Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
|
||||
"""
|
||||
|
||||
ENERGY_STORAGE = "energy_storage"
|
||||
|
@ -191,7 +191,7 @@ class SensorDeviceClass(StrEnum):
|
|||
Use this device class for sensors measuring stored energy, for example the amount
|
||||
of electric energy currently stored in a battery or the capacity of a battery.
|
||||
|
||||
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`
|
||||
Unit of measurement: `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `MJ`, `GJ`
|
||||
"""
|
||||
|
||||
FREQUENCY = "frequency"
|
||||
|
@ -299,7 +299,7 @@ class SensorDeviceClass(StrEnum):
|
|||
POWER = "power"
|
||||
"""Power.
|
||||
|
||||
Unit of measurement: `W`, `kW`
|
||||
Unit of measurement: `W`, `kW`, `MW`, `GW`, `TW`
|
||||
"""
|
||||
|
||||
PRECIPITATION = "precipitation"
|
||||
|
|
|
@ -725,6 +725,9 @@ class UnitOfPower(StrEnum):
|
|||
|
||||
WATT = "W"
|
||||
KILO_WATT = "kW"
|
||||
MEGA_WATT = "MW"
|
||||
GIGA_WATT = "GW"
|
||||
TERA_WATT = "TW"
|
||||
BTU_PER_HOUR = "BTU/h"
|
||||
|
||||
|
||||
|
@ -770,6 +773,8 @@ class UnitOfEnergy(StrEnum):
|
|||
WATT_HOUR = "Wh"
|
||||
KILO_WATT_HOUR = "kWh"
|
||||
MEGA_WATT_HOUR = "MWh"
|
||||
GIGA_WATT_HOUR = "GWh"
|
||||
TERA_WATT_HOUR = "TWh"
|
||||
CALORIE = "cal"
|
||||
KILO_CALORIE = "kcal"
|
||||
MEGA_CALORIE = "Mcal"
|
||||
|
|
|
@ -222,6 +222,8 @@ class EnergyConverter(BaseUnitConverter):
|
|||
UnitOfEnergy.WATT_HOUR: 1e3,
|
||||
UnitOfEnergy.KILO_WATT_HOUR: 1,
|
||||
UnitOfEnergy.MEGA_WATT_HOUR: 1 / 1e3,
|
||||
UnitOfEnergy.GIGA_WATT_HOUR: 1 / 1e6,
|
||||
UnitOfEnergy.TERA_WATT_HOUR: 1 / 1e9,
|
||||
UnitOfEnergy.CALORIE: _WH_TO_CAL * 1e3,
|
||||
UnitOfEnergy.KILO_CALORIE: _WH_TO_CAL,
|
||||
UnitOfEnergy.MEGA_CALORIE: _WH_TO_CAL / 1e3,
|
||||
|
@ -292,10 +294,16 @@ class PowerConverter(BaseUnitConverter):
|
|||
_UNIT_CONVERSION: dict[str | None, float] = {
|
||||
UnitOfPower.WATT: 1,
|
||||
UnitOfPower.KILO_WATT: 1 / 1000,
|
||||
UnitOfPower.MEGA_WATT: 1 / 1e6,
|
||||
UnitOfPower.GIGA_WATT: 1 / 1e9,
|
||||
UnitOfPower.TERA_WATT: 1 / 1e12,
|
||||
}
|
||||
VALID_UNITS = {
|
||||
UnitOfPower.WATT,
|
||||
UnitOfPower.KILO_WATT,
|
||||
UnitOfPower.MEGA_WATT,
|
||||
UnitOfPower.GIGA_WATT,
|
||||
UnitOfPower.TERA_WATT,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4233,8 +4233,8 @@ async def async_record_states(
|
|||
@pytest.mark.parametrize(
|
||||
("units", "attributes", "unit", "unit2", "supported_unit"),
|
||||
[
|
||||
(US_CUSTOMARY_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "W, kW"),
|
||||
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "W, kW"),
|
||||
(US_CUSTOMARY_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"),
|
||||
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"),
|
||||
(
|
||||
US_CUSTOMARY_SYSTEM,
|
||||
TEMPERATURE_SENSOR_ATTRIBUTES,
|
||||
|
@ -4445,8 +4445,8 @@ async def test_validate_statistics_unit_ignore_device_class(
|
|||
@pytest.mark.parametrize(
|
||||
("units", "attributes", "unit", "unit2", "supported_unit"),
|
||||
[
|
||||
(US_CUSTOMARY_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "W, kW"),
|
||||
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "W, kW"),
|
||||
(US_CUSTOMARY_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"),
|
||||
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "kW", "GW, MW, TW, W, kW"),
|
||||
(
|
||||
US_CUSTOMARY_SYSTEM,
|
||||
TEMPERATURE_SENSOR_ATTRIBUTES,
|
||||
|
|
|
@ -794,7 +794,7 @@ EARLY_END_ERROR = "invalid template (TemplateSyntaxError: unexpected 'end of tem
|
|||
),
|
||||
"unit_of_measurement": (
|
||||
"'None' is not a valid unit for device class 'energy'; "
|
||||
"expected one of 'cal', 'Gcal', 'GJ', 'J', 'kcal', 'kJ', 'kWh', 'Mcal', 'MJ', 'MWh', 'Wh'"
|
||||
"expected one of 'cal', 'Gcal', 'GJ', 'GWh', 'J', 'kcal', 'kJ', 'kWh', 'Mcal', 'MJ', 'MWh', 'TWh', 'Wh'"
|
||||
),
|
||||
},
|
||||
),
|
||||
|
|
|
@ -66,7 +66,14 @@ def test_all() -> None:
|
|||
"DEVICE_CLASS_",
|
||||
)
|
||||
+ _create_tuples(const.UnitOfApparentPower, "POWER_")
|
||||
+ _create_tuples(const.UnitOfPower, "POWER_")
|
||||
+ _create_tuples(
|
||||
[
|
||||
const.UnitOfPower.WATT,
|
||||
const.UnitOfPower.KILO_WATT,
|
||||
const.UnitOfPower.BTU_PER_HOUR,
|
||||
],
|
||||
"POWER_",
|
||||
)
|
||||
+ _create_tuples(
|
||||
[
|
||||
const.UnitOfEnergy.KILO_WATT_HOUR,
|
||||
|
|
|
@ -357,10 +357,16 @@ _CONVERTED_VALUE: dict[
|
|||
EnergyConverter: [
|
||||
(10, UnitOfEnergy.WATT_HOUR, 0.01, UnitOfEnergy.KILO_WATT_HOUR),
|
||||
(10, UnitOfEnergy.WATT_HOUR, 0.00001, UnitOfEnergy.MEGA_WATT_HOUR),
|
||||
(10, UnitOfEnergy.WATT_HOUR, 0.00000001, UnitOfEnergy.GIGA_WATT_HOUR),
|
||||
(10, UnitOfEnergy.WATT_HOUR, 0.00000000001, UnitOfEnergy.TERA_WATT_HOUR),
|
||||
(10, UnitOfEnergy.KILO_WATT_HOUR, 10000, UnitOfEnergy.WATT_HOUR),
|
||||
(10, UnitOfEnergy.KILO_WATT_HOUR, 0.01, UnitOfEnergy.MEGA_WATT_HOUR),
|
||||
(10, UnitOfEnergy.MEGA_WATT_HOUR, 10000000, UnitOfEnergy.WATT_HOUR),
|
||||
(10, UnitOfEnergy.MEGA_WATT_HOUR, 10000, UnitOfEnergy.KILO_WATT_HOUR),
|
||||
(10, UnitOfEnergy.GIGA_WATT_HOUR, 10e6, UnitOfEnergy.KILO_WATT_HOUR),
|
||||
(10, UnitOfEnergy.GIGA_WATT_HOUR, 10e9, UnitOfEnergy.WATT_HOUR),
|
||||
(10, UnitOfEnergy.TERA_WATT_HOUR, 10e9, UnitOfEnergy.KILO_WATT_HOUR),
|
||||
(10, UnitOfEnergy.TERA_WATT_HOUR, 10e12, UnitOfEnergy.WATT_HOUR),
|
||||
(10, UnitOfEnergy.GIGA_JOULE, 2777.78, UnitOfEnergy.KILO_WATT_HOUR),
|
||||
(10, UnitOfEnergy.GIGA_JOULE, 2.77778, UnitOfEnergy.MEGA_WATT_HOUR),
|
||||
(10, UnitOfEnergy.MEGA_JOULE, 2.77778, UnitOfEnergy.KILO_WATT_HOUR),
|
||||
|
@ -439,6 +445,9 @@ _CONVERTED_VALUE: dict[
|
|||
],
|
||||
PowerConverter: [
|
||||
(10, UnitOfPower.KILO_WATT, 10000, UnitOfPower.WATT),
|
||||
(10, UnitOfPower.MEGA_WATT, 10e6, UnitOfPower.WATT),
|
||||
(10, UnitOfPower.GIGA_WATT, 10e9, UnitOfPower.WATT),
|
||||
(10, UnitOfPower.TERA_WATT, 10e12, UnitOfPower.WATT),
|
||||
(10, UnitOfPower.WATT, 0.01, UnitOfPower.KILO_WATT),
|
||||
],
|
||||
PressureConverter: [
|
||||
|
|
Loading…
Add table
Reference in a new issue