make hass and entity conditional parameters
This commit is contained in:
parent
e687cf777d
commit
cac2f8ac06
10 changed files with 31 additions and 25 deletions
|
@ -260,21 +260,21 @@ class MqttCommandTemplate:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
command_template: template.Template | None,
|
command_template: template.Template | None,
|
||||||
entity: Entity | HomeAssistant,
|
*,
|
||||||
|
hass: HomeAssistant | None = None,
|
||||||
|
entity: Entity | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Instantiate a command template."""
|
"""Instantiate a command template."""
|
||||||
self._attr_command_template = command_template
|
self._attr_command_template = command_template
|
||||||
if command_template is None:
|
if command_template is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(entity, HomeAssistant):
|
|
||||||
command_template.hass = entity
|
|
||||||
return
|
|
||||||
|
|
||||||
self._entity = entity
|
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
|
@callback
|
||||||
def async_render(
|
def async_render(
|
||||||
|
@ -303,9 +303,9 @@ class MqttCommandTemplate:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
values = {"value": value}
|
values = {"value": value}
|
||||||
if hasattr(self, "_variables"):
|
if self._entity:
|
||||||
for key in self._variables:
|
values[ATTR_ENTITY_ID] = self._entity.entity_id
|
||||||
values[key] = getattr(self._entity, key)
|
values[ATTR_NAME] = self._entity.name
|
||||||
if variables is not None:
|
if variables is not None:
|
||||||
values.update(variables)
|
values.update(variables)
|
||||||
return _convert_outgoing_payload(
|
return _convert_outgoing_payload(
|
||||||
|
@ -589,7 +589,7 @@ async def async_setup_entry(hass, entry):
|
||||||
if payload_template is not None:
|
if payload_template is not None:
|
||||||
try:
|
try:
|
||||||
payload = MqttCommandTemplate(
|
payload = MqttCommandTemplate(
|
||||||
template.Template(payload_template), hass
|
template.Template(payload_template), hass=hass
|
||||||
).async_render()
|
).async_render()
|
||||||
except (template.jinja2.TemplateError, TemplateError) as exc:
|
except (template.jinja2.TemplateError, TemplateError) as exc:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
|
|
@ -151,7 +151,7 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
|
||||||
if value_template is not None:
|
if value_template is not None:
|
||||||
value_template.hass = self.hass
|
value_template.hass = self.hass
|
||||||
self._command_template = MqttCommandTemplate(
|
self._command_template = MqttCommandTemplate(
|
||||||
self._config[CONF_COMMAND_TEMPLATE], self
|
self._config[CONF_COMMAND_TEMPLATE], entity=self
|
||||||
).async_render
|
).async_render
|
||||||
|
|
||||||
async def _subscribe_topics(self):
|
async def _subscribe_topics(self):
|
||||||
|
|
|
@ -378,7 +378,7 @@ class MqttClimate(MqttEntity, ClimateEntity):
|
||||||
command_templates = {}
|
command_templates = {}
|
||||||
for key in COMMAND_TEMPLATE_KEYS:
|
for key in COMMAND_TEMPLATE_KEYS:
|
||||||
command_templates[key] = MqttCommandTemplate(
|
command_templates[key] = MqttCommandTemplate(
|
||||||
config.get(key), self
|
config.get(key), entity=self
|
||||||
).async_render
|
).async_render
|
||||||
|
|
||||||
self._command_templates = command_templates
|
self._command_templates = command_templates
|
||||||
|
|
|
@ -289,7 +289,7 @@ class MqttCover(MqttEntity, CoverEntity):
|
||||||
value_template.hass = self.hass
|
value_template.hass = self.hass
|
||||||
|
|
||||||
self._set_position_template = MqttCommandTemplate(
|
self._set_position_template = MqttCommandTemplate(
|
||||||
self._config.get(CONF_SET_POSITION_TEMPLATE), self
|
self._config.get(CONF_SET_POSITION_TEMPLATE), entity=self
|
||||||
).async_render
|
).async_render
|
||||||
|
|
||||||
get_position_template = self._config.get(CONF_GET_POSITION_TEMPLATE)
|
get_position_template = self._config.get(CONF_GET_POSITION_TEMPLATE)
|
||||||
|
@ -297,7 +297,7 @@ class MqttCover(MqttEntity, CoverEntity):
|
||||||
get_position_template.hass = self.hass
|
get_position_template.hass = self.hass
|
||||||
|
|
||||||
self._set_tilt_template = MqttCommandTemplate(
|
self._set_tilt_template = MqttCommandTemplate(
|
||||||
self._config.get(CONF_TILT_COMMAND_TEMPLATE), self
|
self._config.get(CONF_TILT_COMMAND_TEMPLATE), entity=self
|
||||||
).async_render
|
).async_render
|
||||||
|
|
||||||
tilt_status_template = self._config.get(CONF_TILT_STATUS_TEMPLATE)
|
tilt_status_template = self._config.get(CONF_TILT_STATUS_TEMPLATE)
|
||||||
|
|
|
@ -333,7 +333,9 @@ class MqttFan(MqttEntity, FanEntity):
|
||||||
self._supported_features |= SUPPORT_PRESET_MODE
|
self._supported_features |= SUPPORT_PRESET_MODE
|
||||||
|
|
||||||
for key, tpl in self._command_templates.items():
|
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():
|
for key, tpl in self._value_templates.items():
|
||||||
if tpl is None:
|
if tpl is None:
|
||||||
|
|
|
@ -238,7 +238,9 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
||||||
self._optimistic_mode = optimistic or self._topic[CONF_MODE_STATE_TOPIC] is None
|
self._optimistic_mode = optimistic or self._topic[CONF_MODE_STATE_TOPIC] is None
|
||||||
|
|
||||||
for key, tpl in self._command_templates.items():
|
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():
|
for key, tpl in self._value_templates.items():
|
||||||
if tpl is None:
|
if tpl is None:
|
||||||
|
|
|
@ -330,7 +330,9 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
||||||
for key in COMMAND_TEMPLATE_KEYS:
|
for key in COMMAND_TEMPLATE_KEYS:
|
||||||
command_templates[key] = None
|
command_templates[key] = None
|
||||||
for key in COMMAND_TEMPLATE_KEYS & config.keys():
|
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
|
self._command_templates = command_templates
|
||||||
|
|
||||||
optimistic = config[CONF_OPTIMISTIC]
|
optimistic = config[CONF_OPTIMISTIC]
|
||||||
|
|
|
@ -139,7 +139,7 @@ class MqttNumber(MqttEntity, NumberEntity, RestoreEntity):
|
||||||
|
|
||||||
self._templates = {
|
self._templates = {
|
||||||
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
|
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
|
||||||
config.get(CONF_COMMAND_TEMPLATE), self
|
config.get(CONF_COMMAND_TEMPLATE), entity=self
|
||||||
).async_render,
|
).async_render,
|
||||||
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
|
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
|
||||||
}
|
}
|
||||||
|
|
|
@ -103,7 +103,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
|
||||||
|
|
||||||
self._templates = {
|
self._templates = {
|
||||||
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
|
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
|
||||||
config.get(CONF_COMMAND_TEMPLATE), self
|
config.get(CONF_COMMAND_TEMPLATE), entity=self
|
||||||
).async_render,
|
).async_render,
|
||||||
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
|
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,7 +160,7 @@ async def test_publish(hass, mqtt_mock):
|
||||||
|
|
||||||
async def test_convert_outgoing_payload(hass):
|
async def test_convert_outgoing_payload(hass):
|
||||||
"""Test the converting of outgoing MQTT payloads without template."""
|
"""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 command_template.async_render(b"\xde\xad\xbe\xef") == b"\xde\xad\xbe\xef"
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
|
@ -181,13 +181,13 @@ async def test_command_template_value(hass):
|
||||||
variables = {"id": 1234, "some_var": "beer"}
|
variables = {"id": 1234, "some_var": "beer"}
|
||||||
|
|
||||||
# test rendering value
|
# test rendering value
|
||||||
tpl = template.Template("{{ value + 1 }}", hass)
|
tpl = template.Template("{{ value + 1 }}", hass=hass)
|
||||||
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass)
|
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
|
||||||
assert cmd_tpl.async_render(4321) == "4322"
|
assert cmd_tpl.async_render(4321) == "4322"
|
||||||
|
|
||||||
# test variables at rendering
|
# test variables at rendering
|
||||||
tpl = template.Template("{{ some_var }}", hass)
|
tpl = template.Template("{{ some_var }}", hass=hass)
|
||||||
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass)
|
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
|
||||||
assert cmd_tpl.async_render(None, variables=variables) == "beer"
|
assert cmd_tpl.async_render(None, variables=variables) == "beer"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue