Allow null / None value for non numeric mqtt sensor without warnings (#127032)

Allow `null` / `None` value for mqtt sensor without warnings
This commit is contained in:
Jan Bouwhuis 2024-09-30 07:05:12 +02:00 committed by Franck Nijhof
parent 084c2d976e
commit daa13235e6
No known key found for this signature in database
GPG key ID: D62583BA8AB11CA3
2 changed files with 17 additions and 2 deletions

View file

@ -260,14 +260,18 @@ class MqttSensor(MqttEntity, RestoreSensor):
msg.topic,
)
return
if payload == PAYLOAD_NONE:
self._attr_native_value = None
return
if self._numeric_state_expected:
if payload == "":
_LOGGER.debug("Ignore empty state from '%s'", msg.topic)
elif payload == PAYLOAD_NONE:
self._attr_native_value = None
else:
self._attr_native_value = payload
return
if self.options and payload not in self.options:
_LOGGER.warning(
"Ignoring invalid option received on topic '%s', got '%s', allowed: %s",

View file

@ -299,6 +299,17 @@ async def test_setting_sensor_to_long_state_via_mqtt_message(
STATE_UNKNOWN,
True,
),
(
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
),
sensor.SensorDeviceClass.TIMESTAMP,
"None",
STATE_UNKNOWN,
False,
),
(
help_custom_config(
sensor.DOMAIN,