Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions homeassistant/components/number/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class NumberDeviceClass(StrEnum):
ENERGY = "energy"
"""Energy.

Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `Ws`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
"""

ENERGY_DISTANCE = "energy_distance"
Expand All @@ -193,7 +193,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: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `Ws`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
"""

FREQUENCY = "frequency"
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,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`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `Ws`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
"""

ENERGY_DISTANCE = "energy_distance"
Expand All @@ -228,7 +228,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: `J`, `kJ`, `MJ`, `GJ`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
Unit of measurement: `J`, `kJ`, `MJ`, `GJ`, `Ws`, `mWh`, `Wh`, `kWh`, `MWh`, `GWh`, `TWh`, `cal`, `kcal`, `Mcal`, `Gcal`
"""

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 @@ -488,6 +488,7 @@ class UnitOfEnergy(StrEnum):
KILO_JOULE = "kJ"
MEGA_JOULE = "MJ"
GIGA_JOULE = "GJ"
WATT_SECOND = "Ws"
MILLIWATT_HOUR = "mWh"
WATT_HOUR = "Wh"
KILO_WATT_HOUR = "kWh"
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
_DAYS_TO_SECS = 24 * _HRS_TO_SECS # 1 day = 24 hours = 86400 seconds

# Energy conversion constants
_WH_TO_J = 3600 # 1 Wh = 3600 J
_WH_TO_J = 3600 # 1 Wh = 3600 J = 3600 Ws
_WH_TO_CAL = _WH_TO_J / 4.184 # 1 Wh = 860.42065 cal

# Mass conversion constants
Expand Down Expand Up @@ -334,6 +334,7 @@ class EnergyConverter(BaseUnitConverter):
UnitOfEnergy.KILO_JOULE: _WH_TO_J,
UnitOfEnergy.MEGA_JOULE: _WH_TO_J / 1e3,
UnitOfEnergy.GIGA_JOULE: _WH_TO_J / 1e6,
UnitOfEnergy.WATT_SECOND: _WH_TO_J * 1e3,
UnitOfEnergy.MILLIWATT_HOUR: 1e6,
UnitOfEnergy.WATT_HOUR: 1e3,
UnitOfEnergy.KILO_WATT_HOUR: 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/components/template/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ async def test_config_flow_preview(
),
"unit_of_measurement": (
"'None' is not a valid unit for device class 'energy'; "
"expected one of 'cal', 'Gcal', 'GJ', 'GWh', 'J', 'kcal', 'kJ', 'kWh', 'Mcal', 'MJ', 'MWh', 'mWh', 'TWh', 'Wh'"
"expected one of 'cal', 'Gcal', 'GJ', 'GWh', 'J', 'kcal', 'kJ', 'kWh', 'Mcal', 'MJ', 'MWh', 'mWh', 'TWh', 'Wh', 'Ws'"
),
},
),
Expand Down
2 changes: 2 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,14 @@
(5, UnitOfElectricPotential.MEGAVOLT, 5e3, UnitOfElectricPotential.KILOVOLT),
],
EnergyConverter: [
(10, UnitOfEnergy.WATT_SECOND, 2.77778e-3, UnitOfEnergy.WATT_HOUR),
(10, UnitOfEnergy.MILLIWATT_HOUR, 0.00001, UnitOfEnergy.KILO_WATT_HOUR),
(10, UnitOfEnergy.WATT_HOUR, 10000, UnitOfEnergy.MILLIWATT_HOUR),
(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, 3.6e7, UnitOfEnergy.WATT_SECOND),
(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),
Expand Down