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:
Jan Bouwhuis
2024-08-12 19:20:21 +02:00
committed by GitHub
parent 74a09073c2
commit 21987a67e7
6 changed files with 23 additions and 44 deletions

View File

@@ -89,12 +89,12 @@ async def test_command_template_value(hass: HomeAssistant) -> None:
# test rendering value
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"
# test variables at rendering
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"
@@ -161,8 +161,8 @@ async def test_command_template_variables(
async def test_command_template_fails(hass: HomeAssistant) -> None:
"""Test the exception handling of an MQTT command template."""
tpl = template.Template("{{ value * 2 }}")
cmd_tpl = mqtt.MqttCommandTemplate(tpl, hass=hass)
tpl = template.Template("{{ value * 2 }}", hass=hass)
cmd_tpl = mqtt.MqttCommandTemplate(tpl)
with pytest.raises(MqttCommandTemplateException) as exc:
cmd_tpl.async_render(None)
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"}
# test rendering value
tpl = template.Template("{{ value_json.id }}")
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
tpl = template.Template("{{ value_json.id }}", hass=hass)
val_tpl = mqtt.MqttValueTemplate(tpl)
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
# test variables at rendering
tpl = template.Template("{{ value_json.id }} {{ some_var }} {{ code }}")
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass, config_attributes={"code": 1234})
tpl = template.Template("{{ value_json.id }} {{ some_var }} {{ code }}", hass=hass)
val_tpl = mqtt.MqttValueTemplate(tpl, config_attributes={"code": 1234})
assert (
val_tpl.async_render_with_possible_json_value(
'{"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
tpl = template.Template("{{ value_json.id | as_datetime }}")
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
tpl = template.Template("{{ value_json.id | as_datetime }}", hass=hass)
val_tpl = mqtt.MqttValueTemplate(tpl)
assert (
val_tpl.async_render_with_possible_json_value('{"otherid": 4321}', "my default")
== "my default"
@@ -200,19 +200,19 @@ async def test_value_template_value(hass: HomeAssistant) -> None:
entity = Entity()
entity.hass = hass
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)
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
# 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)
assert val_tpl2.async_render_with_possible_json_value("bla") == "select.test"
with patch(
"homeassistant.helpers.template.TemplateStateFromEntityId", MagicMock()
) 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.async_render_with_possible_json_value("call1")
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."""
entity = MockEntity(entity_id="sensor.test")
entity.hass = hass
tpl = template.Template("{{ value_json.some_var * 2 }}")
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass, entity=entity)
tpl = template.Template("{{ value_json.some_var * 2 }}", hass=hass)
val_tpl = mqtt.MqttValueTemplate(tpl, entity=entity)
with pytest.raises(MqttValueTemplateException) as exc:
val_tpl.async_render_with_possible_json_value('{"some_var": null }')
assert str(exc.value) == (