Add suggested_unit_of_measurement attribute to sensors (#80638)

* Add suggested_unit_of_measurement attribute to sensors

* Lazy calculation of initial entity options

* Add type alias for entity options

* Small tweak

* Add tests

* Store suggested_unit_of_measurement in its own option key

* Adapt to renaming of IMPERIAL_SYSTEM

* Fix rebase mistakes

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Erik Montnemery 2022-10-24 16:08:02 +02:00 committed by GitHub
parent 0c79a9a33d
commit 6979cd95b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 380 additions and 48 deletions

View file

@ -39,85 +39,92 @@ def test_invalid_units():
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
accumulated_precipitation=LENGTH_MILLIMETERS,
length=LENGTH_METERS,
length_conversions={},
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=INVALID_UNIT,
length=LENGTH_METERS,
wind_speed=SPEED_METERS_PER_SECOND,
volume=VOLUME_LITERS,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
accumulated_precipitation=LENGTH_MILLIMETERS,
wind_speed=SPEED_METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
temperature=TEMP_CELSIUS,
accumulated_precipitation=LENGTH_MILLIMETERS,
length=INVALID_UNIT,
wind_speed=SPEED_METERS_PER_SECOND,
volume=VOLUME_LITERS,
length_conversions={},
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
accumulated_precipitation=LENGTH_MILLIMETERS,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
temperature=TEMP_CELSIUS,
accumulated_precipitation=LENGTH_MILLIMETERS,
length=LENGTH_METERS,
length_conversions={},
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=INVALID_UNIT,
volume=VOLUME_LITERS,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
accumulated_precipitation=LENGTH_MILLIMETERS,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
temperature=TEMP_CELSIUS,
accumulated_precipitation=LENGTH_MILLIMETERS,
length=LENGTH_METERS,
wind_speed=SPEED_METERS_PER_SECOND,
length_conversions={},
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
temperature=TEMP_CELSIUS,
volume=INVALID_UNIT,
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
accumulated_precipitation=LENGTH_MILLIMETERS,
wind_speed=SPEED_METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
temperature=TEMP_CELSIUS,
accumulated_precipitation=LENGTH_MILLIMETERS,
length=LENGTH_METERS,
wind_speed=SPEED_METERS_PER_SECOND,
volume=VOLUME_LITERS,
length_conversions={},
mass=INVALID_UNIT,
pressure=PRESSURE_PA,
accumulated_precipitation=LENGTH_MILLIMETERS,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
temperature=TEMP_CELSIUS,
accumulated_precipitation=LENGTH_MILLIMETERS,
length=LENGTH_METERS,
wind_speed=SPEED_METERS_PER_SECOND,
volume=VOLUME_LITERS,
length_conversions={},
mass=MASS_GRAMS,
pressure=INVALID_UNIT,
accumulated_precipitation=LENGTH_MILLIMETERS,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
)
with pytest.raises(ValueError):
UnitSystem(
SYSTEM_NAME,
temperature=TEMP_CELSIUS,
accumulated_precipitation=INVALID_UNIT,
length=LENGTH_METERS,
wind_speed=SPEED_METERS_PER_SECOND,
volume=VOLUME_LITERS,
length_conversions={},
mass=MASS_GRAMS,
pressure=PRESSURE_PA,
accumulated_precipitation=INVALID_UNIT,
temperature=TEMP_CELSIUS,
volume=VOLUME_LITERS,
wind_speed=SPEED_METERS_PER_SECOND,
)