make hass and entity conditional parameters

This commit is contained in:
jbouwh 2021-12-21 11:18:51 +00:00
parent e687cf777d
commit cac2f8ac06
10 changed files with 31 additions and 25 deletions

View file

@ -260,21 +260,21 @@ class MqttCommandTemplate:
def __init__(
self,
command_template: template.Template | None,
entity: Entity | HomeAssistant,
*,
hass: HomeAssistant | None = None,
entity: Entity | None = None,
) -> None:
"""Instantiate a command template."""
self._attr_command_template = command_template
if command_template is None:
return
if isinstance(entity, HomeAssistant):
command_template.hass = entity
return
self._entity = entity
self._variables = [ATTR_ENTITY_ID, ATTR_NAME]
command_template.hass = entity.hass
command_template.hass = hass
if entity:
command_template.hass = entity.hass
@callback
def async_render(
@ -303,9 +303,9 @@ class MqttCommandTemplate:
return value
values = {"value": value}
if hasattr(self, "_variables"):
for key in self._variables:
values[key] = getattr(self._entity, key)
if self._entity:
values[ATTR_ENTITY_ID] = self._entity.entity_id
values[ATTR_NAME] = self._entity.name
if variables is not None:
values.update(variables)
return _convert_outgoing_payload(
@ -589,7 +589,7 @@ async def async_setup_entry(hass, entry):
if payload_template is not None:
try:
payload = MqttCommandTemplate(
template.Template(payload_template), hass
template.Template(payload_template), hass=hass
).async_render()
except (template.jinja2.TemplateError, TemplateError) as exc:
_LOGGER.error(

View file

@ -151,7 +151,7 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
if value_template is not None:
value_template.hass = self.hass
self._command_template = MqttCommandTemplate(
self._config[CONF_COMMAND_TEMPLATE], self
self._config[CONF_COMMAND_TEMPLATE], entity=self
).async_render
async def _subscribe_topics(self):

View file

@ -378,7 +378,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
command_templates = {}
for key in COMMAND_TEMPLATE_KEYS:
command_templates[key] = MqttCommandTemplate(
config.get(key), self
config.get(key), entity=self
).async_render
self._command_templates = command_templates

View file

@ -289,7 +289,7 @@ class MqttCover(MqttEntity, CoverEntity):
value_template.hass = self.hass
self._set_position_template = MqttCommandTemplate(
self._config.get(CONF_SET_POSITION_TEMPLATE), self
self._config.get(CONF_SET_POSITION_TEMPLATE), entity=self
).async_render
get_position_template = self._config.get(CONF_GET_POSITION_TEMPLATE)
@ -297,7 +297,7 @@ class MqttCover(MqttEntity, CoverEntity):
get_position_template.hass = self.hass
self._set_tilt_template = MqttCommandTemplate(
self._config.get(CONF_TILT_COMMAND_TEMPLATE), self
self._config.get(CONF_TILT_COMMAND_TEMPLATE), entity=self
).async_render
tilt_status_template = self._config.get(CONF_TILT_STATUS_TEMPLATE)

View file

@ -333,7 +333,9 @@ class MqttFan(MqttEntity, FanEntity):
self._supported_features |= SUPPORT_PRESET_MODE
for key, tpl in self._command_templates.items():
self._command_templates[key] = MqttCommandTemplate(tpl, self).async_render
self._command_templates[key] = MqttCommandTemplate(
tpl, entity=self
).async_render
for key, tpl in self._value_templates.items():
if tpl is None:

View file

@ -238,7 +238,9 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
self._optimistic_mode = optimistic or self._topic[CONF_MODE_STATE_TOPIC] is None
for key, tpl in self._command_templates.items():
self._command_templates[key] = MqttCommandTemplate(tpl, self).async_render
self._command_templates[key] = MqttCommandTemplate(
tpl, entity=self
).async_render
for key, tpl in self._value_templates.items():
if tpl is None:

View file

@ -330,7 +330,9 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
for key in COMMAND_TEMPLATE_KEYS:
command_templates[key] = None
for key in COMMAND_TEMPLATE_KEYS & config.keys():
command_templates[key] = MqttCommandTemplate(config[key], self).async_render
command_templates[key] = MqttCommandTemplate(
config[key], entity=self
).async_render
self._command_templates = command_templates
optimistic = config[CONF_OPTIMISTIC]

View file

@ -139,7 +139,7 @@ class MqttNumber(MqttEntity, NumberEntity, RestoreEntity):
self._templates = {
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
config.get(CONF_COMMAND_TEMPLATE), self
config.get(CONF_COMMAND_TEMPLATE), entity=self
).async_render,
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
}

View file

@ -103,7 +103,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
self._templates = {
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
config.get(CONF_COMMAND_TEMPLATE), self
config.get(CONF_COMMAND_TEMPLATE), entity=self
).async_render,
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
}

View file

@ -160,7 +160,7 @@ async def test_publish(hass, mqtt_mock):
async def test_convert_outgoing_payload(hass):
"""Test the converting of outgoing MQTT payloads without template."""
command_template = mqtt.MqttCommandTemplate(None, hass)
command_template = mqtt.MqttCommandTemplate(None, hass=hass)
assert command_template.async_render(b"\xde\xad\xbe\xef") == b"\xde\xad\xbe\xef"
assert (
@ -181,13 +181,13 @@ async def test_command_template_value(hass):
variables = {"id": 1234, "some_var": "beer"}
# test rendering value
tpl = template.Template("{{ value + 1 }}", hass)
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass)
tpl = template.Template("{{ value + 1 }}", hass=hass)
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
assert cmd_tpl.async_render(4321) == "4322"
# test variables at rendering
tpl = template.Template("{{ some_var }}", hass)
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass)
tpl = template.Template("{{ some_var }}", hass=hass)
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
assert cmd_tpl.async_render(None, variables=variables) == "beer"