Allow integrations to drop custom unit conversion (#81005)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2022-10-26 21:11:28 +02:00 committed by GitHub
parent a603441180
commit a4310d2085
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 6 deletions

View file

@ -873,3 +873,57 @@ async def test_unit_conversion_priority_suggested_unit_change(
state = hass.states.get(entity1.entity_id)
assert float(state.state) == approx(float(original_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == original_unit
@pytest.mark.parametrize(
"unit_system, native_unit, original_unit, native_value, original_value, device_class",
[
# Distance
(
US_CUSTOMARY_SYSTEM,
LENGTH_KILOMETERS,
LENGTH_MILES,
1000,
621,
SensorDeviceClass.DISTANCE,
),
],
)
async def test_unit_conversion_priority_legacy_conversion_removed(
hass,
enable_custom_integrations,
unit_system,
native_unit,
original_unit,
native_value,
original_value,
device_class,
):
"""Test priority of unit conversion."""
hass.config.units = unit_system
entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.sensor")
platform.init(empty=True)
# Pre-register entities
entity_registry.async_get_or_create(
"sensor", "test", "very_unique", unit_of_measurement=original_unit
)
platform.ENTITIES["0"] = platform.MockSensor(
name="Test",
device_class=device_class,
native_unit_of_measurement=native_unit,
native_value=str(native_value),
unique_id="very_unique",
)
entity0 = platform.ENTITIES["0"]
assert await async_setup_component(hass, "sensor", {"sensor": {"platform": "test"}})
await hass.async_block_till_done()
state = hass.states.get(entity0.entity_id)
assert float(state.state) == approx(float(original_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == original_unit