diff --git a/tests/components/alarm_control_panel/test_condition.py b/tests/components/alarm_control_panel/test_condition.py index b0c85c786e3..55e575b1819 100644 --- a/tests/components/alarm_control_panel/test_condition.py +++ b/tests/components/alarm_control_panel/test_condition.py @@ -13,14 +13,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -222,29 +221,13 @@ async def test_alarm_control_panel_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the alarm_control_panel state condition with the 'all' behavior.""" - other_entity_ids = set(target_alarm_control_panels["included"]) - {entity_id} - - # Set all alarm_control_panels, including the tested alarm_control_panel, to the initial state - for eid in target_alarm_control_panels["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_alarm_control_panels, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/assist_satellite/test_condition.py b/tests/components/assist_satellite/test_condition.py index 4e06680593c..26c43ec7db9 100644 --- a/tests/components/assist_satellite/test_condition.py +++ b/tests/components/assist_satellite/test_condition.py @@ -9,14 +9,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -137,29 +136,13 @@ async def test_assist_satellite_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the assist satellite state condition with the 'all' behavior.""" - other_entity_ids = set(target_assist_satellites["included"]) - {entity_id} - - # Set all assist satellites, including the tested one, to the initial state - for eid in target_assist_satellites["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_assist_satellites, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/climate/test_condition.py b/tests/components/climate/test_condition.py index c0c80584eab..16eb718055a 100644 --- a/tests/components/climate/test_condition.py +++ b/tests/components/climate/test_condition.py @@ -13,14 +13,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -136,33 +135,17 @@ async def test_climate_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the climate state condition with the 'all' behavior.""" - other_entity_ids = set(target_climates["included"]) - {entity_id} - - # Set all climates, including the tested climate, to the initial state - for eid in target_climates["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_climates, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] - @pytest.mark.usefixtures("enable_labs_preview_features") @pytest.mark.parametrize( @@ -248,29 +231,13 @@ async def test_climate_attribute_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the climate attribute condition with the 'all' behavior.""" - other_entity_ids = set(target_climates["included"]) - {entity_id} - - # Set all climates, including the tested climate, to the initial state - for eid in target_climates["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_climates, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/common.py b/tests/components/common.py index 09c5da280a0..687a6301123 100644 --- a/tests/components/common.py +++ b/tests/components/common.py @@ -909,6 +909,53 @@ async def assert_condition_behavior_any( assert condition(hass) == state["condition_true"] +async def assert_condition_behavior_all( + hass: HomeAssistant, + *, + target_entities: dict[str, list[str]], + condition_target_config: dict, + entity_id: str, + entities_in_target: int, + condition: str, + condition_options: dict[str, Any], + states: list[ConditionStateDescription], +) -> None: + """Test condition with the 'all' behavior.""" + other_entity_ids = set(target_entities["included"]) - {entity_id} + excluded_entity_ids = set(target_entities["excluded"]) - {entity_id} + + for eid in target_entities["included"]: + set_or_remove_state(hass, eid, states[0]["included"]) + await hass.async_block_till_done() + for eid in excluded_entity_ids: + set_or_remove_state(hass, eid, states[0]["excluded"]) + await hass.async_block_till_done() + + condition = await create_target_condition( + hass, + condition=condition, + target=condition_target_config, + behavior="all", + ) + + for state in states: + included_state = state["included"] + excluded_state = state["excluded"] + + set_or_remove_state(hass, entity_id, included_state) + await hass.async_block_till_done() + assert condition(hass) == state["condition_true_first_entity"] + + for other_entity_id in other_entity_ids: + set_or_remove_state(hass, other_entity_id, included_state) + await hass.async_block_till_done() + for excluded_entity_id in excluded_entity_ids: + set_or_remove_state(hass, excluded_entity_id, excluded_state) + await hass.async_block_till_done() + + assert condition(hass) == state["condition_true"] + + async def assert_trigger_behavior_any( hass: HomeAssistant, *, diff --git a/tests/components/cover/test_condition.py b/tests/components/cover/test_condition.py index e6b357f1626..a83262f5b7a 100644 --- a/tests/components/cover/test_condition.py +++ b/tests/components/cover/test_condition.py @@ -10,13 +10,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, create_target_condition, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -164,40 +164,17 @@ async def test_cover_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test cover condition with the 'all' behavior.""" - other_entity_ids = set(target_covers["included"]) - {entity_id} - excluded_entity_ids = set(target_covers["excluded"]) - {entity_id} - - for eid in target_covers["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - for eid in excluded_entity_ids: - set_or_remove_state(hass, eid, states[0]["excluded"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_covers, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - for state in states: - included_state = state["included"] - excluded_state = state["excluded"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - for excluded_entity_id in excluded_entity_ids: - set_or_remove_state(hass, excluded_entity_id, excluded_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] - @pytest.mark.usefixtures("enable_labs_preview_features") @pytest.mark.parametrize( diff --git a/tests/components/device_tracker/test_condition.py b/tests/components/device_tracker/test_condition.py index 1f0cacb6846..1432e5e85c9 100644 --- a/tests/components/device_tracker/test_condition.py +++ b/tests/components/device_tracker/test_condition.py @@ -9,13 +9,12 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -114,29 +113,13 @@ async def test_device_tracker_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the device tracker state condition with the 'all' behavior.""" - other_entity_ids = set(target_device_trackers["included"]) - {entity_id} - - # Set all device trackers, including the tested one, to the initial state - for eid in target_device_trackers["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_device_trackers, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/humidifier/test_condition.py b/tests/components/humidifier/test_condition.py index 5040d27b3af..dc61566edc7 100644 --- a/tests/components/humidifier/test_condition.py +++ b/tests/components/humidifier/test_condition.py @@ -10,13 +10,12 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -117,33 +116,17 @@ async def test_humidifier_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the humidifier state condition with the 'all' behavior.""" - other_entity_ids = set(target_humidifiers["included"]) - {entity_id} - - # Set all humidifiers, including the tested humidifier, to the initial state - for eid in target_humidifiers["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_humidifiers, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] - @pytest.mark.usefixtures("enable_labs_preview_features") @pytest.mark.parametrize( @@ -219,29 +202,13 @@ async def test_humidifier_attribute_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the humidifier attribute condition with the 'all' behavior.""" - other_entity_ids = set(target_humidifiers["included"]) - {entity_id} - - # Set all humidifiers, including the tested humidifier, to the initial state - for eid in target_humidifiers["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_humidifiers, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/lawn_mower/test_condition.py b/tests/components/lawn_mower/test_condition.py index 66b0393dac3..25bdf62d8fa 100644 --- a/tests/components/lawn_mower/test_condition.py +++ b/tests/components/lawn_mower/test_condition.py @@ -9,14 +9,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -148,29 +147,13 @@ async def test_lawn_mower_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the lawn mower state condition with the 'all' behavior.""" - other_entity_ids = set(target_lawn_mowers["included"]) - {entity_id} - - # Set all lawn mowers, including the tested lawn mower, to the initial state - for eid in target_lawn_mowers["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_lawn_mowers, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/lock/test_condition.py b/tests/components/lock/test_condition.py index 029f105e4ad..73d51620974 100644 --- a/tests/components/lock/test_condition.py +++ b/tests/components/lock/test_condition.py @@ -9,14 +9,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -137,29 +136,13 @@ async def test_lock_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the lock state condition with the 'all' behavior.""" - other_entity_ids = set(target_locks["included"]) - {entity_id} - - # Set all locks, including the tested lock, to the initial state - for eid in target_locks["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_locks, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/media_player/test_condition.py b/tests/components/media_player/test_condition.py index d4d89a957ee..2dded050bfd 100644 --- a/tests/components/media_player/test_condition.py +++ b/tests/components/media_player/test_condition.py @@ -9,14 +9,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -172,29 +171,13 @@ async def test_media_player_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the media player state condition with the 'all' behavior.""" - other_entity_ids = set(target_media_players["included"]) - {entity_id} - - # Set all media players, including the tested media player, to the initial state - for eid in target_media_players["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_media_players, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/person/test_condition.py b/tests/components/person/test_condition.py index 94d71a6b3dd..3dcf2071eb7 100644 --- a/tests/components/person/test_condition.py +++ b/tests/components/person/test_condition.py @@ -9,13 +9,12 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -114,29 +113,13 @@ async def test_person_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the person state condition with the 'all' behavior.""" - other_entity_ids = set(target_persons["included"]) - {entity_id} - - # Set all persons, including the tested person, to the initial state - for eid in target_persons["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_persons, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"] diff --git a/tests/components/vacuum/test_condition.py b/tests/components/vacuum/test_condition.py index 4715b681af3..7d3abfc3811 100644 --- a/tests/components/vacuum/test_condition.py +++ b/tests/components/vacuum/test_condition.py @@ -9,14 +9,13 @@ from homeassistant.core import HomeAssistant from tests.components.common import ( ConditionStateDescription, + assert_condition_behavior_all, assert_condition_behavior_any, assert_condition_gated_by_labs_flag, - create_target_condition, other_states, parametrize_condition_states_all, parametrize_condition_states_any, parametrize_target_entities, - set_or_remove_state, target_entities, ) @@ -148,29 +147,13 @@ async def test_vacuum_state_condition_behavior_all( states: list[ConditionStateDescription], ) -> None: """Test the vacuum state condition with the 'all' behavior.""" - other_entity_ids = set(target_vacuums["included"]) - {entity_id} - - # Set all vacuums, including the tested vacuum, to the initial state - for eid in target_vacuums["included"]: - set_or_remove_state(hass, eid, states[0]["included"]) - await hass.async_block_till_done() - - condition = await create_target_condition( + await assert_condition_behavior_all( hass, + target_entities=target_vacuums, + condition_target_config=condition_target_config, + entity_id=entity_id, + entities_in_target=entities_in_target, condition=condition, - target=condition_target_config, - behavior="all", + condition_options=condition_options, + states=states, ) - - for state in states: - included_state = state["included"] - - set_or_remove_state(hass, entity_id, included_state) - await hass.async_block_till_done() - assert condition(hass) == state["condition_true_first_entity"] - - for other_entity_id in other_entity_ids: - set_or_remove_state(hass, other_entity_id, included_state) - await hass.async_block_till_done() - - assert condition(hass) == state["condition_true"]