Skip to content

Commit

Permalink
Add GJ as supported unit for energy sensors (home-assistant#80870)
Browse files Browse the repository at this point in the history
* Add GJ as supported unit for energy sensors

* Update homeassistant/const.py

Co-authored-by: epenet <[email protected]>

Co-authored-by: epenet <[email protected]>
  • Loading branch information
emontnemery and epenet authored Oct 25, 2022
1 parent e9a3982 commit 727eccf
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
17 changes: 15 additions & 2 deletions homeassistant/components/energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from homeassistant.components.sensor.recorder import reset_detected
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
ENERGY_GIGA_JOULE,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
Expand Down Expand Up @@ -44,7 +45,12 @@
SensorStateClass.TOTAL,
SensorStateClass.TOTAL_INCREASING,
]
VALID_ENERGY_UNITS = [ENERGY_WATT_HOUR, ENERGY_KILO_WATT_HOUR, ENERGY_MEGA_WATT_HOUR]
VALID_ENERGY_UNITS = [
ENERGY_WATT_HOUR,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_GIGA_JOULE,
]
VALID_ENERGY_UNITS_GAS = [VOLUME_CUBIC_FEET, VOLUME_CUBIC_METERS] + VALID_ENERGY_UNITS
_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -233,7 +239,7 @@ def _reset(self, energy_state: State) -> None:
self.async_write_ha_state()

@callback
def _update_cost(self) -> None:
def _update_cost(self) -> None: # noqa: C901
"""Update incurred costs."""
energy_state = self.hass.states.get(
cast(str, self._config[self._adapter.stat_energy_key])
Expand Down Expand Up @@ -289,6 +295,11 @@ def _update_cost(self) -> None:
):
energy_price /= 1000.0

if energy_price_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT, "").endswith(
f"/{ENERGY_GIGA_JOULE}"
):
energy_price /= 1000 / 3.6

else:
energy_price_state = None
energy_price = cast(float, self._config["number_energy_price"])
Expand All @@ -312,6 +323,8 @@ def _update_cost(self) -> None:
energy_price /= 1000
elif energy_unit == ENERGY_MEGA_WATT_HOUR:
energy_price *= 1000
elif energy_unit == ENERGY_GIGA_JOULE:
energy_price *= 1000 / 3.6

if energy_unit is None:
if not self._wrong_unit_reported:
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/energy/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.components import recorder, sensor
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ENERGY_GIGA_JOULE,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
Expand All @@ -28,6 +29,7 @@
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
ENERGY_GIGA_JOULE,
)
}
ENERGY_PRICE_UNITS = tuple(
Expand All @@ -44,6 +46,7 @@
ENERGY_WATT_HOUR,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_GIGA_JOULE,
),
sensor.SensorDeviceClass.GAS: (VOLUME_CUBIC_METERS, VOLUME_CUBIC_FEET),
}
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class SensorDeviceClass(StrEnum):
ENERGY = "energy"
"""Energy.
Unit of measurement: `Wh`, `kWh`, `MWh`
Unit of measurement: `Wh`, `kWh`, `MWh`, `GJ`
"""

FREQUENCY = "frequency"
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,10 @@ class Platform(StrEnum):
POWER_VOLT_AMPERE_REACTIVE: Final = "var"

# Energy units
ENERGY_WATT_HOUR: Final = "Wh"
ENERGY_GIGA_JOULE: Final = "GJ"
ENERGY_KILO_WATT_HOUR: Final = "kWh"
ENERGY_MEGA_WATT_HOUR: Final = "MWh"
ENERGY_WATT_HOUR: Final = "Wh"

# Electric_current units
ELECTRIC_CURRENT_MILLIAMPERE: Final = "mA"
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from homeassistant.const import (
ENERGY_GIGA_JOULE,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
Expand Down Expand Up @@ -158,11 +159,13 @@ class EnergyConverter(BaseUnitConverter):
ENERGY_WATT_HOUR: 1 * 1000,
ENERGY_KILO_WATT_HOUR: 1,
ENERGY_MEGA_WATT_HOUR: 1 / 1000,
ENERGY_GIGA_JOULE: 3.6 / 1000,
}
VALID_UNITS = {
ENERGY_WATT_HOUR,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_GIGA_JOULE,
}


Expand Down
3 changes: 3 additions & 0 deletions tests/components/energy/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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,
Expand Down Expand Up @@ -703,6 +704,7 @@ def _compile_statistics(_):
(ENERGY_WATT_HOUR, 1000),
(ENERGY_KILO_WATT_HOUR, 1),
(ENERGY_MEGA_WATT_HOUR, 0.001),
(ENERGY_GIGA_JOULE, 0.001 * 3.6),
],
)
async def test_cost_sensor_handle_energy_units(
Expand Down Expand Up @@ -768,6 +770,7 @@ async def test_cost_sensor_handle_energy_units(
(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),
],
)
async def test_cost_sensor_handle_price_units(
Expand Down
2 changes: 2 additions & 0 deletions tests/components/energy/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from homeassistant.components.energy import async_get_manager, validate
from homeassistant.const import (
ENERGY_GIGA_JOULE,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
Expand Down Expand Up @@ -73,6 +74,7 @@ async def test_validation_empty_config(hass):
("total", ENERGY_KILO_WATT_HOUR, {}),
("total", ENERGY_KILO_WATT_HOUR, {"last_reset": "abc"}),
("measurement", ENERGY_KILO_WATT_HOUR, {"last_reset": "abc"}),
("total_increasing", ENERGY_GIGA_JOULE, {}),
],
)
async def test_validation(
Expand Down
4 changes: 4 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from homeassistant.const import (
ENERGY_GIGA_JOULE,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
Expand Down Expand Up @@ -78,6 +79,7 @@
(EnergyConverter, ENERGY_WATT_HOUR),
(EnergyConverter, ENERGY_KILO_WATT_HOUR),
(EnergyConverter, ENERGY_MEGA_WATT_HOUR),
(EnergyConverter, ENERGY_GIGA_JOULE),
(MassConverter, MASS_GRAMS),
(MassConverter, MASS_KILOGRAMS),
(MassConverter, MASS_MICROGRAMS),
Expand Down Expand Up @@ -268,6 +270,8 @@ def test_distance_convert(
(10, ENERGY_KILO_WATT_HOUR, 0.01, ENERGY_MEGA_WATT_HOUR),
(10, ENERGY_MEGA_WATT_HOUR, 10000000, ENERGY_WATT_HOUR),
(10, ENERGY_MEGA_WATT_HOUR, 10000, ENERGY_KILO_WATT_HOUR),
(10, ENERGY_GIGA_JOULE, 10000 / 3.6, ENERGY_KILO_WATT_HOUR),
(10, ENERGY_GIGA_JOULE, 10 / 3.6, ENERGY_MEGA_WATT_HOUR),
],
)
def test_energy_convert(
Expand Down

0 comments on commit 727eccf

Please sign in to comment.