mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Raise ValueError on date parsing of MQTT sensor with invalid date format (#89036)
* Suppress ValueError on date parsing of MQTT sensor * Simplify, but not update state on invalid payload * Still raise an an invalid date * Make datetime state unknown on invalid format * remove unrelated added new line
This commit is contained in:
parent
2fc2c2efbe
commit
a9becd8e0e
@ -284,7 +284,10 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
if self.device_class is None:
|
if self.device_class is None:
|
||||||
self._attr_native_value = new_value
|
self._attr_native_value = new_value
|
||||||
return
|
return
|
||||||
if (payload_datetime := dt_util.parse_datetime(new_value)) is None:
|
try:
|
||||||
|
if (payload_datetime := dt_util.parse_datetime(new_value)) is None:
|
||||||
|
raise ValueError
|
||||||
|
except ValueError:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Invalid state message '%s' from '%s'", msg.payload, msg.topic
|
"Invalid state message '%s' from '%s'", msg.payload, msg.topic
|
||||||
)
|
)
|
||||||
|
@ -134,6 +134,12 @@ async def test_setting_sensor_value_via_mqtt_message(
|
|||||||
"2021-11-18T19:25:00+00:00",
|
"2021-11-18T19:25:00+00:00",
|
||||||
False,
|
False,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
sensor.SensorDeviceClass.TIMESTAMP,
|
||||||
|
"2021-13-18T35:25:00+00:00",
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
True,
|
||||||
|
),
|
||||||
(sensor.SensorDeviceClass.TIMESTAMP, "invalid", STATE_UNKNOWN, True),
|
(sensor.SensorDeviceClass.TIMESTAMP, "invalid", STATE_UNKNOWN, True),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user