diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index a314fcb07ef..0614b3c3b4f 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -428,7 +428,7 @@ class Template: # output; use the original render instead of the evaluated one. if not isinstance(result, str): return result - except (ValueError, SyntaxError, MemoryError): + except (ValueError, TypeError, SyntaxError, MemoryError): pass return render_result diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 7a833a72d51..39ea8bc92ec 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -1502,6 +1502,7 @@ async def test_debug_info_qos_retain(hass, mqtt_mock): async def test_publish_json_from_template(hass, mqtt_mock): """Test the publishing of call to services.""" test_str = "{'valid': 'python', 'invalid': 'json'}" + test_str_tpl = "{'valid': '{{ \"python\" }}', 'invalid': 'json'}" await async_setup_component( hass, @@ -1511,13 +1512,16 @@ async def test_publish_json_from_template(hass, mqtt_mock): "test_script_payload": { "sequence": { "service": "mqtt.publish", - "data": {"topic": "test-topic", "payload": test_str}, + "data": {"topic": "test-topic", "payload": test_str_tpl}, } }, "test_script_payload_template": { "sequence": { "service": "mqtt.publish", - "data": {"topic": "test-topic", "payload_template": test_str}, + "data": { + "topic": "test-topic", + "payload_template": test_str_tpl, + }, } }, } diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 856fc1495e5..9ac546c6a75 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -2698,3 +2698,13 @@ async def test_result_wrappers(hass): assert result == native assert result.render_result == text schema(result) # should not raise + + +async def test_parse_result(hass): + """Test parse result.""" + for tpl, result in ( + ('{{ "{{}}" }}', "{{}}"), + ("not-something", "not-something"), + ("2a", "2a"), + ): + assert template.Template(tpl, hass).async_render() == result