Support empty output of MQTT binary_sensor value_template (#37420)

* Support empty output of MQTT binary_sensor value_template

* Strip white space

* Add test

* Add test
pull/37478/head
Erik Montnemery 2020-07-04 17:48:02 +02:00 committed by GitHub
parent 333c151955
commit 9ade1de3d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 4 deletions

View File

@ -189,17 +189,30 @@ class MqttBinarySensor(
payload = value_template.async_render_with_possible_json_value(
payload, variables={"entity_id": self.entity_id}
)
if not payload.strip(): # No output from template, ignore
_LOGGER.debug(
"Empty template output for entity: %s with state topic: %s. Payload: '%s', with value template '%s'",
self._config[CONF_NAME],
self._config[CONF_STATE_TOPIC],
msg.payload,
value_template,
)
return
if payload == self._config[CONF_PAYLOAD_ON]:
self._state = True
elif payload == self._config[CONF_PAYLOAD_OFF]:
self._state = False
else: # Payload is not for this entity
_LOGGER.warning(
"No matching payload found for entity: %s with state topic: %s. Payload: %s, with value template %s",
template_info = ""
if value_template is not None:
template_info = f", template output: '{payload}', with value template '{str(value_template)}'"
_LOGGER.info(
"No matching payload found for entity: %s with state topic: %s. Payload: '%s'%s",
self._config[CONF_NAME],
self._config[CONF_STATE_TOPIC],
payload,
value_template,
msg.payload,
template_info,
)
return

View File

@ -259,6 +259,76 @@ async def test_setting_sensor_value_via_mqtt_message_and_template(hass, mqtt_moc
assert state.state == STATE_OFF
async def test_setting_sensor_value_via_mqtt_message_and_template2(
hass, mqtt_mock, caplog
):
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
{
binary_sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"payload_on": "ON",
"payload_off": "OFF",
"value_template": "{{value | upper}}",
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "test-topic", "on")
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_ON
async_fire_mqtt_message(hass, "test-topic", "off")
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "test-topic", "illegal")
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_OFF
assert "template output: 'ILLEGAL'" in caplog.text
async def test_setting_sensor_value_via_mqtt_message_empty_template(
hass, mqtt_mock, caplog
):
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
hass,
binary_sensor.DOMAIN,
{
binary_sensor.DOMAIN: {
"platform": "mqtt",
"name": "test",
"state_topic": "test-topic",
"payload_on": "ON",
"payload_off": "OFF",
"value_template": '{%if value == "ABC"%}ON{%endif%}',
}
},
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "test-topic", "DEF")
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_OFF
assert "Empty template output" in caplog.text
async_fire_mqtt_message(hass, "test-topic", "ABC")
state = hass.states.get("binary_sensor.test")
assert state.state == STATE_ON
async def test_valid_device_class(hass, mqtt_mock):
"""Test the setting of a valid sensor class."""
assert await async_setup_component(