Support overriding unit of temperature number entities (#74977)

This commit is contained in:
Erik Montnemery 2022-07-11 14:49:36 +02:00 committed by GitHub
parent 66e27945ac
commit 9d2c213903
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 197 additions and 2 deletions

View file

@ -22,6 +22,7 @@ from homeassistant.const import (
TEMP_FAHRENHEIT,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY
from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
@ -689,3 +690,161 @@ async def test_restore_number_restore_state(
assert entity0.native_value == native_value
assert type(entity0.native_value) == native_value_type
assert entity0.native_unit_of_measurement == uom
@pytest.mark.parametrize(
"device_class,native_unit,custom_unit,state_unit,native_value,custom_value",
[
# Not a supported temperature unit
(
NumberDeviceClass.TEMPERATURE,
TEMP_CELSIUS,
"my_temperature_unit",
TEMP_CELSIUS,
1000,
1000,
),
(
NumberDeviceClass.TEMPERATURE,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
TEMP_FAHRENHEIT,
37.5,
99.5,
),
(
NumberDeviceClass.TEMPERATURE,
TEMP_FAHRENHEIT,
TEMP_CELSIUS,
TEMP_CELSIUS,
100,
38.0,
),
],
)
async def test_custom_unit(
hass,
enable_custom_integrations,
device_class,
native_unit,
custom_unit,
state_unit,
native_value,
custom_value,
):
"""Test custom unit."""
entity_registry = er.async_get(hass)
entry = entity_registry.async_get_or_create("number", "test", "very_unique")
entity_registry.async_update_entity_options(
entry.entity_id, "number", {"unit_of_measurement": custom_unit}
)
await hass.async_block_till_done()
platform = getattr(hass.components, "test.number")
platform.init(empty=True)
platform.ENTITIES.append(
platform.MockNumberEntity(
name="Test",
native_value=native_value,
native_unit_of_measurement=native_unit,
device_class=device_class,
unique_id="very_unique",
)
)
entity0 = platform.ENTITIES[0]
assert await async_setup_component(hass, "number", {"number": {"platform": "test"}})
await hass.async_block_till_done()
state = hass.states.get(entity0.entity_id)
assert float(state.state) == pytest.approx(float(custom_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == state_unit
@pytest.mark.parametrize(
"native_unit, custom_unit, used_custom_unit, default_unit, native_value, custom_value, default_value",
[
(
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
TEMP_FAHRENHEIT,
TEMP_CELSIUS,
37.5,
99.5,
37.5,
),
(
TEMP_FAHRENHEIT,
TEMP_FAHRENHEIT,
TEMP_FAHRENHEIT,
TEMP_CELSIUS,
100,
100,
38.0,
),
# Not a supported temperature unit
(TEMP_CELSIUS, "no_unit", TEMP_CELSIUS, TEMP_CELSIUS, 1000, 1000, 1000),
],
)
async def test_custom_unit_change(
hass,
enable_custom_integrations,
native_unit,
custom_unit,
used_custom_unit,
default_unit,
native_value,
custom_value,
default_value,
):
"""Test custom unit changes are picked up."""
entity_registry = er.async_get(hass)
platform = getattr(hass.components, "test.number")
platform.init(empty=True)
platform.ENTITIES.append(
platform.MockNumberEntity(
name="Test",
native_value=native_value,
native_unit_of_measurement=native_unit,
device_class=NumberDeviceClass.TEMPERATURE,
unique_id="very_unique",
)
)
entity0 = platform.ENTITIES[0]
assert await async_setup_component(hass, "number", {"number": {"platform": "test"}})
await hass.async_block_till_done()
# Default unit conversion according to unit system
state = hass.states.get(entity0.entity_id)
assert float(state.state) == pytest.approx(float(default_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == default_unit
entity_registry.async_update_entity_options(
"number.test", "number", {"unit_of_measurement": custom_unit}
)
await hass.async_block_till_done()
# Unit conversion to the custom unit
state = hass.states.get(entity0.entity_id)
assert float(state.state) == pytest.approx(float(custom_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == used_custom_unit
entity_registry.async_update_entity_options(
"number.test", "number", {"unit_of_measurement": native_unit}
)
await hass.async_block_till_done()
# Unit conversion to another custom unit
state = hass.states.get(entity0.entity_id)
assert float(state.state) == pytest.approx(float(native_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == native_unit
entity_registry.async_update_entity_options("number.test", "number", None)
await hass.async_block_till_done()
# Default unit conversion according to unit system
state = hass.states.get(entity0.entity_id)
assert float(state.state) == pytest.approx(float(default_value))
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == default_unit