diff --git a/tests/components/group/test_config_flow.py b/tests/components/group/test_config_flow.py index d720b051e53..cf0254768a3 100644 --- a/tests/components/group/test_config_flow.py +++ b/tests/components/group/test_config_flow.py @@ -8,6 +8,8 @@ from homeassistant.components.group import DOMAIN, async_setup_entry from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM +from tests.common import MockConfigEntry + @pytest.mark.parametrize( "group_type,group_state,member_state,member_attributes,extra_input,extra_options,extra_attrs", @@ -122,46 +124,26 @@ async def test_options( for member in members2: hass.states.async_set(member, member_state, {}) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] == RESULT_TYPE_FORM - assert result["errors"] is None - - assert get_suggested(result["data_schema"].schema, "group_type") is None - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - {"group_type": group_type}, - ) - await hass.async_block_till_done() - assert result["type"] == RESULT_TYPE_FORM - assert result["step_id"] == group_type - - assert get_suggested(result["data_schema"].schema, "entities") is None - assert get_suggested(result["data_schema"].schema, "name") is None - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "name": "Bed Room", + switch_as_x_config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ "entities": members1, + "group_type": group_type, + "name": "Bed Room", + **extra_options, }, + title="Bed Room", ) + switch_as_x_config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(switch_as_x_config_entry.entry_id) await hass.async_block_till_done() - assert result["type"] == RESULT_TYPE_CREATE_ENTRY state = hass.states.get(f"{group_type}.bed_room") assert state.attributes["entity_id"] == members1 config_entry = hass.config_entries.async_entries(DOMAIN)[0] - assert config_entry.data == {} - assert config_entry.options == { - "group_type": group_type, - "entities": members1, - "name": "Bed Room", - **extra_options, - } result = await hass.config_entries.options.async_init(config_entry.entry_id) assert result["type"] == RESULT_TYPE_FORM diff --git a/tests/components/group/test_init.py b/tests/components/group/test_init.py index 4eb0e455422..19b9245dc7f 100644 --- a/tests/components/group/test_init.py +++ b/tests/components/group/test_init.py @@ -1,6 +1,9 @@ """The tests for the Group components.""" # pylint: disable=protected-access +from __future__ import annotations + from collections import OrderedDict +from typing import Any from unittest.mock import patch import pytest @@ -18,11 +21,12 @@ from homeassistant.const import ( STATE_ON, STATE_UNKNOWN, ) -from homeassistant.core import CoreState +from homeassistant.core import CoreState, HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.event import TRACK_STATE_CHANGE_CALLBACKS from homeassistant.setup import async_setup_component -from tests.common import assert_setup_component +from tests.common import MockConfigEntry, assert_setup_component from tests.components.group import common @@ -1358,3 +1362,57 @@ async def test_plant_group(hass): await hass.async_block_till_done() assert hass.states.get("group.plants").state == "problem" assert hass.states.get("group.plant_with_binary_sensors").state == "on" + + +@pytest.mark.parametrize( + "group_type,member_state,extra_options", + ( + ("binary_sensor", "on", {"all": False}), + ("cover", "open", {}), + ("fan", "on", {}), + ("light", "on", {}), + ("media_player", "on", {}), + ), +) +async def test_setup_and_remove_config_entry( + hass: HomeAssistant, + group_type: str, + member_state: str, + extra_options: dict[str, Any], +) -> None: + """Test removing a config entry.""" + registry = er.async_get(hass) + + members1 = [f"{group_type}.one", f"{group_type}.two"] + + for member in members1: + hass.states.async_set(member, member_state, {}) + + # Setup the config entry + group_config_entry = MockConfigEntry( + data={}, + domain=group.DOMAIN, + options={ + "entities": members1, + "group_type": group_type, + "name": "Bed Room", + **extra_options, + }, + title="Bed Room", + ) + group_config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(group_config_entry.entry_id) + await hass.async_block_till_done() + + # Check the state and entity registry entry are present + state = hass.states.get(f"{group_type}.bed_room") + assert state.attributes["entity_id"] == members1 + assert registry.async_get(f"{group_type}.bed_room") is not None + + # Remove the config entry + assert await hass.config_entries.async_remove(group_config_entry.entry_id) + await hass.async_block_till_done() + + # Check the state and entity registry entry are removed + assert hass.states.get(f"{group_type}.bed_room") is None + assert registry.async_get(f"{group_type}.bed_room") is None