diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index de30207fe7b..36f217e0bd9 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -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( diff --git a/homeassistant/components/mqtt/alarm_control_panel.py b/homeassistant/components/mqtt/alarm_control_panel.py index 6de096520a9..2a2c440e3ca 100644 --- a/homeassistant/components/mqtt/alarm_control_panel.py +++ b/homeassistant/components/mqtt/alarm_control_panel.py @@ -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): diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index 770de1779ba..1edea7a62a6 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -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 diff --git a/homeassistant/components/mqtt/cover.py b/homeassistant/components/mqtt/cover.py index 9875358d9de..9a950c8e44d 100644 --- a/homeassistant/components/mqtt/cover.py +++ b/homeassistant/components/mqtt/cover.py @@ -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) diff --git a/homeassistant/components/mqtt/fan.py b/homeassistant/components/mqtt/fan.py index fe3d432d00f..1837eb6a28f 100644 --- a/homeassistant/components/mqtt/fan.py +++ b/homeassistant/components/mqtt/fan.py @@ -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: diff --git a/homeassistant/components/mqtt/humidifier.py b/homeassistant/components/mqtt/humidifier.py index 4a832030ea5..74de601134c 100644 --- a/homeassistant/components/mqtt/humidifier.py +++ b/homeassistant/components/mqtt/humidifier.py @@ -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: diff --git a/homeassistant/components/mqtt/light/schema_basic.py b/homeassistant/components/mqtt/light/schema_basic.py index b9a4d8cae12..7e6c34a33eb 100644 --- a/homeassistant/components/mqtt/light/schema_basic.py +++ b/homeassistant/components/mqtt/light/schema_basic.py @@ -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] diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index a5d7f3636f1..11d714361a9 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -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), } diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 3fb77b002c8..9899860c86d 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -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), } diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index bb49d15022e..b2d468a66b8 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -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"