diff --git a/homeassistant/components/energy/sensor.py b/homeassistant/components/energy/sensor.py index 5ad4c74a6cf..6f16d2dc831 100644 --- a/homeassistant/components/energy/sensor.py +++ b/homeassistant/components/energy/sensor.py @@ -43,6 +43,7 @@ SUPPORTED_STATE_CLASSES = { VALID_ENERGY_UNITS: set[str] = { UnitOfEnergy.GIGA_JOULE, UnitOfEnergy.KILO_WATT_HOUR, + UnitOfEnergy.MEGA_JOULE, UnitOfEnergy.MEGA_WATT_HOUR, UnitOfEnergy.WATT_HOUR, } diff --git a/homeassistant/components/energy/validate.py b/homeassistant/components/energy/validate.py index f8f276f6438..a2c3ad094da 100644 --- a/homeassistant/components/energy/validate.py +++ b/homeassistant/components/energy/validate.py @@ -23,6 +23,7 @@ ENERGY_USAGE_UNITS = { sensor.SensorDeviceClass.ENERGY: ( UnitOfEnergy.GIGA_JOULE, UnitOfEnergy.KILO_WATT_HOUR, + UnitOfEnergy.MEGA_JOULE, UnitOfEnergy.MEGA_WATT_HOUR, UnitOfEnergy.WATT_HOUR, ) @@ -40,6 +41,7 @@ GAS_USAGE_UNITS = { sensor.SensorDeviceClass.ENERGY: ( UnitOfEnergy.GIGA_JOULE, UnitOfEnergy.KILO_WATT_HOUR, + UnitOfEnergy.MEGA_JOULE, UnitOfEnergy.MEGA_WATT_HOUR, UnitOfEnergy.WATT_HOUR, ), diff --git a/homeassistant/components/number/const.py b/homeassistant/components/number/const.py index c10d677b773..b3f31ac23fe 100644 --- a/homeassistant/components/number/const.py +++ b/homeassistant/components/number/const.py @@ -122,7 +122,7 @@ class NumberDeviceClass(StrEnum): ENERGY = "energy" """Energy. - Unit of measurement: `Wh`, `kWh`, `MWh`, `GJ` + Unit of measurement: `Wh`, `kWh`, `MWh`, `MJ`, `GJ` """ FREQUENCY = "frequency" diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py index 66d231bea24..93fccbab124 100644 --- a/homeassistant/components/sensor/const.py +++ b/homeassistant/components/sensor/const.py @@ -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` """ FREQUENCY = "frequency" diff --git a/homeassistant/const.py b/homeassistant/const.py index 4be0be42f76..44cec9fb861 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -517,6 +517,7 @@ class UnitOfEnergy(StrEnum): GIGA_JOULE = "GJ" KILO_WATT_HOUR = "kWh" + MEGA_JOULE = "MJ" MEGA_WATT_HOUR = "MWh" WATT_HOUR = "Wh" diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 930e5a71e42..0fde15acd71 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -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, } diff --git a/tests/components/energy/test_validate.py b/tests/components/energy/test_validate.py index 4aa852e69e0..924a63dc12c 100644 --- a/tests/components/energy/test_validate.py +++ b/tests/components/energy/test_validate.py @@ -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" + }, } ] ], @@ -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" + }, } ] ], @@ -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" + }, }, ] ], @@ -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", @@ -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", @@ -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" }, }, ), @@ -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³", }, }, @@ -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³" ) }, }, diff --git a/tests/util/test_unit_conversion.py b/tests/util/test_unit_conversion.py index b18813a4079..648db8420d3 100644 --- a/tests/util/test_unit_conversion.py +++ b/tests/util/test_unit_conversion.py @@ -208,6 +208,8 @@ _CONVERTED_VALUE: dict[ (10, UnitOfEnergy.MEGA_WATT_HOUR, 10000, UnitOfEnergy.KILO_WATT_HOUR), (10, UnitOfEnergy.GIGA_JOULE, 10000 / 3.6, UnitOfEnergy.KILO_WATT_HOUR), (10, UnitOfEnergy.GIGA_JOULE, 10 / 3.6, UnitOfEnergy.MEGA_WATT_HOUR), + (10, UnitOfEnergy.MEGA_JOULE, 10 / 3.6, UnitOfEnergy.KILO_WATT_HOUR), + (10, UnitOfEnergy.MEGA_JOULE, 0.010 / 3.6, UnitOfEnergy.MEGA_WATT_HOUR), ], InformationConverter: [ (8e3, UnitOfInformation.BITS, 8, UnitOfInformation.KILOBITS),