mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
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
17c3e7b238
commit
b573e5a2b3
@ -260,14 +260,18 @@ class MqttSensor(MqttEntity, RestoreSensor):
|
|||||||
msg.topic,
|
msg.topic,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if payload == PAYLOAD_NONE:
|
||||||
|
self._attr_native_value = None
|
||||||
|
return
|
||||||
|
|
||||||
if self._numeric_state_expected:
|
if self._numeric_state_expected:
|
||||||
if payload == "":
|
if payload == "":
|
||||||
_LOGGER.debug("Ignore empty state from '%s'", msg.topic)
|
_LOGGER.debug("Ignore empty state from '%s'", msg.topic)
|
||||||
elif payload == PAYLOAD_NONE:
|
|
||||||
self._attr_native_value = None
|
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = payload
|
self._attr_native_value = payload
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.options and payload not in self.options:
|
if self.options and payload not in self.options:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Ignoring invalid option received on topic '%s', got '%s', allowed: %s",
|
"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,
|
STATE_UNKNOWN,
|
||||||
True,
|
True,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
help_custom_config(
|
||||||
|
sensor.DOMAIN,
|
||||||
|
DEFAULT_CONFIG,
|
||||||
|
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
|
||||||
|
),
|
||||||
|
sensor.SensorDeviceClass.TIMESTAMP,
|
||||||
|
"None",
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
False,
|
||||||
|
),
|
||||||
(
|
(
|
||||||
help_custom_config(
|
help_custom_config(
|
||||||
sensor.DOMAIN,
|
sensor.DOMAIN,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user