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

Improve error logging on invalid MQTT entity state (#118006)

* Improve error logging on invalid MQTT entity state

* Explain not hanlding TpeError and ValueError

* Move length check closer to source

* use _LOGGER.exception
This commit is contained in:
Jan Bouwhuis
2024-05-24 13:11:52 +02:00
committed by GitHub
parent 7d44321f0f
commit f12aee28a8
7 changed files with 106 additions and 12 deletions

View File

@@ -931,7 +931,11 @@ async def test_handle_logging_on_writing_the_entity_state(
assert state is not None
assert state.state == "initial_state"
assert "Invalid value for sensor" in caplog.text
assert "Exception raised when updating state of" in caplog.text
assert (
"Exception raised while updating "
"state of sensor.test_sensor, topic: 'test/state' "
"with payload: b'payload causing errors'" in caplog.text
)
async def test_receiving_non_utf8_message_gets_logged(

View File

@@ -110,6 +110,36 @@ 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",
}
}
},
],
)
async def test_setting_sensor_to_long_state_via_mqtt_message(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the setting of the value via MQTT."""
await mqtt_mock_entry()
async_fire_mqtt_message(hass, "test-topic", "".join("x" for _ in range(310)))
state = hass.states.get("sensor.test")
await hass.async_block_till_done()
assert state.state == STATE_UNKNOWN
assert "Cannot update state for entity sensor.test" in caplog.text
@pytest.mark.parametrize(
("hass_config", "device_class", "native_value", "state_value", "log"),
[

View File

@@ -142,7 +142,7 @@ async def test_forced_text_length(
state = hass.states.get("text.test")
assert state.state == "12345"
assert (
"ValueError: Entity text.test provides state 123456 "
"Entity text.test provides state 123456 "
"which is too long (maximum length 5)" in caplog.text
)
@@ -152,7 +152,7 @@ async def test_forced_text_length(
state = hass.states.get("text.test")
assert state.state == "12345"
assert (
"ValueError: Entity text.test provides state 1 "
"Entity text.test provides state 1 "
"which is too short (minimum length 5)" in caplog.text
)
# Valid update
@@ -200,7 +200,7 @@ async def test_controlling_validation_state_via_topic(
async_fire_mqtt_message(hass, "state-topic", "other")
await hass.async_block_till_done()
assert (
"ValueError: Entity text.test provides state other which does not match expected pattern (y|n)"
"Entity text.test provides state other which does not match expected pattern (y|n)"
in caplog.text
)
state = hass.states.get("text.test")
@@ -211,7 +211,7 @@ async def test_controlling_validation_state_via_topic(
async_fire_mqtt_message(hass, "state-topic", "yesyesyesyes")
await hass.async_block_till_done()
assert (
"ValueError: Entity text.test provides state yesyesyesyes which is too long (maximum length 10)"
"Entity text.test provides state yesyesyesyes which is too long (maximum length 10)"
in caplog.text
)
state = hass.states.get("text.test")
@@ -222,7 +222,7 @@ async def test_controlling_validation_state_via_topic(
async_fire_mqtt_message(hass, "state-topic", "y")
await hass.async_block_till_done()
assert (
"ValueError: Entity text.test provides state y which is too short (minimum length 2)"
"Entity text.test provides state y which is too short (minimum length 2)"
in caplog.text
)
state = hass.states.get("text.test")
@@ -285,6 +285,36 @@ async def test_attribute_validation_max_not_greater_then_max_state_length(
assert "max text length must be <= 255" in caplog.text
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
text.DOMAIN: {
"name": "test",
"command_topic": "command-topic",
"state_topic": "state-topic",
}
}
}
],
)
async def test_validation_payload_greater_then_max_state_length(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the max value of of max configuration attribute."""
assert await mqtt_mock_entry()
state = hass.states.get("text.test")
assert state.state == STATE_UNKNOWN
async_fire_mqtt_message(hass, "state-topic", "".join("x" for _ in range(310)))
assert "Cannot update state for entity text.test" in caplog.text
@pytest.mark.parametrize(
"hass_config",
[