Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make variable entity_id available to value_template for MQTT binary sensor #19195

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/binary_sensor/mqtt.py
Expand Up @@ -135,7 +135,7 @@ def state_message_received(_topic, payload, _qos):
value_template = self._config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
payload = value_template.async_render_with_possible_json_value(
payload)
payload, variables={'entity_id': self.entity_id})
if payload == self._config.get(CONF_PAYLOAD_ON):
self._state = True
elif payload == self._config.get(CONF_PAYLOAD_OFF):
Expand Down
9 changes: 5 additions & 4 deletions homeassistant/helpers/template.py
Expand Up @@ -149,7 +149,8 @@ def render_with_possible_json_value(self, value, error_value=_SENTINEL):
error_value).result()

def async_render_with_possible_json_value(self, value,
error_value=_SENTINEL):
error_value=_SENTINEL,
variables=None):
"""Render template with value exposed.

If valid JSON will expose value_json too.
Expand All @@ -159,9 +160,9 @@ def async_render_with_possible_json_value(self, value,
if self._compiled is None:
self._ensure_compiled()

variables = {
'value': value
}
variables = dict(variables or {})
variables['value'] = value

try:
variables['value_json'] = json.loads(value)
except ValueError:
Expand Down
27 changes: 27 additions & 0 deletions tests/components/binary_sensor/test_mqtt.py
Expand Up @@ -58,6 +58,33 @@ def test_setting_sensor_value_via_mqtt_message(self):
state = self.hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state

def test_setting_sensor_value_via_mqtt_message_and_template(self):
"""Test the setting of the value via MQTT."""
assert setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'payload_on': 'ON',
'payload_off': 'OFF',
'value_template': '{%if is_state(entity_id,\"on\")-%}OFF'
'{%-else-%}ON{%-endif%}'
}
})

state = self.hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state

fire_mqtt_message(self.hass, 'test-topic', '')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test')
assert STATE_ON == state.state

fire_mqtt_message(self.hass, 'test-topic', '')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state

def test_valid_device_class(self):
"""Test the setting of a valid sensor class."""
assert setup_component(self.hass, binary_sensor.DOMAIN, {
Expand Down