Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanov committed Aug 2, 2024
1 parent 20d4713 commit 130d06f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
22 changes: 11 additions & 11 deletions homeassistant/util/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,17 @@ class EnergyConverter(BaseUnitConverter):
UNIT_CLASS = "energy"
NORMALIZED_UNIT = UnitOfEnergy.KILO_WATT_HOUR
_UNIT_CONVERSION: dict[str | None, float] = {
UnitOfEnergy.JOULE: 1,
UnitOfEnergy.KILO_JOULE: 1 / 1e3,
UnitOfEnergy.MEGA_JOULE: 1 / 1e6,
UnitOfEnergy.GIGA_JOULE: 1 / 1e9,
UnitOfEnergy.WATT_HOUR: 1 / _WH_TO_J,
UnitOfEnergy.KILO_WATT_HOUR: 1 / _WH_TO_J / 1e3,
UnitOfEnergy.MEGA_WATT_HOUR: 1 / _WH_TO_J / 1e6,
UnitOfEnergy.CALORIE: 1 / _CAL_TO_J,
UnitOfEnergy.KILO_CALORIE: 1 / _CAL_TO_J / 1e3,
UnitOfEnergy.MEGA_CALORIE: 1 / _CAL_TO_J / 1e6,
UnitOfEnergy.GIGA_CALORIE: 1 / _CAL_TO_J / 1e9,
UnitOfEnergy.JOULE: _WH_TO_J * 1e3,
UnitOfEnergy.KILO_JOULE: _WH_TO_J,
UnitOfEnergy.MEGA_JOULE: _WH_TO_J / 1e3,
UnitOfEnergy.GIGA_JOULE: _WH_TO_J / 1e6,
UnitOfEnergy.WATT_HOUR: 1e3,
UnitOfEnergy.KILO_WATT_HOUR: 1,
UnitOfEnergy.MEGA_WATT_HOUR: 1 / 1e3,
UnitOfEnergy.CALORIE: _WH_TO_J / _CAL_TO_J * 1e3,
UnitOfEnergy.KILO_CALORIE: _WH_TO_J / _CAL_TO_J,
UnitOfEnergy.MEGA_CALORIE: _WH_TO_J / _CAL_TO_J / 1e3,
UnitOfEnergy.GIGA_CALORIE: _WH_TO_J / _CAL_TO_J / 1e6,
}
VALID_UNITS = set(UnitOfEnergy)

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 @@ -694,7 +694,7 @@ async def test_config_flow_preview(
),
"unit_of_measurement": (
"'None' is not a valid unit for device class 'energy'; "
"expected one of 'GJ', 'kWh', 'MJ', 'MWh', 'Wh'"
"expected one of 'cal', 'Gcal', 'GJ', 'J', 'kcal', 'kJ', 'kWh', 'Mcal', 'MJ', 'MWh', 'Wh'"
),
},
),
Expand Down
12 changes: 12 additions & 0 deletions tests/util/test_unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,18 @@
(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),
(10, UnitOfEnergy.KILO_JOULE, 10 / 3.6, UnitOfEnergy.WATT_HOUR),
(10, UnitOfEnergy.KILO_JOULE, 0.010 / 3600, UnitOfEnergy.MEGA_WATT_HOUR),
(10, UnitOfEnergy.JOULE, 10 / 3600, UnitOfEnergy.WATT_HOUR),
(10, UnitOfEnergy.JOULE, 10 / 4.184, UnitOfEnergy.CALORIE),
(10, UnitOfEnergy.CALORIE, 0.01, UnitOfEnergy.KILO_CALORIE),
(10, UnitOfEnergy.CALORIE, 41.84 / 3600, UnitOfEnergy.WATT_HOUR),
(10, UnitOfEnergy.KILO_CALORIE, 0.01, UnitOfEnergy.MEGA_CALORIE),
(10, UnitOfEnergy.KILO_CALORIE, 41.84 / 3600, UnitOfEnergy.KILO_WATT_HOUR),
(10, UnitOfEnergy.MEGA_CALORIE, 0.01, UnitOfEnergy.GIGA_CALORIE),
(10, UnitOfEnergy.MEGA_CALORIE, 41.84 / 3600, UnitOfEnergy.MEGA_WATT_HOUR),
(10, UnitOfEnergy.GIGA_CALORIE, 10000, UnitOfEnergy.MEGA_CALORIE),
(10, UnitOfEnergy.GIGA_CALORIE, 41.84 / 3.6, UnitOfEnergy.MEGA_WATT_HOUR),
],
InformationConverter: [
(8e3, UnitOfInformation.BITS, 8, UnitOfInformation.KILOBITS),
Expand Down

0 comments on commit 130d06f

Please sign in to comment.