mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Fix handling undecoded mqtt sensor payloads (#118633)
This commit is contained in:
parent
afc29fdbe7
commit
375f48142c
@ -237,28 +237,32 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
payload = msg.payload
|
payload = msg.payload
|
||||||
if payload is PayloadSentinel.DEFAULT:
|
if payload is PayloadSentinel.DEFAULT:
|
||||||
return
|
return
|
||||||
new_value = str(payload)
|
if not isinstance(payload, str):
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Invalid undecoded state message '%s' received from '%s'",
|
||||||
|
payload,
|
||||||
|
msg.topic,
|
||||||
|
)
|
||||||
|
return
|
||||||
if self._numeric_state_expected:
|
if self._numeric_state_expected:
|
||||||
if new_value == "":
|
if payload == "":
|
||||||
_LOGGER.debug("Ignore empty state from '%s'", msg.topic)
|
_LOGGER.debug("Ignore empty state from '%s'", msg.topic)
|
||||||
elif new_value == PAYLOAD_NONE:
|
elif payload == PAYLOAD_NONE:
|
||||||
self._attr_native_value = None
|
self._attr_native_value = None
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = new_value
|
self._attr_native_value = payload
|
||||||
return
|
return
|
||||||
if self.device_class in {
|
if self.device_class in {
|
||||||
None,
|
None,
|
||||||
SensorDeviceClass.ENUM,
|
SensorDeviceClass.ENUM,
|
||||||
} and not check_state_too_long(_LOGGER, new_value, self.entity_id, msg):
|
} and not check_state_too_long(_LOGGER, payload, self.entity_id, msg):
|
||||||
self._attr_native_value = new_value
|
self._attr_native_value = payload
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
if (payload_datetime := dt_util.parse_datetime(new_value)) is None:
|
if (payload_datetime := dt_util.parse_datetime(payload)) is None:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning("Invalid state message '%s' from '%s'", payload, msg.topic)
|
||||||
"Invalid state message '%s' from '%s'", msg.payload, msg.topic
|
|
||||||
)
|
|
||||||
self._attr_native_value = None
|
self._attr_native_value = None
|
||||||
return
|
return
|
||||||
if self.device_class == SensorDeviceClass.DATE:
|
if self.device_class == SensorDeviceClass.DATE:
|
||||||
|
@ -110,6 +110,42 @@ async def test_setting_sensor_value_via_mqtt_message(
|
|||||||
assert state.attributes.get("unit_of_measurement") == "fav unit"
|
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(
|
@pytest.mark.parametrize(
|
||||||
"hass_config",
|
"hass_config",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user