From 8c04e4c7a36a6419c77b7ffda6142a2b5859af41 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 30 Aug 2023 19:56:34 +0200 Subject: [PATCH] Add explicit test of template config entry setup (#99345) --- .../snapshots/test_binary_sensor.ambr | 26 +++++++++ .../template/snapshots/test_sensor.ambr | 28 ++++++++++ .../components/template/test_binary_sensor.py | 51 ++++++++++++++++++ tests/components/template/test_sensor.py | 54 ++++++++++++++++++- 4 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 tests/components/template/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/template/snapshots/test_sensor.ambr diff --git a/tests/components/template/snapshots/test_binary_sensor.ambr b/tests/components/template/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..2529021971a --- /dev/null +++ b/tests/components/template/snapshots/test_binary_sensor.ambr @@ -0,0 +1,26 @@ +# serializer version: 1 +# name: test_setup_config_entry[config_entry_extra_options0] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'My template', + }), + 'context': , + 'entity_id': 'binary_sensor.my_template', + 'last_changed': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_setup_config_entry[config_entry_extra_options1] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'My template', + }), + 'context': , + 'entity_id': 'binary_sensor.my_template', + 'last_changed': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/template/snapshots/test_sensor.ambr b/tests/components/template/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..7959959dfa9 --- /dev/null +++ b/tests/components/template/snapshots/test_sensor.ambr @@ -0,0 +1,28 @@ +# serializer version: 1 +# name: test_setup_config_entry[config_entry_extra_options0] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'My template', + }), + 'context': , + 'entity_id': 'sensor.my_template', + 'last_changed': , + 'last_updated': , + 'state': '30.0', + }) +# --- +# name: test_setup_config_entry[config_entry_extra_options1] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'My template', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.my_template', + 'last_changed': , + 'last_updated': , + 'state': '30.0', + }) +# --- diff --git a/tests/components/template/test_binary_sensor.py b/tests/components/template/test_binary_sensor.py index e43163f66fc..01c0f005716 100644 --- a/tests/components/template/test_binary_sensor.py +++ b/tests/components/template/test_binary_sensor.py @@ -5,6 +5,7 @@ from unittest.mock import patch from freezegun.api import FrozenDateTimeFactory import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant import setup from homeassistant.components import binary_sensor, template @@ -23,6 +24,7 @@ from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from tests.common import ( + MockConfigEntry, assert_setup_component, async_fire_time_changed, mock_restore_cache, @@ -123,6 +125,55 @@ async def test_setup(hass: HomeAssistant, start_ha, entity_id) -> None: assert state.attributes["device_class"] == "motion" +@pytest.mark.parametrize( + "config_entry_extra_options", + [ + {}, + {"device_class": "battery"}, + ], +) +async def test_setup_config_entry( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + config_entry_extra_options: dict[str, str], +) -> None: + """Test the config flow.""" + state_template = ( + "{{ states('binary_sensor.one') == 'on' or " + " states('binary_sensor.two') == 'on' }}" + ) + input_entities = ["one", "two"] + input_states = {"one": "on", "two": "off"} + template_type = binary_sensor.DOMAIN + + for input_entity in input_entities: + hass.states.async_set( + f"{template_type}.{input_entity}", + input_states[input_entity], + {}, + ) + + template_config_entry = MockConfigEntry( + data={}, + domain=template.DOMAIN, + options={ + "name": "My template", + "state": state_template, + "template_type": template_type, + } + | config_entry_extra_options, + title="My template", + ) + template_config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(template_config_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get(f"{template_type}.my_template") + assert state is not None + assert state == snapshot + + @pytest.mark.parametrize("count", [0]) @pytest.mark.parametrize( ("config", "domain"), diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index 5eca8330789..47e307bc6aa 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -4,9 +4,10 @@ from datetime import timedelta from unittest.mock import patch import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.bootstrap import async_from_config_dict -from homeassistant.components import sensor +from homeassistant.components import sensor, template from homeassistant.const import ( ATTR_ENTITY_PICTURE, ATTR_ICON, @@ -25,6 +26,7 @@ from homeassistant.setup import ATTR_COMPONENT, async_setup_component import homeassistant.util.dt as dt_util from tests.common import ( + MockConfigEntry, assert_setup_component, async_fire_time_changed, mock_restore_cache_with_extra_data, @@ -33,6 +35,56 @@ from tests.common import ( TEST_NAME = "sensor.test_template_sensor" +@pytest.mark.parametrize( + "config_entry_extra_options", + [ + {}, + { + "device_class": "battery", + "state_class": "measurement", + "unit_of_measurement": "%", + }, + ], +) +async def test_setup_config_entry( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + config_entry_extra_options: dict[str, str], +) -> None: + """Test the config flow.""" + state_template = "{{ float(states('sensor.one')) + float(states('sensor.two')) }}" + input_entities = ["one", "two"] + input_states = {"one": "10", "two": "20"} + template_type = sensor.DOMAIN + + for input_entity in input_entities: + hass.states.async_set( + f"{template_type}.{input_entity}", + input_states[input_entity], + {}, + ) + + template_config_entry = MockConfigEntry( + data={}, + domain=template.DOMAIN, + options={ + "name": "My template", + "state": state_template, + "template_type": template_type, + } + | config_entry_extra_options, + title="My template", + ) + template_config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(template_config_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get(f"{template_type}.my_template") + assert state is not None + assert state == snapshot + + @pytest.mark.parametrize(("count", "domain"), [(1, sensor.DOMAIN)]) @pytest.mark.parametrize( "config",