Ignore empty status update for mqtt number (#94800)

This commit is contained in:
Jan Bouwhuis 2023-06-20 23:04:01 +02:00 committed by GitHub
parent 3e71b1daa4
commit 4def901ecc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View file

@ -186,6 +186,9 @@ class MqttNumber(MqttEntity, RestoreNumber):
"""Handle new MQTT messages."""
num_value: int | float | None
payload = str(self._value_template(msg.payload))
if not payload.strip():
_LOGGER.debug("Ignoring empty state update from '%s'", msg.topic)
return
try:
if payload == self._config[CONF_PAYLOAD_RESET]:
num_value = None

View file

@ -189,6 +189,14 @@ async def test_value_template(
state = hass.states.get("number.test_number")
assert state.state == "10"
# Assert an empty value from a template is ignored
async_fire_mqtt_message(hass, topic, '{"other_val":12}')
await hass.async_block_till_done()
state = hass.states.get("number.test_number")
assert state.state == "10"
async_fire_mqtt_message(hass, topic, '{"val":20.5}')
await hass.async_block_till_done()