Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions homeassistant/components/energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
VALID_ENERGY_UNITS: set[str] = {
UnitOfEnergy.GIGA_JOULE,
UnitOfEnergy.KILO_WATT_HOUR,
UnitOfEnergy.MEGA_JOULE,
UnitOfEnergy.MEGA_WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
}
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/energy/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
sensor.SensorDeviceClass.ENERGY: (
UnitOfEnergy.GIGA_JOULE,
UnitOfEnergy.KILO_WATT_HOUR,
UnitOfEnergy.MEGA_JOULE,
UnitOfEnergy.MEGA_WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
)
Expand All @@ -40,6 +41,7 @@
sensor.SensorDeviceClass.ENERGY: (
UnitOfEnergy.GIGA_JOULE,
UnitOfEnergy.KILO_WATT_HOUR,
UnitOfEnergy.MEGA_JOULE,
UnitOfEnergy.MEGA_WATT_HOUR,
UnitOfEnergy.WATT_HOUR,
),
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class SensorDeviceClass(StrEnum):
ENERGY = "energy"
"""Energy.

Unit of measurement: `Wh`, `kWh`, `MWh`, `GJ`
Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same change is needed in number platform.

"""

FREQUENCY = "frequency"
Expand Down
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ class UnitOfEnergy(StrEnum):

GIGA_JOULE = "GJ"
KILO_WATT_HOUR = "kWh"
MEGA_JOULE = "MJ"
MEGA_WATT_HOUR = "MWh"
WATT_HOUR = "Wh"

Expand Down
2 changes: 2 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ class EnergyConverter(BaseUnitConverter):
UnitOfEnergy.WATT_HOUR: 1 * 1000,
UnitOfEnergy.KILO_WATT_HOUR: 1,
UnitOfEnergy.MEGA_WATT_HOUR: 1 / 1000,
UnitOfEnergy.MEGA_JOULE: 3.6,
UnitOfEnergy.GIGA_JOULE: 3.6 / 1000,
}
VALID_UNITS = {
UnitOfEnergy.WATT_HOUR,
UnitOfEnergy.KILO_WATT_HOUR,
UnitOfEnergy.MEGA_WATT_HOUR,
UnitOfEnergy.MEGA_JOULE,
UnitOfEnergy.GIGA_JOULE,
}

Expand Down
26 changes: 18 additions & 8 deletions tests/components/energy/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ async def test_validation_device_consumption_entity_unexpected_unit(
{
"type": "entity_unexpected_unit_energy",
"affected_entities": {("sensor.unexpected_unit", "beers")},
"translation_placeholders": {"energy_units": "GJ, kWh, MWh, Wh"},
"translation_placeholders": {
"energy_units": "GJ, kWh, MJ, MWh, Wh"
},
}
]
],
Expand Down Expand Up @@ -306,7 +308,9 @@ async def test_validation_solar(hass, mock_energy_manager, mock_get_metadata):
{
"type": "entity_unexpected_unit_energy",
"affected_entities": {("sensor.solar_production", "beers")},
"translation_placeholders": {"energy_units": "GJ, kWh, MWh, Wh"},
"translation_placeholders": {
"energy_units": "GJ, kWh, MJ, MWh, Wh"
},
}
]
],
Expand Down Expand Up @@ -355,7 +359,9 @@ async def test_validation_battery(hass, mock_energy_manager, mock_get_metadata):
("sensor.battery_import", "beers"),
("sensor.battery_export", "beers"),
},
"translation_placeholders": {"energy_units": "GJ, kWh, MWh, Wh"},
"translation_placeholders": {
"energy_units": "GJ, kWh, MJ, MWh, Wh"
},
},
]
],
Expand Down Expand Up @@ -424,7 +430,9 @@ async def test_validation_grid(
("sensor.grid_consumption_1", "beers"),
("sensor.grid_production_1", "beers"),
},
"translation_placeholders": {"energy_units": "GJ, kWh, MWh, Wh"},
"translation_placeholders": {
"energy_units": "GJ, kWh, MJ, MWh, Wh"
},
},
{
"type": "statistics_not_defined",
Expand Down Expand Up @@ -511,7 +519,9 @@ async def test_validation_grid_external_cost_compensation(
("sensor.grid_consumption_1", "beers"),
("sensor.grid_production_1", "beers"),
},
"translation_placeholders": {"energy_units": "GJ, kWh, MWh, Wh"},
"translation_placeholders": {
"energy_units": "GJ, kWh, MJ, MWh, Wh"
},
},
{
"type": "statistics_not_defined",
Expand Down Expand Up @@ -678,7 +688,7 @@ async def test_validation_grid_auto_cost_entity_errors(
"type": "entity_unexpected_unit_energy_price",
"affected_entities": {("sensor.grid_price_1", "$/Ws")},
"translation_placeholders": {
"price_units": "EUR/GJ, EUR/kWh, EUR/MWh, EUR/Wh"
"price_units": "EUR/GJ, EUR/kWh, EUR/MJ, EUR/MWh, EUR/Wh"
},
},
),
Expand Down Expand Up @@ -822,7 +832,7 @@ async def test_validation_gas(
"type": "entity_unexpected_unit_gas",
"affected_entities": {("sensor.gas_consumption_1", "beers")},
"translation_placeholders": {
"energy_units": "GJ, kWh, MWh, Wh",
"energy_units": "GJ, kWh, MJ, MWh, Wh",
"gas_units": "CCF, ft³, m³",
},
},
Expand Down Expand Up @@ -852,7 +862,7 @@ async def test_validation_gas(
"affected_entities": {("sensor.gas_price_2", "EUR/invalid")},
"translation_placeholders": {
"price_units": (
"EUR/GJ, EUR/kWh, EUR/MWh, EUR/Wh, EUR/CCF, EUR/ft³, EUR/m³"
"EUR/GJ, EUR/kWh, EUR/MJ, EUR/MWh, EUR/Wh, EUR/CCF, EUR/ft³, EUR/m³"
)
},
},
Expand Down