diff --git a/tests/components/template/conftest.py b/tests/components/template/conftest.py index e2168d0925e..5ccc9e6479a 100644 --- a/tests/components/template/conftest.py +++ b/tests/components/template/conftest.py @@ -25,3 +25,9 @@ async def start_ha(hass, count, domain, config, caplog): await hass.async_block_till_done() await hass.async_start() await hass.async_block_till_done() + + +@pytest.fixture +async def caplog_setup_text(caplog): + """Return setup log of integration.""" + yield caplog.text diff --git a/tests/components/template/test_binary_sensor.py b/tests/components/template/test_binary_sensor.py index 92b8d6f773f..98d76776242 100644 --- a/tests/components/template/test_binary_sensor.py +++ b/tests/components/template/test_binary_sensor.py @@ -431,13 +431,12 @@ async def test_availability_template(hass, start_ha): }, ], ) -async def test_invalid_attribute_template(hass, caplog, start_ha): +async def test_invalid_attribute_template(hass, start_ha, caplog_setup_text): """Test that errors are logged if rendering template fails.""" hass.states.async_set("binary_sensor.test_sensor", "true") assert len(hass.states.async_all()) == 2 - text = str([x.getMessage() for x in caplog.get_records("setup")]) - assert ("test_attribute") in text - assert ("TemplateError") in text + assert ("test_attribute") in caplog_setup_text + assert ("TemplateError") in caplog_setup_text @pytest.mark.parametrize("count,domain", [(1, binary_sensor.DOMAIN)]) @@ -458,13 +457,12 @@ async def test_invalid_attribute_template(hass, caplog, start_ha): ], ) async def test_invalid_availability_template_keeps_component_available( - hass, caplog, start_ha + hass, start_ha, caplog_setup_text ): """Test that an invalid availability keeps the device available.""" assert hass.states.get("binary_sensor.my_sensor").state != STATE_UNAVAILABLE - text = str([x.getMessage() for x in caplog.get_records("setup")]) - assert ("UndefinedError: \\'x\\' is undefined") in text + assert "UndefinedError: 'x' is undefined" in caplog_setup_text async def test_no_update_template_match_all(hass, caplog): diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index 4c45ff1b150..242ac09d3d0 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -381,7 +381,7 @@ async def test_available_template_with_entities(hass, start_ha): }, ], ) -async def test_invalid_attribute_template(hass, caplog, start_ha): +async def test_invalid_attribute_template(hass, caplog, start_ha, caplog_setup_text): """Test that errors are logged if rendering template fails.""" hass.states.async_set("sensor.test_sensor", "startup") await hass.async_block_till_done() @@ -390,9 +390,7 @@ async def test_invalid_attribute_template(hass, caplog, start_ha): hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() await hass.helpers.entity_component.async_update_entity("sensor.invalid_template") - - messages = str([x.message for x in caplog.get_records("setup")]) - assert "TemplateError" in messages + assert "TemplateError" in caplog_setup_text assert "test_attribute" in caplog.text @@ -414,12 +412,11 @@ async def test_invalid_attribute_template(hass, caplog, start_ha): ], ) async def test_invalid_availability_template_keeps_component_available( - hass, caplog, start_ha + hass, start_ha, caplog_setup_text ): """Test that an invalid availability keeps the device available.""" assert hass.states.get("sensor.my_sensor").state != STATE_UNAVAILABLE - messages = str([x.message for x in caplog.get_records("setup")]) - assert "UndefinedError: \\'x\\' is undefined" in messages + assert "UndefinedError: 'x' is undefined" in caplog_setup_text async def test_no_template_match_all(hass, caplog): @@ -624,14 +621,12 @@ async def test_sun_renders_once_per_sensor(hass, start_ha): }, ], ) -async def test_self_referencing_sensor_loop(hass, caplog, start_ha): +async def test_self_referencing_sensor_loop(hass, start_ha, caplog_setup_text): """Test a self referencing sensor does not loop forever.""" assert len(hass.states.async_all()) == 1 await hass.async_block_till_done() await hass.async_block_till_done() - messages = str([x.message for x in caplog.get_records("setup")]) - assert "Template loop detected" in messages - + assert "Template loop detected" in caplog_setup_text assert int(hass.states.get("sensor.test").state) == 2 await hass.async_block_till_done() assert int(hass.states.get("sensor.test").state) == 2 @@ -654,13 +649,14 @@ async def test_self_referencing_sensor_loop(hass, caplog, start_ha): }, ], ) -async def test_self_referencing_sensor_with_icon_loop(hass, caplog, start_ha): +async def test_self_referencing_sensor_with_icon_loop( + hass, start_ha, caplog_setup_text +): """Test a self referencing sensor loops forever with a valid self referencing icon.""" assert len(hass.states.async_all()) == 1 await hass.async_block_till_done() await hass.async_block_till_done() - messages = str([x.message for x in caplog.get_records("setup")]) - assert "Template loop detected" in messages + assert "Template loop detected" in caplog_setup_text state = hass.states.get("sensor.test") assert int(state.state) == 3 @@ -689,14 +685,13 @@ async def test_self_referencing_sensor_with_icon_loop(hass, caplog, start_ha): ], ) async def test_self_referencing_sensor_with_icon_and_picture_entity_loop( - hass, caplog, start_ha + hass, start_ha, caplog_setup_text ): """Test a self referencing sensor loop forevers with a valid self referencing icon.""" assert len(hass.states.async_all()) == 1 await hass.async_block_till_done() await hass.async_block_till_done() - messages = str([x.message for x in caplog.get_records("setup")]) - assert "Template loop detected" in messages + assert "Template loop detected" in caplog_setup_text state = hass.states.get("sensor.test") assert int(state.state) == 4 @@ -724,7 +719,7 @@ async def test_self_referencing_sensor_with_icon_and_picture_entity_loop( }, ], ) -async def test_self_referencing_entity_picture_loop(hass, caplog, start_ha): +async def test_self_referencing_entity_picture_loop(hass, start_ha, caplog_setup_text): """Test a self referencing sensor does not loop forever with a looping self referencing entity picture.""" assert len(hass.states.async_all()) == 1 next_time = dt_util.utcnow() + timedelta(seconds=1.2) @@ -735,8 +730,7 @@ async def test_self_referencing_entity_picture_loop(hass, caplog, start_ha): await hass.async_block_till_done() await hass.async_block_till_done() - messages = str([x.message for x in caplog.get_records("setup")]) - assert "Template loop detected" in messages + assert "Template loop detected" in caplog_setup_text state = hass.states.get("sensor.test") assert int(state.state) == 1 @@ -1006,14 +1000,13 @@ async def test_trigger_entity_render_error(hass, start_ha): }, ], ) -async def test_trigger_not_allowed_platform_config(hass, caplog, start_ha): +async def test_trigger_not_allowed_platform_config(hass, start_ha, caplog_setup_text): """Test we throw a helpful warning if a trigger is configured in platform config.""" state = hass.states.get(TEST_NAME) assert state is None - messages = str([x.message for x in caplog.get_records("setup")]) assert ( "You can only add triggers to template entities if they are defined under `template:`." - in messages + in caplog_setup_text ) diff --git a/tests/components/template/test_vacuum.py b/tests/components/template/test_vacuum.py index 6e0252845d1..2bd6063b6ef 100644 --- a/tests/components/template/test_vacuum.py +++ b/tests/components/template/test_vacuum.py @@ -207,12 +207,11 @@ async def test_available_template_with_entities(hass, start_ha): ], ) async def test_invalid_availability_template_keeps_component_available( - hass, caplog, start_ha + hass, start_ha, caplog_setup_text ): """Test that an invalid availability keeps the device available.""" assert hass.states.get("vacuum.test_template_vacuum") != STATE_UNAVAILABLE - text = str([x.getMessage() for x in caplog.get_records("setup")]) - assert ("UndefinedError: \\'x\\' is undefined") in text + assert "UndefinedError: 'x' is undefined" in caplog_setup_text @pytest.mark.parametrize( @@ -275,13 +274,11 @@ async def test_attribute_templates(hass, start_ha): ) ], ) -async def test_invalid_attribute_template(hass, caplog, start_ha): +async def test_invalid_attribute_template(hass, start_ha, caplog_setup_text): """Test that errors are logged if rendering template fails.""" assert len(hass.states.async_all()) == 1 - - text = str([x.getMessage() for x in caplog.get_records("setup")]) - assert "test_attribute" in text - assert "TemplateError" in text + assert "test_attribute" in caplog_setup_text + assert "TemplateError" in caplog_setup_text @pytest.mark.parametrize(