Fix handling undecoded mqtt sensor payloads (#118633)

This commit is contained in:
Jan Bouwhuis
2024-06-02 21:25:05 +02:00
committed by GitHub
parent afc29fdbe7
commit 375f48142c
2 changed files with 50 additions and 10 deletions

View File

@@ -110,6 +110,42 @@ async def test_setting_sensor_value_via_mqtt_message(
assert state.attributes.get("unit_of_measurement") == "fav unit"
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"name": "test",
"state_topic": "test-topic",
"unit_of_measurement": "%",
"device_class": "battery",
"encoding": "",
}
}
}
],
)
async def test_handling_undecoded_sensor_value(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the setting of the value via MQTT."""
await mqtt_mock_entry()
state = hass.states.get("sensor.test")
assert state.state == STATE_UNKNOWN
async_fire_mqtt_message(hass, "test-topic", b"88")
state = hass.states.get("sensor.test")
assert state.state == STATE_UNKNOWN
assert (
"Invalid undecoded state message 'b'88'' received from 'test-topic'"
in caplog.text
)
@pytest.mark.parametrize(
"hass_config",
[