1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-25 05:26:47 +00:00

Add device_class to MQTT number and migrate to native_value (#73534)

This commit is contained in:
Erik Montnemery
2022-06-16 13:34:54 +02:00
committed by GitHub
parent 67b0354632
commit dea8041461
3 changed files with 122 additions and 57 deletions

View File

@@ -18,11 +18,14 @@ from homeassistant.components.number import (
ATTR_VALUE,
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
NumberDeviceClass,
)
from homeassistant.const import (
ATTR_ASSUMED_STATE,
ATTR_DEVICE_CLASS,
ATTR_ENTITY_ID,
ATTR_UNIT_OF_MEASUREMENT,
TEMP_FAHRENHEIT,
Platform,
)
import homeassistant.core as ha
@@ -58,7 +61,7 @@ from .test_common import (
help_test_update_with_json_attrs_not_dict,
)
from tests.common import async_fire_mqtt_message
from tests.common import async_fire_mqtt_message, mock_restore_cache_with_extra_data
DEFAULT_CONFIG = {
number.DOMAIN: {"platform": "mqtt", "name": "test", "command_topic": "test-topic"}
@@ -84,7 +87,8 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
"state_topic": topic,
"command_topic": topic,
"name": "Test Number",
"unit_of_measurement": "my unit",
"device_class": "temperature",
"unit_of_measurement": TEMP_FAHRENHEIT,
"payload_reset": "reset!",
}
},
@@ -97,16 +101,18 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
await hass.async_block_till_done()
state = hass.states.get("number.test_number")
assert state.state == "10"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "my unit"
assert state.state == "-12.0" # 10 °F -> -12 °C
assert state.attributes.get(ATTR_DEVICE_CLASS) == NumberDeviceClass.TEMPERATURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "°C"
async_fire_mqtt_message(hass, topic, "20.5")
await hass.async_block_till_done()
state = hass.states.get("number.test_number")
assert state.state == "20.5"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "my unit"
assert state.state == "-6.4" # 20.5 °F -> -6.4 °C
assert state.attributes.get(ATTR_DEVICE_CLASS) == NumberDeviceClass.TEMPERATURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "°C"
async_fire_mqtt_message(hass, topic, "reset!")
@@ -114,7 +120,8 @@ async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config):
state = hass.states.get("number.test_number")
assert state.state == "unknown"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "my unit"
assert state.attributes.get(ATTR_DEVICE_CLASS) == NumberDeviceClass.TEMPERATURE
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "°C"
async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
@@ -158,29 +165,70 @@ async def test_value_template(hass, mqtt_mock_entry_with_yaml_config):
assert state.state == "unknown"
async def test_restore_native_value(hass, mqtt_mock_entry_with_yaml_config):
"""Test that the stored native_value is restored."""
topic = "test/number"
RESTORE_DATA = {
"native_max_value": None, # Ignored by MQTT number
"native_min_value": None, # Ignored by MQTT number
"native_step": None, # Ignored by MQTT number
"native_unit_of_measurement": None, # Ignored by MQTT number
"native_value": 100.0,
}
mock_restore_cache_with_extra_data(
hass, ((ha.State("number.test_number", "abc"), RESTORE_DATA),)
)
assert await async_setup_component(
hass,
number.DOMAIN,
{
"number": {
"platform": "mqtt",
"command_topic": topic,
"device_class": "temperature",
"unit_of_measurement": TEMP_FAHRENHEIT,
"name": "Test Number",
}
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("number.test_number")
assert state.state == "37.8"
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_run_number_service_optimistic(hass, mqtt_mock_entry_with_yaml_config):
"""Test that set_value service works in optimistic mode."""
topic = "test/number"
fake_state = ha.State("switch.test", "3")
RESTORE_DATA = {
"native_max_value": None, # Ignored by MQTT number
"native_min_value": None, # Ignored by MQTT number
"native_step": None, # Ignored by MQTT number
"native_unit_of_measurement": None, # Ignored by MQTT number
"native_value": 3,
}
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=fake_state,
):
assert await async_setup_component(
hass,
number.DOMAIN,
{
"number": {
"platform": "mqtt",
"command_topic": topic,
"name": "Test Number",
}
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
mock_restore_cache_with_extra_data(
hass, ((ha.State("number.test_number", "abc"), RESTORE_DATA),)
)
assert await async_setup_component(
hass,
number.DOMAIN,
{
"number": {
"platform": "mqtt",
"command_topic": topic,
"name": "Test Number",
}
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("number.test_number")
assert state.state == "3"
@@ -232,26 +280,31 @@ async def test_run_number_service_optimistic_with_command_template(
"""Test that set_value service works in optimistic mode and with a command_template."""
topic = "test/number"
fake_state = ha.State("switch.test", "3")
RESTORE_DATA = {
"native_max_value": None, # Ignored by MQTT number
"native_min_value": None, # Ignored by MQTT number
"native_step": None, # Ignored by MQTT number
"native_unit_of_measurement": None, # Ignored by MQTT number
"native_value": 3,
}
with patch(
"homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state",
return_value=fake_state,
):
assert await async_setup_component(
hass,
number.DOMAIN,
{
"number": {
"platform": "mqtt",
"command_topic": topic,
"name": "Test Number",
"command_template": '{"number": {{ value }} }',
}
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
mock_restore_cache_with_extra_data(
hass, ((ha.State("number.test_number", "abc"), RESTORE_DATA),)
)
assert await async_setup_component(
hass,
number.DOMAIN,
{
"number": {
"platform": "mqtt",
"command_topic": topic,
"name": "Test Number",
"command_template": '{"number": {{ value }} }',
}
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("number.test_number")
assert state.state == "3"