Mqtt rework on value templates (#62105)

* add MqttValueTemplate class

* support variables at initiation

* pass MqttEntity instead of hass

* Use MqttValueTemplace class for value templates

* make hass en enitity parameters conditional

* remove unused property and remove None assignment

* rename self._attr_value_template
This commit is contained in:
Jan Bouwhuis 2022-01-03 16:07:40 +01:00 committed by GitHub
parent bf78ddcadb
commit 3ca18922e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 253 additions and 227 deletions

View file

@ -30,7 +30,7 @@ from homeassistant.helpers.reload import async_setup_reload_service
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util
from . import PLATFORMS, subscription
from . import PLATFORMS, MqttValueTemplate, subscription
from .. import mqtt
from .const import CONF_ENCODING, CONF_QOS, CONF_STATE_TOPIC, DOMAIN
from .debug_info import log_messages
@ -119,9 +119,10 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity):
return DISCOVERY_SCHEMA
def _setup_from_config(self, config):
value_template = self._config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
value_template.hass = self.hass
self._value_template = MqttValueTemplate(
self._config.get(CONF_VALUE_TEMPLATE),
entity=self,
).async_render_with_possible_json_value
async def _subscribe_topics(self):
"""(Re)Subscribe to topics."""
@ -137,7 +138,6 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity):
@log_messages(self.hass, self.entity_id)
def state_message_received(msg):
"""Handle a new received MQTT state message."""
payload = msg.payload
# auto-expire enabled?
expire_after = self._config.get(CONF_EXPIRE_AFTER)
@ -159,20 +159,16 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity):
self.hass, self._value_is_expired, expiration_at
)
value_template = self._config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
payload = value_template.async_render_with_possible_json_value(
payload, variables={"entity_id": self.entity_id}
payload = self._value_template(msg.payload)
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,
self._config.get(CONF_VALUE_TEMPLATE),
)
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
return
if payload == self._config[CONF_PAYLOAD_ON]:
self._state = True
@ -180,8 +176,8 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity):
self._state = False
else: # Payload is not for this entity
template_info = ""
if value_template is not None:
template_info = f", template output: '{payload}', with value template '{str(value_template)}'"
if self._config.get(CONF_VALUE_TEMPLATE) is not None:
template_info = f", template output: '{payload}', with value template '{str(self._config.get(CONF_VALUE_TEMPLATE))}'"
_LOGGER.info(
"No matching payload found for entity: %s with state topic: %s. Payload: '%s'%s",
self._config[CONF_NAME],