mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Cleanup unneeded assignment of hass property on MQTT Template objects (#123706)
* Cleanup unneeded assignment of hass property on MQTT Template objects * Commented out code and unneeded checks * Consistent assign hass to Template in mqtt tests * Remove unused hass attribute * Missed line
This commit is contained in:
parent
74a09073c2
commit
21987a67e7
@ -303,8 +303,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
# has been deprecated with HA Core 2024.8.0
|
# has been deprecated with HA Core 2024.8.0
|
||||||
# and will be removed with HA Core 2025.2.0
|
# and will be removed with HA Core 2025.2.0
|
||||||
rendered_topic: Any = MqttCommandTemplate(
|
rendered_topic: Any = MqttCommandTemplate(
|
||||||
template.Template(msg_topic_template),
|
template.Template(msg_topic_template, hass),
|
||||||
hass=hass,
|
|
||||||
).async_render()
|
).async_render()
|
||||||
ir.async_create_issue(
|
ir.async_create_issue(
|
||||||
hass,
|
hass,
|
||||||
@ -353,7 +352,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
payload = MqttCommandTemplate(
|
payload = MqttCommandTemplate(
|
||||||
template.Template(payload_template), hass=hass
|
template.Template(payload_template, hass)
|
||||||
).async_render()
|
).async_render()
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
@ -12,7 +12,7 @@ import logging
|
|||||||
from typing import TYPE_CHECKING, Any, TypedDict
|
from typing import TYPE_CHECKING, Any, TypedDict
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME, Platform
|
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME, Platform
|
||||||
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
from homeassistant.core import CALLBACK_TYPE, callback
|
||||||
from homeassistant.exceptions import ServiceValidationError, TemplateError
|
from homeassistant.exceptions import ServiceValidationError, TemplateError
|
||||||
from homeassistant.helpers import template
|
from homeassistant.helpers import template
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
@ -159,22 +159,13 @@ class MqttCommandTemplate:
|
|||||||
self,
|
self,
|
||||||
command_template: template.Template | None,
|
command_template: template.Template | None,
|
||||||
*,
|
*,
|
||||||
hass: HomeAssistant | None = None,
|
|
||||||
entity: Entity | None = None,
|
entity: Entity | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Instantiate a command template."""
|
"""Instantiate a command template."""
|
||||||
self._template_state: template.TemplateStateFromEntityId | None = None
|
self._template_state: template.TemplateStateFromEntityId | None = None
|
||||||
self._command_template = command_template
|
self._command_template = command_template
|
||||||
if command_template is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
self._entity = entity
|
self._entity = entity
|
||||||
|
|
||||||
command_template.hass = hass
|
|
||||||
|
|
||||||
if entity:
|
|
||||||
command_template.hass = entity.hass
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_render(
|
def async_render(
|
||||||
self,
|
self,
|
||||||
@ -270,7 +261,6 @@ class MqttValueTemplate:
|
|||||||
self,
|
self,
|
||||||
value_template: template.Template | None,
|
value_template: template.Template | None,
|
||||||
*,
|
*,
|
||||||
hass: HomeAssistant | None = None,
|
|
||||||
entity: Entity | None = None,
|
entity: Entity | None = None,
|
||||||
config_attributes: TemplateVarsType = None,
|
config_attributes: TemplateVarsType = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -278,15 +268,8 @@ class MqttValueTemplate:
|
|||||||
self._template_state: template.TemplateStateFromEntityId | None = None
|
self._template_state: template.TemplateStateFromEntityId | None = None
|
||||||
self._value_template = value_template
|
self._value_template = value_template
|
||||||
self._config_attributes = config_attributes
|
self._config_attributes = config_attributes
|
||||||
if value_template is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
value_template.hass = hass
|
|
||||||
self._entity = entity
|
self._entity = entity
|
||||||
|
|
||||||
if entity:
|
|
||||||
value_template.hass = entity.hass
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_render_with_possible_json_value(
|
def async_render_with_possible_json_value(
|
||||||
self,
|
self,
|
||||||
|
@ -118,8 +118,7 @@ class MQTTTagScanner(MqttDiscoveryDeviceUpdateMixin):
|
|||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._sub_state: dict[str, EntitySubscription] | None = None
|
self._sub_state: dict[str, EntitySubscription] | None = None
|
||||||
self._value_template = MqttValueTemplate(
|
self._value_template = MqttValueTemplate(
|
||||||
config.get(CONF_VALUE_TEMPLATE),
|
config.get(CONF_VALUE_TEMPLATE)
|
||||||
hass=self.hass,
|
|
||||||
).async_render_with_possible_json_value
|
).async_render_with_possible_json_value
|
||||||
|
|
||||||
MqttDiscoveryDeviceUpdateMixin.__init__(
|
MqttDiscoveryDeviceUpdateMixin.__init__(
|
||||||
@ -136,8 +135,7 @@ class MQTTTagScanner(MqttDiscoveryDeviceUpdateMixin):
|
|||||||
return
|
return
|
||||||
self._config = config
|
self._config = config
|
||||||
self._value_template = MqttValueTemplate(
|
self._value_template = MqttValueTemplate(
|
||||||
config.get(CONF_VALUE_TEMPLATE),
|
config.get(CONF_VALUE_TEMPLATE)
|
||||||
hass=self.hass,
|
|
||||||
).async_render_with_possible_json_value
|
).async_render_with_possible_json_value
|
||||||
update_device(self.hass, self._config_entry, config)
|
update_device(self.hass, self._config_entry, config)
|
||||||
await self.subscribe_topics()
|
await self.subscribe_topics()
|
||||||
|
@ -60,10 +60,10 @@ async def async_attach_trigger(
|
|||||||
trigger_data: TriggerData = trigger_info["trigger_data"]
|
trigger_data: TriggerData = trigger_info["trigger_data"]
|
||||||
command_template: Callable[
|
command_template: Callable[
|
||||||
[PublishPayloadType, TemplateVarsType], PublishPayloadType
|
[PublishPayloadType, TemplateVarsType], PublishPayloadType
|
||||||
] = MqttCommandTemplate(config.get(CONF_PAYLOAD), hass=hass).async_render
|
] = MqttCommandTemplate(config.get(CONF_PAYLOAD)).async_render
|
||||||
value_template: Callable[[ReceivePayloadType, str], ReceivePayloadType]
|
value_template: Callable[[ReceivePayloadType, str], ReceivePayloadType]
|
||||||
value_template = MqttValueTemplate(
|
value_template = MqttValueTemplate(
|
||||||
config.get(CONF_VALUE_TEMPLATE), hass=hass
|
config.get(CONF_VALUE_TEMPLATE)
|
||||||
).async_render_with_possible_json_value
|
).async_render_with_possible_json_value
|
||||||
encoding: str | None = config[CONF_ENCODING] or None
|
encoding: str | None = config[CONF_ENCODING] or None
|
||||||
qos: int = config[CONF_QOS]
|
qos: int = config[CONF_QOS]
|
||||||
@ -75,7 +75,6 @@ async def async_attach_trigger(
|
|||||||
wanted_payload = command_template(None, variables)
|
wanted_payload = command_template(None, variables)
|
||||||
|
|
||||||
topic_template: Template = config[CONF_TOPIC]
|
topic_template: Template = config[CONF_TOPIC]
|
||||||
topic_template.hass = hass
|
|
||||||
topic = topic_template.async_render(variables, limited=True, parse_result=False)
|
topic = topic_template.async_render(variables, limited=True, parse_result=False)
|
||||||
mqtt.util.valid_subscribe_topic(topic)
|
mqtt.util.valid_subscribe_topic(topic)
|
||||||
|
|
||||||
|
@ -225,7 +225,7 @@ async def test_publish(
|
|||||||
|
|
||||||
async def test_convert_outgoing_payload(hass: HomeAssistant) -> None:
|
async def test_convert_outgoing_payload(hass: HomeAssistant) -> None:
|
||||||
"""Test the converting of outgoing MQTT payloads without template."""
|
"""Test the converting of outgoing MQTT payloads without template."""
|
||||||
command_template = mqtt.MqttCommandTemplate(None, hass=hass)
|
command_template = mqtt.MqttCommandTemplate(None)
|
||||||
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 (
|
||||||
command_template.async_render("b'\\xde\\xad\\xbe\\xef'")
|
command_template.async_render("b'\\xde\\xad\\xbe\\xef'")
|
||||||
|
@ -89,12 +89,12 @@ async def test_command_template_value(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
# test rendering value
|
# test rendering value
|
||||||
tpl = template.Template("{{ value + 1 }}", hass=hass)
|
tpl = template.Template("{{ value + 1 }}", hass=hass)
|
||||||
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
|
cmd_tpl = mqtt.MqttCommandTemplate(tpl)
|
||||||
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=hass)
|
tpl = template.Template("{{ some_var }}", hass=hass)
|
||||||
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
|
cmd_tpl = mqtt.MqttCommandTemplate(tpl)
|
||||||
assert cmd_tpl.async_render(None, variables=variables) == "beer"
|
assert cmd_tpl.async_render(None, variables=variables) == "beer"
|
||||||
|
|
||||||
|
|
||||||
@ -161,8 +161,8 @@ async def test_command_template_variables(
|
|||||||
|
|
||||||
async def test_command_template_fails(hass: HomeAssistant) -> None:
|
async def test_command_template_fails(hass: HomeAssistant) -> None:
|
||||||
"""Test the exception handling of an MQTT command template."""
|
"""Test the exception handling of an MQTT command template."""
|
||||||
tpl = template.Template("{{ value * 2 }}")
|
tpl = template.Template("{{ value * 2 }}", hass=hass)
|
||||||
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
|
cmd_tpl = mqtt.MqttCommandTemplate(tpl)
|
||||||
with pytest.raises(MqttCommandTemplateException) as exc:
|
with pytest.raises(MqttCommandTemplateException) as exc:
|
||||||
cmd_tpl.async_render(None)
|
cmd_tpl.async_render(None)
|
||||||
assert "unsupported operand type(s) for *: 'NoneType' and 'int'" in str(exc.value)
|
assert "unsupported operand type(s) for *: 'NoneType' and 'int'" in str(exc.value)
|
||||||
@ -174,13 +174,13 @@ async def test_value_template_value(hass: HomeAssistant) -> None:
|
|||||||
variables = {"id": 1234, "some_var": "beer"}
|
variables = {"id": 1234, "some_var": "beer"}
|
||||||
|
|
||||||
# test rendering value
|
# test rendering value
|
||||||
tpl = template.Template("{{ value_json.id }}")
|
tpl = template.Template("{{ value_json.id }}", hass=hass)
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
|
val_tpl = mqtt.MqttValueTemplate(tpl)
|
||||||
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
||||||
|
|
||||||
# test variables at rendering
|
# test variables at rendering
|
||||||
tpl = template.Template("{{ value_json.id }} {{ some_var }} {{ code }}")
|
tpl = template.Template("{{ value_json.id }} {{ some_var }} {{ code }}", hass=hass)
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass, config_attributes={"code": 1234})
|
val_tpl = mqtt.MqttValueTemplate(tpl, config_attributes={"code": 1234})
|
||||||
assert (
|
assert (
|
||||||
val_tpl.async_render_with_possible_json_value(
|
val_tpl.async_render_with_possible_json_value(
|
||||||
'{"id": 4321}', variables=variables
|
'{"id": 4321}', variables=variables
|
||||||
@ -189,8 +189,8 @@ async def test_value_template_value(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# test with default value if an error occurs due to an invalid template
|
# test with default value if an error occurs due to an invalid template
|
||||||
tpl = template.Template("{{ value_json.id | as_datetime }}")
|
tpl = template.Template("{{ value_json.id | as_datetime }}", hass=hass)
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
|
val_tpl = mqtt.MqttValueTemplate(tpl)
|
||||||
assert (
|
assert (
|
||||||
val_tpl.async_render_with_possible_json_value('{"otherid": 4321}', "my default")
|
val_tpl.async_render_with_possible_json_value('{"otherid": 4321}', "my default")
|
||||||
== "my default"
|
== "my default"
|
||||||
@ -200,19 +200,19 @@ async def test_value_template_value(hass: HomeAssistant) -> None:
|
|||||||
entity = Entity()
|
entity = Entity()
|
||||||
entity.hass = hass
|
entity.hass = hass
|
||||||
entity.entity_id = "select.test"
|
entity.entity_id = "select.test"
|
||||||
tpl = template.Template("{{ value_json.id }}")
|
tpl = template.Template("{{ value_json.id }}", hass=hass)
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, entity=entity)
|
val_tpl = mqtt.MqttValueTemplate(tpl, entity=entity)
|
||||||
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
||||||
|
|
||||||
# test this object in a template
|
# test this object in a template
|
||||||
tpl2 = template.Template("{{ this.entity_id }}")
|
tpl2 = template.Template("{{ this.entity_id }}", hass=hass)
|
||||||
val_tpl2 = mqtt.MqttValueTemplate(tpl2, entity=entity)
|
val_tpl2 = mqtt.MqttValueTemplate(tpl2, entity=entity)
|
||||||
assert val_tpl2.async_render_with_possible_json_value("bla") == "select.test"
|
assert val_tpl2.async_render_with_possible_json_value("bla") == "select.test"
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.helpers.template.TemplateStateFromEntityId", MagicMock()
|
"homeassistant.helpers.template.TemplateStateFromEntityId", MagicMock()
|
||||||
) as template_state_calls:
|
) as template_state_calls:
|
||||||
tpl3 = template.Template("{{ this.entity_id }}")
|
tpl3 = template.Template("{{ this.entity_id }}", hass=hass)
|
||||||
val_tpl3 = mqtt.MqttValueTemplate(tpl3, entity=entity)
|
val_tpl3 = mqtt.MqttValueTemplate(tpl3, entity=entity)
|
||||||
val_tpl3.async_render_with_possible_json_value("call1")
|
val_tpl3.async_render_with_possible_json_value("call1")
|
||||||
val_tpl3.async_render_with_possible_json_value("call2")
|
val_tpl3.async_render_with_possible_json_value("call2")
|
||||||
@ -223,8 +223,8 @@ async def test_value_template_fails(hass: HomeAssistant) -> None:
|
|||||||
"""Test the rendering of MQTT value template fails."""
|
"""Test the rendering of MQTT value template fails."""
|
||||||
entity = MockEntity(entity_id="sensor.test")
|
entity = MockEntity(entity_id="sensor.test")
|
||||||
entity.hass = hass
|
entity.hass = hass
|
||||||
tpl = template.Template("{{ value_json.some_var * 2 }}")
|
tpl = template.Template("{{ value_json.some_var * 2 }}", hass=hass)
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass, entity=entity)
|
val_tpl = mqtt.MqttValueTemplate(tpl, entity=entity)
|
||||||
with pytest.raises(MqttValueTemplateException) as exc:
|
with pytest.raises(MqttValueTemplateException) as exc:
|
||||||
val_tpl.async_render_with_possible_json_value('{"some_var": null }')
|
val_tpl.async_render_with_possible_json_value('{"some_var": null }')
|
||||||
assert str(exc.value) == (
|
assert str(exc.value) == (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user