From 0e154635ffb7a592f2ed11e9a2127962ac6ae6b0 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Sat, 4 Oct 2025 21:45:48 +0200 Subject: [PATCH] Limit shelly tests to single platform (#153681) --- tests/components/shelly/__init__.py | 30 +++++++++++++++++-- tests/components/shelly/test_binary_sensor.py | 16 +++++++++- tests/components/shelly/test_button.py | 11 +++++-- tests/components/shelly/test_climate.py | 16 +++++++++- tests/components/shelly/test_cover.py | 16 ++++++++-- tests/components/shelly/test_event.py | 16 ++++++++-- tests/components/shelly/test_light.py | 9 ++++++ tests/components/shelly/test_number.py | 16 ++++++++-- tests/components/shelly/test_select.py | 11 +++++-- tests/components/shelly/test_sensor.py | 9 ++++++ tests/components/shelly/test_switch.py | 11 +++++++ tests/components/shelly/test_text.py | 11 +++++-- tests/components/shelly/test_update.py | 9 ++++++ tests/components/shelly/test_valve.py | 10 ++++++- 14 files changed, 174 insertions(+), 17 deletions(-) diff --git a/tests/components/shelly/__init__.py b/tests/components/shelly/__init__.py index 30ae74079f0..8eaffe5af86 100644 --- a/tests/components/shelly/__init__.py +++ b/tests/components/shelly/__init__.py @@ -1,10 +1,11 @@ """Tests for the Shelly integration.""" from collections.abc import Mapping, Sequence +from contextlib import contextmanager from copy import deepcopy from datetime import timedelta from typing import Any -from unittest.mock import Mock +from unittest.mock import Mock, patch from aioshelly.const import MODEL_25 from freezegun.api import FrozenDateTimeFactory @@ -12,6 +13,11 @@ import pytest from syrupy.assertion import SnapshotAssertion from syrupy.filters import props +from homeassistant.components.shelly import ( + BLOCK_SLEEPING_PLATFORMS, + PLATFORMS, + RPC_SLEEPING_PLATFORMS, +) from homeassistant.components.shelly.const import ( CONF_GEN, CONF_SLEEP_PERIOD, @@ -20,7 +26,7 @@ from homeassistant.components.shelly.const import ( RPC_SENSORS_POLLING_INTERVAL, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST, CONF_MODEL +from homeassistant.const import CONF_HOST, CONF_MODEL, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.helpers.device_registry import ( @@ -204,3 +210,23 @@ async def force_uptime_value( """Force time to a specific point.""" await hass.config.async_set_time_zone("UTC") freezer.move_to("2025-05-26 16:04:00+00:00") + + +@contextmanager +def patch_platforms(platforms: list[Platform]): + """Only allow given platforms to be loaded.""" + with ( + patch( + "homeassistant.components.shelly.PLATFORMS", + list(set(PLATFORMS) & set(platforms)), + ), + patch( + "homeassistant.components.shelly.BLOCK_SLEEPING_PLATFORMS", + list(set(BLOCK_SLEEPING_PLATFORMS) & set(platforms)), + ), + patch( + "homeassistant.components.shelly.RPC_SLEEPING_PLATFORMS", + list(set(RPC_SLEEPING_PLATFORMS) & set(platforms)), + ), + ): + yield diff --git a/tests/components/shelly/test_binary_sensor.py b/tests/components/shelly/test_binary_sensor.py index db0b05aec95..ed764ddf601 100644 --- a/tests/components/shelly/test_binary_sensor.py +++ b/tests/components/shelly/test_binary_sensor.py @@ -10,7 +10,13 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN from homeassistant.components.shelly.const import UPDATE_PERIOD_MULTIPLIER -from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN +from homeassistant.const import ( + STATE_OFF, + STATE_ON, + STATE_UNAVAILABLE, + STATE_UNKNOWN, + Platform, +) from homeassistant.core import HomeAssistant, State from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.entity_registry import EntityRegistry @@ -19,6 +25,7 @@ from . import ( init_integration, mock_rest_update, mutate_rpc_device_status, + patch_platforms, register_device, register_entity, register_sub_device, @@ -30,6 +37,13 @@ RELAY_BLOCK_ID = 0 SENSOR_BLOCK_ID = 3 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.BINARY_SENSOR]): + yield + + async def test_block_binary_sensor( hass: HomeAssistant, mock_block_device: Mock, diff --git a/tests/components/shelly/test_button.py b/tests/components/shelly/test_button.py index fe220b5b3d7..f6a3df0bb48 100644 --- a/tests/components/shelly/test_button.py +++ b/tests/components/shelly/test_button.py @@ -11,13 +11,20 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.components.shelly.const import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration, register_device, register_entity +from . import init_integration, patch_platforms, register_device, register_entity + + +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.BUTTON]): + yield async def test_block_button( diff --git a/tests/components/shelly/test_climate.py b/tests/components/shelly/test_climate.py index c19bd916fed..61946298f79 100644 --- a/tests/components/shelly/test_climate.py +++ b/tests/components/shelly/test_climate.py @@ -35,6 +35,7 @@ from homeassistant.const import ( ATTR_TEMPERATURE, STATE_ON, STATE_UNAVAILABLE, + Platform, ) from homeassistant.core import HomeAssistant, State from homeassistant.exceptions import HomeAssistantError, ServiceValidationError @@ -43,7 +44,13 @@ from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM -from . import MOCK_MAC, init_integration, register_device, register_entity +from . import ( + MOCK_MAC, + init_integration, + patch_platforms, + register_device, + register_entity, +) from .conftest import MOCK_STATUS_COAP from tests.common import mock_restore_cache, mock_restore_cache_with_extra_data @@ -55,6 +62,13 @@ GAS_VALVE_BLOCK_ID = 6 ENTITY_ID = f"{CLIMATE_DOMAIN}.test_name" +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.CLIMATE, Platform.SWITCH]): + yield + + async def test_climate_hvac_mode( hass: HomeAssistant, mock_block_device: Mock, diff --git a/tests/components/shelly/test_cover.py b/tests/components/shelly/test_cover.py index 7d194b1b005..637adaed225 100644 --- a/tests/components/shelly/test_cover.py +++ b/tests/components/shelly/test_cover.py @@ -23,15 +23,27 @@ from homeassistant.components.cover import ( CoverState, ) from homeassistant.components.shelly.const import RPC_COVER_UPDATE_TIME_SEC -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration, mock_polling_rpc_update, mutate_rpc_device_status +from . import ( + init_integration, + mock_polling_rpc_update, + mutate_rpc_device_status, + patch_platforms, +) ROLLER_BLOCK_ID = 1 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.COVER]): + yield + + async def test_block_device_services( hass: HomeAssistant, mock_block_device: Mock, diff --git a/tests/components/shelly/test_event.py b/tests/components/shelly/test_event.py index 520233eaf60..c530f30beb9 100644 --- a/tests/components/shelly/test_event.py +++ b/tests/components/shelly/test_event.py @@ -14,15 +14,27 @@ from homeassistant.components.event import ( DOMAIN as EVENT_DOMAIN, EventDeviceClass, ) -from homeassistant.const import ATTR_DEVICE_CLASS, STATE_UNKNOWN +from homeassistant.const import ATTR_DEVICE_CLASS, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration, inject_rpc_device_event, register_entity +from . import ( + init_integration, + inject_rpc_device_event, + patch_platforms, + register_entity, +) DEVICE_BLOCK_ID = 4 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.EVENT]): + yield + + async def test_rpc_button( hass: HomeAssistant, mock_rpc_device: Mock, diff --git a/tests/components/shelly/test_light.py b/tests/components/shelly/test_light.py index 959e6a471ba..cf4ffbc2f66 100644 --- a/tests/components/shelly/test_light.py +++ b/tests/components/shelly/test_light.py @@ -38,6 +38,7 @@ from homeassistant.const import ( ATTR_SUPPORTED_FEATURES, STATE_OFF, STATE_ON, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceRegistry @@ -47,6 +48,7 @@ from . import ( get_entity, init_integration, mutate_rpc_device_status, + patch_platforms, register_device, register_entity, ) @@ -57,6 +59,13 @@ LIGHT_BLOCK_ID = 2 SHELLY_PLUS_RGBW_CHANNELS = 4 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.LIGHT]): + yield + + async def test_block_device_rgbw_bulb( hass: HomeAssistant, mock_block_device: Mock, diff --git a/tests/components/shelly/test_number.py b/tests/components/shelly/test_number.py index 9f7e85f8f05..c7230821772 100644 --- a/tests/components/shelly/test_number.py +++ b/tests/components/shelly/test_number.py @@ -20,19 +20,31 @@ from homeassistant.components.number import ( ) from homeassistant.components.shelly.const import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN +from homeassistant.const import ( + ATTR_ENTITY_ID, + ATTR_UNIT_OF_MEASUREMENT, + STATE_UNKNOWN, + Platform, +) from homeassistant.core import HomeAssistant, State from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration, register_device, register_entity +from . import init_integration, patch_platforms, register_device, register_entity from tests.common import mock_restore_cache_with_extra_data DEVICE_BLOCK_ID = 4 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.NUMBER]): + yield + + async def test_block_number_update( hass: HomeAssistant, mock_block_device: Mock, diff --git a/tests/components/shelly/test_select.py b/tests/components/shelly/test_select.py index 4586da344db..eefd84d40eb 100644 --- a/tests/components/shelly/test_select.py +++ b/tests/components/shelly/test_select.py @@ -14,13 +14,20 @@ from homeassistant.components.select import ( ) from homeassistant.components.shelly.const import DOMAIN from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration, register_device, register_entity +from . import init_integration, patch_platforms, register_device, register_entity + + +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.SELECT]): + yield @pytest.mark.parametrize( diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 8bca4ce38ab..e6d6812505b 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -28,6 +28,7 @@ from homeassistant.const import ( PERCENTAGE, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, UnitOfElectricCurrent, UnitOfElectricPotential, UnitOfEnergy, @@ -47,6 +48,7 @@ from . import ( mock_polling_rpc_update, mock_rest_update, mutate_rpc_device_status, + patch_platforms, register_device, register_entity, ) @@ -58,6 +60,13 @@ SENSOR_BLOCK_ID = 3 DEVICE_BLOCK_ID = 4 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.SENSOR]): + yield + + async def test_block_sensor( hass: HomeAssistant, mock_block_device: Mock, diff --git a/tests/components/shelly/test_switch.py b/tests/components/shelly/test_switch.py index 2cb807236ec..39fc001cbed 100644 --- a/tests/components/shelly/test_switch.py +++ b/tests/components/shelly/test_switch.py @@ -25,6 +25,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant, State from homeassistant.exceptions import HomeAssistantError @@ -34,6 +35,7 @@ from homeassistant.helpers.entity_registry import EntityRegistry from . import ( init_integration, inject_rpc_device_event, + patch_platforms, register_device, register_entity, register_sub_device, @@ -48,6 +50,15 @@ GAS_VALVE_BLOCK_ID = 6 MOTION_BLOCK_ID = 3 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms( + [Platform.SWITCH, Platform.CLIMATE, Platform.VALVE, Platform.LIGHT] + ): + yield + + async def test_block_device_services( hass: HomeAssistant, mock_block_device: Mock ) -> None: diff --git a/tests/components/shelly/test_text.py b/tests/components/shelly/test_text.py index 3190fabfbea..59c434213b1 100644 --- a/tests/components/shelly/test_text.py +++ b/tests/components/shelly/test_text.py @@ -13,13 +13,20 @@ from homeassistant.components.text import ( SERVICE_SET_VALUE, ) from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration, register_device, register_entity +from . import init_integration, patch_platforms, register_device, register_entity + + +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.TEXT]): + yield @pytest.mark.parametrize( diff --git a/tests/components/shelly/test_update.py b/tests/components/shelly/test_update.py index 51016f0cdaa..8007ecc3615 100644 --- a/tests/components/shelly/test_update.py +++ b/tests/components/shelly/test_update.py @@ -29,6 +29,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) from homeassistant.core import HomeAssistant, State from homeassistant.exceptions import HomeAssistantError @@ -39,6 +40,7 @@ from . import ( init_integration, inject_rpc_device_event, mock_rest_update, + patch_platforms, register_device, register_entity, ) @@ -46,6 +48,13 @@ from . import ( from tests.common import mock_restore_cache +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.UPDATE]): + yield + + @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_block_update( hass: HomeAssistant, diff --git a/tests/components/shelly/test_valve.py b/tests/components/shelly/test_valve.py index adb6559ee10..301de83c2d8 100644 --- a/tests/components/shelly/test_valve.py +++ b/tests/components/shelly/test_valve.py @@ -21,15 +21,23 @@ from homeassistant.const import ( SERVICE_CLOSE_VALVE, SERVICE_OPEN_VALVE, SERVICE_SET_VALVE_POSITION, + Platform, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_registry import EntityRegistry -from . import init_integration +from . import init_integration, patch_platforms GAS_VALVE_BLOCK_ID = 6 +@pytest.fixture(autouse=True) +def fixture_platforms(): + """Limit platforms under test.""" + with patch_platforms([Platform.VALVE]): + yield + + async def test_block_device_gas_valve( hass: HomeAssistant, entity_registry: EntityRegistry,