mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
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
This commit is contained in:
parent
333c151955
commit
9ade1de3d5
@ -189,17 +189,30 @@ class MqttBinarySensor(
|
|||||||
payload = value_template.async_render_with_possible_json_value(
|
payload = value_template.async_render_with_possible_json_value(
|
||||||
payload, variables={"entity_id": self.entity_id}
|
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]:
|
if payload == self._config[CONF_PAYLOAD_ON]:
|
||||||
self._state = True
|
self._state = True
|
||||||
elif payload == self._config[CONF_PAYLOAD_OFF]:
|
elif payload == self._config[CONF_PAYLOAD_OFF]:
|
||||||
self._state = False
|
self._state = False
|
||||||
else: # Payload is not for this entity
|
else: # Payload is not for this entity
|
||||||
_LOGGER.warning(
|
template_info = ""
|
||||||
"No matching payload found for entity: %s with state topic: %s. Payload: %s, with value template %s",
|
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_NAME],
|
||||||
self._config[CONF_STATE_TOPIC],
|
self._config[CONF_STATE_TOPIC],
|
||||||
payload,
|
msg.payload,
|
||||||
value_template,
|
template_info,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -259,6 +259,76 @@ async def test_setting_sensor_value_via_mqtt_message_and_template(hass, mqtt_moc
|
|||||||
assert state.state == STATE_OFF
|
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):
|
async def test_valid_device_class(hass, mqtt_mock):
|
||||||
"""Test the setting of a valid sensor class."""
|
"""Test the setting of a valid sensor class."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user