diff --git a/homeassistant/components/mqtt/binary_sensor.py b/homeassistant/components/mqtt/binary_sensor.py index 800db2cad79..d9ec64a02c9 100644 --- a/homeassistant/components/mqtt/binary_sensor.py +++ b/homeassistant/components/mqtt/binary_sensor.py @@ -49,6 +49,7 @@ DEFAULT_PAYLOAD_OFF = "OFF" DEFAULT_PAYLOAD_ON = "ON" DEFAULT_FORCE_UPDATE = False CONF_EXPIRE_AFTER = "expire_after" +PAYLOAD_NONE = "None" PLATFORM_SCHEMA = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend( { @@ -174,6 +175,8 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity): self._state = True elif payload == self._config[CONF_PAYLOAD_OFF]: self._state = False + elif payload == PAYLOAD_NONE: + self._state = None else: # Payload is not for this entity template_info = "" if self._config.get(CONF_VALUE_TEMPLATE) is not None: @@ -221,7 +224,7 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity): self.async_write_ha_state() @property - def is_on(self): + def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" return self._state diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index a13f0781dfb..1d94dc7ccf7 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -269,6 +269,10 @@ async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock): state = hass.states.get("binary_sensor.test") assert state.state == STATE_OFF + async_fire_mqtt_message(hass, "test-topic", "None") + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_UNKNOWN + async def test_invalid_sensor_value_via_mqtt_message(hass, mqtt_mock, caplog): """Test the setting of the value via MQTT."""