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:
parent
084c2d976e
commit
daa13235e6
2 changed files with 17 additions and 2 deletions
|
@ -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",
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue