From 3d212b868ef746181d85a93ee430f4fc23f309d6 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 10 Mar 2022 17:23:16 +0100 Subject: [PATCH] Address review comments on switch_as_x tests (#67951) * Address review comments on switch_as_x tests * Correct test * Move test instead of duplicating it --- tests/components/switch_as_x/test_init.py | 27 ++++++++++++ tests/components/switch_as_x/test_light.py | 49 +++++++++++++--------- 2 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 tests/components/switch_as_x/test_init.py diff --git a/tests/components/switch_as_x/test_init.py b/tests/components/switch_as_x/test_init.py new file mode 100644 index 00000000000..5797c66fab5 --- /dev/null +++ b/tests/components/switch_as_x/test_init.py @@ -0,0 +1,27 @@ +"""Tests for the Switch as X.""" +import pytest + +from homeassistant.components.switch_as_x import DOMAIN +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +@pytest.mark.parametrize("target_domain", ("light",)) +async def test_config_entry_unregistered_uuid(hass: HomeAssistant, target_domain): + """Test light switch setup from config entry with unknown entity registry id.""" + fake_uuid = "a266a680b608c32770e6c45bfe6b8411" + + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={"entity_id": fake_uuid, "target_domain": target_domain}, + title="ABC", + ) + + config_entry.add_to_hass(hass) + + assert not await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 0 diff --git a/tests/components/switch_as_x/test_light.py b/tests/components/switch_as_x/test_light.py index 26b6199cbc6..9a480776510 100644 --- a/tests/components/switch_as_x/test_light.py +++ b/tests/components/switch_as_x/test_light.py @@ -1,4 +1,4 @@ -"""The tests for the Light Switch platform.""" +"""Tests for the Switch as X Light platform.""" import pytest from homeassistant.components.light import ( @@ -106,6 +106,34 @@ async def test_switch_service_calls(hass): assert hass.states.get("light.decorative_lights").state == "on" +@pytest.mark.parametrize("target_domain", ("light",)) +async def test_config_entry_entity_id(hass: HomeAssistant, target_domain): + """Test light switch setup from config entry with entity id.""" + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={"entity_id": "switch.abc", "target_domain": target_domain}, + title="ABC", + ) + + config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert DOMAIN in hass.config.components + + state = hass.states.get(f"{target_domain}.abc") + assert state.state == "unavailable" + # Name copied from config entry title + assert state.name == "ABC" + + # Check the light is added to the entity registry + registry = er.async_get(hass) + entity_entry = registry.async_get(f"{target_domain}.abc") + assert entity_entry.unique_id == config_entry.entry_id + + @pytest.mark.parametrize("target_domain", ("light",)) async def test_config_entry_uuid(hass: HomeAssistant, target_domain): """Test light switch setup from config entry with entity registry id.""" @@ -125,22 +153,3 @@ async def test_config_entry_uuid(hass: HomeAssistant, target_domain): await hass.async_block_till_done() assert hass.states.get(f"{target_domain}.abc") - - -async def test_config_entry_unregistered_uuid(hass: HomeAssistant): - """Test light switch setup from config entry with unknown entity registry id.""" - fake_uuid = "a266a680b608c32770e6c45bfe6b8411" - - config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={"entity_id": fake_uuid}, - title="ABC", - ) - - config_entry.add_to_hass(hass) - - assert not await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() - - assert len(hass.states.async_all()) == 0