Add support for unit of measurement in MQTT number platform (#58343)
parent
34b936da98
commit
027e167d95
|
@ -1,4 +1,6 @@
|
|||
"""Configure number in a device through MQTT topic."""
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
import logging
|
||||
|
||||
|
@ -11,7 +13,12 @@ from homeassistant.components.number import (
|
|||
DEFAULT_STEP,
|
||||
NumberEntity,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_VALUE_TEMPLATE
|
||||
from homeassistant.const import (
|
||||
CONF_NAME,
|
||||
CONF_OPTIMISTIC,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
|
@ -64,6 +71,7 @@ PLATFORM_SCHEMA = vol.All(
|
|||
vol.Coerce(float), vol.Range(min=1e-3)
|
||||
),
|
||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
||||
},
|
||||
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema),
|
||||
validate_config,
|
||||
|
@ -195,6 +203,11 @@ class MqttNumber(MqttEntity, NumberEntity, RestoreEntity):
|
|||
"""Return the increment/decrement step."""
|
||||
return self._config[CONF_STEP]
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str | None:
|
||||
"""Return the unit of measurement."""
|
||||
return self._config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
"""Return the current value."""
|
||||
|
|
|
@ -18,7 +18,11 @@ from homeassistant.components.number import (
|
|||
DOMAIN as NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
)
|
||||
from homeassistant.const import ATTR_ASSUMED_STATE, ATTR_ENTITY_ID
|
||||
from homeassistant.const import (
|
||||
ATTR_ASSUMED_STATE,
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
)
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
@ -66,6 +70,7 @@ async def test_run_number_setup(hass, mqtt_mock):
|
|||
"state_topic": topic,
|
||||
"command_topic": topic,
|
||||
"name": "Test Number",
|
||||
"unit_of_measurement": "my unit",
|
||||
"payload_reset": "reset!",
|
||||
}
|
||||
},
|
||||
|
@ -78,6 +83,7 @@ async def test_run_number_setup(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("number.test_number")
|
||||
assert state.state == "10"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "my unit"
|
||||
|
||||
async_fire_mqtt_message(hass, topic, "20.5")
|
||||
|
||||
|
@ -85,6 +91,7 @@ async def test_run_number_setup(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("number.test_number")
|
||||
assert state.state == "20.5"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "my unit"
|
||||
|
||||
async_fire_mqtt_message(hass, topic, "reset!")
|
||||
|
||||
|
@ -92,6 +99,7 @@ async def test_run_number_setup(hass, mqtt_mock):
|
|||
|
||||
state = hass.states.get("number.test_number")
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "my unit"
|
||||
|
||||
|
||||
async def test_value_template(hass, mqtt_mock):
|
||||
|
|
Loading…
Reference in New Issue