Fix mqtt sensor or binary_sensor state not saved after expiry (#101670)
Fix mqtt sensor state not saved after expire
This commit is contained in:
parent
78535b99df
commit
db0c5bbbea
4 changed files with 28 additions and 4 deletions
|
@ -180,7 +180,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@log_messages(self.hass, self.entity_id)
|
@log_messages(self.hass, self.entity_id)
|
||||||
@write_state_on_attr_change(self, {"_attr_is_on"})
|
@write_state_on_attr_change(self, {"_attr_is_on", "_expired"})
|
||||||
def state_message_received(msg: ReceiveMessage) -> None:
|
def state_message_received(msg: ReceiveMessage) -> None:
|
||||||
"""Handle a new received MQTT state message."""
|
"""Handle a new received MQTT state message."""
|
||||||
# auto-expire enabled?
|
# auto-expire enabled?
|
||||||
|
|
|
@ -277,7 +277,9 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@write_state_on_attr_change(self, {"_attr_native_value", "_attr_last_reset"})
|
@write_state_on_attr_change(
|
||||||
|
self, {"_attr_native_value", "_attr_last_reset", "_expired"}
|
||||||
|
)
|
||||||
@log_messages(self.hass, self.entity_id)
|
@log_messages(self.hass, self.entity_id)
|
||||||
def message_received(msg: ReceiveMessage) -> None:
|
def message_received(msg: ReceiveMessage) -> None:
|
||||||
"""Handle new MQTT messages."""
|
"""Handle new MQTT messages."""
|
||||||
|
|
|
@ -123,7 +123,6 @@ async def test_setting_sensor_value_expires_availability_topic(
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"expire_after": 4,
|
"expire_after": 4,
|
||||||
"force_update": True,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,6 +199,18 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
||||||
state = hass.states.get("binary_sensor.test")
|
state = hass.states.get("binary_sensor.test")
|
||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
# Send the last message again
|
||||||
|
# Time jump 0.5s
|
||||||
|
now += timedelta(seconds=0.5)
|
||||||
|
freezer.move_to(now)
|
||||||
|
async_fire_time_changed(hass, now)
|
||||||
|
async_fire_mqtt_message(hass, "test-topic", "OFF")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Value was updated correctly.
|
||||||
|
state = hass.states.get("binary_sensor.test")
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
|
||||||
|
|
||||||
async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
|
|
@ -339,7 +339,6 @@ async def test_setting_sensor_value_expires_availability_topic(
|
||||||
"state_topic": "test-topic",
|
"state_topic": "test-topic",
|
||||||
"unit_of_measurement": "fav unit",
|
"unit_of_measurement": "fav unit",
|
||||||
"expire_after": "4",
|
"expire_after": "4",
|
||||||
"force_update": True,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -413,6 +412,18 @@ async def expires_helper(hass: HomeAssistant) -> None:
|
||||||
state = hass.states.get("sensor.test")
|
state = hass.states.get("sensor.test")
|
||||||
assert state.state == STATE_UNAVAILABLE
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
# Send the last message again
|
||||||
|
# Time jump 0.5s
|
||||||
|
now += timedelta(seconds=0.5)
|
||||||
|
freezer.move_to(now)
|
||||||
|
async_fire_time_changed(hass, now)
|
||||||
|
async_fire_mqtt_message(hass, "test-topic", "101")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Value was updated correctly.
|
||||||
|
state = hass.states.get("sensor.test")
|
||||||
|
assert state.state == "101"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"hass_config",
|
"hass_config",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue