From 935c2df7cbd9c2947fec77015e5b3ee18cd20ad7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Oct 2020 10:08:57 -0500 Subject: [PATCH] Add a test for template entities that reuse the template (#42404) --- tests/components/template/test_sensor.py | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index 681794e910f..c39fb474bca 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -946,3 +946,40 @@ async def test_self_referencing_icon_with_no_loop(hass, caplog): assert state.state == "extreme" assert state.attributes[ATTR_ICON] == "mdi:hazard-lights" assert "Template loop detected" not in caplog.text + + +async def test_duplicate_templates(hass): + """Test template entity where the value and friendly name as the same template.""" + hass.states.async_set("sensor.test_state", "Abc") + + with assert_setup_component(1, sensor.DOMAIN): + assert await async_setup_component( + hass, + sensor.DOMAIN, + { + "sensor": { + "platform": "template", + "sensors": { + "test_template_sensor": { + "value_template": "{{ states.sensor.test_state.state }}", + "friendly_name_template": "{{ states.sensor.test_state.state }}", + } + }, + } + }, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + state = hass.states.get("sensor.test_template_sensor") + assert state.attributes["friendly_name"] == "Abc" + assert state.state == "Abc" + + hass.states.async_set("sensor.test_state", "Def") + await hass.async_block_till_done() + + state = hass.states.get("sensor.test_template_sensor") + assert state.attributes["friendly_name"] == "Def" + assert state.state == "Def"