From 5b082ed6691df748833bc5c0998e91eeffa538d9 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Fri, 19 Apr 2024 14:48:18 +0200 Subject: [PATCH] Add group tests with mixed domain entities (#115849) --- tests/components/group/test_init.py | 107 ++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/tests/components/group/test_init.py b/tests/components/group/test_init.py index 0f8d487b340..9c2f14f5d74 100644 --- a/tests/components/group/test_init.py +++ b/tests/components/group/test_init.py @@ -2,6 +2,7 @@ from __future__ import annotations +import asyncio from collections import OrderedDict from typing import Any from unittest.mock import patch @@ -15,11 +16,15 @@ from homeassistant.const import ( ATTR_ICON, EVENT_HOMEASSISTANT_START, SERVICE_RELOAD, + STATE_CLOSED, STATE_HOME, + STATE_LOCKED, STATE_NOT_HOME, STATE_OFF, STATE_ON, + STATE_OPEN, STATE_UNKNOWN, + STATE_UNLOCKED, ) from homeassistant.core import CoreState, HomeAssistant from homeassistant.helpers import entity_registry as er @@ -603,6 +608,108 @@ async def test_is_on(hass: HomeAssistant) -> None: assert not group.is_on(hass, "non.existing") +@pytest.mark.parametrize( + ( + "domains", + "states_old", + "states_new", + "state_ison_group_old", + "state_ison_group_new", + ), + [ + ( + ("light", "light"), + (STATE_ON, STATE_OFF), + (STATE_OFF, STATE_OFF), + (STATE_ON, True), + (STATE_OFF, False), + ), + ( + ("cover", "cover"), + (STATE_OPEN, STATE_CLOSED), + (STATE_CLOSED, STATE_CLOSED), + (STATE_OPEN, True), + (STATE_CLOSED, False), + ), + ( + ("lock", "lock"), + (STATE_UNLOCKED, STATE_LOCKED), + (STATE_LOCKED, STATE_LOCKED), + (STATE_UNLOCKED, True), + (STATE_LOCKED, False), + ), + ( + ("cover", "lock"), + (STATE_OPEN, STATE_LOCKED), + (STATE_CLOSED, STATE_LOCKED), + (STATE_ON, True), + (STATE_OFF, False), + ), + ( + ("cover", "lock"), + (STATE_OPEN, STATE_UNLOCKED), + (STATE_CLOSED, STATE_LOCKED), + (STATE_ON, True), + (STATE_OFF, False), + ), + ( + ("cover", "lock", "light"), + (STATE_OPEN, STATE_LOCKED, STATE_ON), + (STATE_CLOSED, STATE_LOCKED, STATE_OFF), + (STATE_ON, True), + (STATE_OFF, False), + ), + ], +) +async def test_is_on_and_state_mixed_domains( + hass: HomeAssistant, + domains: tuple[str,], + states_old: tuple[str,], + states_new: tuple[str,], + state_ison_group_old: tuple[str, bool], + state_ison_group_new: tuple[str, bool], +) -> None: + """Test is_on method with mixed domains.""" + count = len(domains) + entity_ids = [f"{domains[index]}.test_{index}" for index in range(count)] + for index in range(count): + hass.states.async_set(entity_ids[index], states_old[index]) + + assert not group.is_on(hass, "group.none") + await asyncio.gather( + *[async_setup_component(hass, domain, {}) for domain in set(domains)] + ) + assert await async_setup_component(hass, "group", {}) + await hass.async_block_till_done() + + test_group = await group.Group.async_create_group( + hass, + "init_group", + created_by_service=True, + entity_ids=entity_ids, + icon=None, + mode=None, + object_id=None, + order=None, + ) + await hass.async_block_till_done() + + # Assert on old state + state = hass.states.get(test_group.entity_id) + assert state is not None + assert state.state == state_ison_group_old[0] + assert group.is_on(hass, test_group.entity_id) == state_ison_group_old[1] + + # Switch and assert on new state + for index in range(count): + hass.states.async_set(entity_ids[index], states_new[index]) + await hass.async_block_till_done() + state = hass.states.get(test_group.entity_id) + assert state is not None + assert state.state == state_ison_group_new[0] + assert group.is_on(hass, test_group.entity_id) == state_ison_group_new[1] + + async def test_reloading_groups(hass: HomeAssistant) -> None: """Test reloading the group config.""" assert await async_setup_component(