Migrate energy units to an enum (#80998)
This commit is contained in:
parent
9d3442055b
commit
842cb18d39
8 changed files with 85 additions and 93 deletions
|
@ -16,13 +16,10 @@ from homeassistant.components.sensor.recorder import compile_statistics
|
|||
from homeassistant.const import (
|
||||
ATTR_DEVICE_CLASS,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
ENERGY_GIGA_JOULE,
|
||||
ENERGY_KILO_WATT_HOUR,
|
||||
ENERGY_MEGA_WATT_HOUR,
|
||||
ENERGY_WATT_HOUR,
|
||||
STATE_UNKNOWN,
|
||||
VOLUME_CUBIC_FEET,
|
||||
VOLUME_CUBIC_METERS,
|
||||
UnitOfEnergy,
|
||||
)
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -143,7 +140,7 @@ async def test_cost_sensor_price_entity_total_increasing(
|
|||
return compile_statistics(hass, now, now + timedelta(seconds=1)).platform_stats
|
||||
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING,
|
||||
}
|
||||
|
||||
|
@ -347,7 +344,7 @@ async def test_cost_sensor_price_entity_total(
|
|||
return compile_statistics(hass, now, now + timedelta(seconds=1)).platform_stats
|
||||
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: energy_state_class,
|
||||
}
|
||||
|
||||
|
@ -553,7 +550,7 @@ async def test_cost_sensor_price_entity_total_no_reset(
|
|||
return compile_statistics(hass, now, now + timedelta(seconds=1)).platform_stats
|
||||
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: energy_state_class,
|
||||
}
|
||||
|
||||
|
@ -701,10 +698,10 @@ async def test_cost_sensor_price_entity_total_no_reset(
|
|||
@pytest.mark.parametrize(
|
||||
"energy_unit,factor",
|
||||
[
|
||||
(ENERGY_WATT_HOUR, 1000),
|
||||
(ENERGY_KILO_WATT_HOUR, 1),
|
||||
(ENERGY_MEGA_WATT_HOUR, 0.001),
|
||||
(ENERGY_GIGA_JOULE, 0.001 * 3.6),
|
||||
(UnitOfEnergy.WATT_HOUR, 1000),
|
||||
(UnitOfEnergy.KILO_WATT_HOUR, 1),
|
||||
(UnitOfEnergy.MEGA_WATT_HOUR, 0.001),
|
||||
(UnitOfEnergy.GIGA_JOULE, 0.001 * 3.6),
|
||||
],
|
||||
)
|
||||
async def test_cost_sensor_handle_energy_units(
|
||||
|
@ -767,10 +764,10 @@ async def test_cost_sensor_handle_energy_units(
|
|||
@pytest.mark.parametrize(
|
||||
"price_unit,factor",
|
||||
[
|
||||
(f"EUR/{ENERGY_WATT_HOUR}", 0.001),
|
||||
(f"EUR/{ENERGY_KILO_WATT_HOUR}", 1),
|
||||
(f"EUR/{ENERGY_MEGA_WATT_HOUR}", 1000),
|
||||
(f"EUR/{ENERGY_GIGA_JOULE}", 1000 / 3.6),
|
||||
(f"EUR/{UnitOfEnergy.WATT_HOUR}", 0.001),
|
||||
(f"EUR/{UnitOfEnergy.KILO_WATT_HOUR}", 1),
|
||||
(f"EUR/{UnitOfEnergy.MEGA_WATT_HOUR}", 1000),
|
||||
(f"EUR/{UnitOfEnergy.GIGA_JOULE}", 1000 / 3.6),
|
||||
],
|
||||
)
|
||||
async def test_cost_sensor_handle_price_units(
|
||||
|
@ -778,7 +775,7 @@ async def test_cost_sensor_handle_price_units(
|
|||
) -> None:
|
||||
"""Test energy cost price from sensor entity."""
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING,
|
||||
}
|
||||
price_attributes = {
|
||||
|
@ -891,7 +888,7 @@ async def test_cost_sensor_handle_gas_kwh(
|
|||
) -> None:
|
||||
"""Test gas cost price from sensor entity."""
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING,
|
||||
}
|
||||
energy_data = data.EnergyManager.default_preferences()
|
||||
|
@ -942,7 +939,7 @@ async def test_cost_sensor_wrong_state_class(
|
|||
) -> None:
|
||||
"""Test energy sensor rejects sensor with wrong state_class."""
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: state_class,
|
||||
}
|
||||
energy_data = data.EnergyManager.default_preferences()
|
||||
|
@ -1003,7 +1000,7 @@ async def test_cost_sensor_state_class_measurement_no_reset(
|
|||
) -> None:
|
||||
"""Test energy sensor rejects state_class measurement with no last_reset."""
|
||||
energy_attributes = {
|
||||
ATTR_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
|
||||
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.KILO_WATT_HOUR,
|
||||
ATTR_STATE_CLASS: state_class,
|
||||
}
|
||||
energy_data = data.EnergyManager.default_preferences()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue