From 02efe41564e2f16aa3f12f563b44b13c04a61938 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 8 Feb 2024 16:31:17 -0600 Subject: [PATCH] Avoid directly changing config entry state in tests (#110048) --- tests/common.py | 21 +++++++++++++++++++ tests/components/blink/test_services.py | 4 ++-- tests/components/hue/test_light_v1.py | 2 +- tests/components/opower/test_config_flow.py | 4 ++-- .../smartthings/test_binary_sensor.py | 2 +- tests/components/smartthings/test_cover.py | 2 +- tests/components/smartthings/test_fan.py | 2 +- tests/components/smartthings/test_light.py | 2 +- tests/components/smartthings/test_lock.py | 2 +- tests/components/smartthings/test_scene.py | 2 +- tests/components/smartthings/test_sensor.py | 2 +- tests/components/smartthings/test_switch.py | 2 +- 12 files changed, 34 insertions(+), 13 deletions(-) diff --git a/tests/common.py b/tests/common.py index 1b40904d5e2..24b9134c32e 100644 --- a/tests/common.py +++ b/tests/common.py @@ -944,6 +944,27 @@ class MockConfigEntry(config_entries.ConfigEntry): """Test helper to add entry to entry manager.""" manager._entries[self.entry_id] = self + def mock_state( + self, + hass: HomeAssistant, + state: config_entries.ConfigEntryState, + reason: str | None = None, + ) -> None: + """Mock the state of a config entry to be used in tests. + + Currently this is a wrapper around _async_set_state, but it may + change in the future. + + It is preferable to get the config entry into the desired state + by using the normal config entry methods, and this helper + is only intended to be used in cases where that is not possible. + + When in doubt, this helper should not be used in new code + and is only intended for backwards compatibility with existing + tests. + """ + self._async_set_state(hass, state, reason) + def patch_yaml_files(files_dict, endswith=True): """Patch load_yaml with a dictionary of yaml files.""" diff --git a/tests/components/blink/test_services.py b/tests/components/blink/test_services.py index 1c2faa32d04..c5777ecb2f0 100644 --- a/tests/components/blink/test_services.py +++ b/tests/components/blink/test_services.py @@ -179,7 +179,7 @@ async def test_service_pin_called_with_unloaded_entry( mock_config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - mock_config_entry.state = ConfigEntryState.SETUP_ERROR + mock_config_entry.mock_state(hass, ConfigEntryState.SETUP_ERROR) hass.config.is_allowed_path = Mock(return_value=True) mock_blink_api.cameras = {CAMERA_NAME: AsyncMock()} @@ -207,7 +207,7 @@ async def test_service_update_called_with_unloaded_entry( assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - mock_config_entry.state = ConfigEntryState.SETUP_ERROR + mock_config_entry.mock_state(hass, ConfigEntryState.SETUP_ERROR) hass.config.is_allowed_path = Mock(return_value=True) mock_blink_api.cameras = {CAMERA_NAME: AsyncMock()} diff --git a/tests/components/hue/test_light_v1.py b/tests/components/hue/test_light_v1.py index d1fd9cdc62f..90f8b0adfbf 100644 --- a/tests/components/hue/test_light_v1.py +++ b/tests/components/hue/test_light_v1.py @@ -179,7 +179,7 @@ async def setup_bridge(hass, mock_bridge_v1): hass.config.components.add(hue.DOMAIN) config_entry = create_config_entry() config_entry.add_to_hass(hass) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) config_entry.options = {CONF_ALLOW_HUE_GROUPS: True} mock_bridge_v1.config_entry = config_entry hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge_v1} diff --git a/tests/components/opower/test_config_flow.py b/tests/components/opower/test_config_flow.py index f9ae457a80e..0e96af200df 100644 --- a/tests/components/opower/test_config_flow.py +++ b/tests/components/opower/test_config_flow.py @@ -277,7 +277,7 @@ async def test_form_valid_reauth( mock_config_entry: MockConfigEntry, ) -> None: """Test that we can handle a valid reauth.""" - mock_config_entry.state = ConfigEntryState.LOADED + mock_config_entry.mock_state(hass, ConfigEntryState.LOADED) mock_config_entry.async_start_reauth(hass) await hass.async_block_till_done() @@ -326,7 +326,7 @@ async def test_form_valid_reauth_with_mfa( "utility": "Consolidated Edison (ConEd)", }, ) - mock_config_entry.state = ConfigEntryState.LOADED + mock_config_entry.mock_state(hass, ConfigEntryState.LOADED) mock_config_entry.async_start_reauth(hass) await hass.async_block_till_done() diff --git a/tests/components/smartthings/test_binary_sensor.py b/tests/components/smartthings/test_binary_sensor.py index d6fe0bd40fc..1c222b3ca78 100644 --- a/tests/components/smartthings/test_binary_sensor.py +++ b/tests/components/smartthings/test_binary_sensor.py @@ -106,7 +106,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: "Motion Sensor 1", [Capability.motion_sensor], {Attribute.motion: "inactive"} ) config_entry = await setup_platform(hass, BINARY_SENSOR_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor") # Assert diff --git a/tests/components/smartthings/test_cover.py b/tests/components/smartthings/test_cover.py index bf781c71c4e..081b40e57a9 100644 --- a/tests/components/smartthings/test_cover.py +++ b/tests/components/smartthings/test_cover.py @@ -243,7 +243,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: "Garage", [Capability.garage_door_control], {Attribute.door: "open"} ) config_entry = await setup_platform(hass, COVER_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, COVER_DOMAIN) # Assert diff --git a/tests/components/smartthings/test_fan.py b/tests/components/smartthings/test_fan.py index 751646580d9..ca2f97b2909 100644 --- a/tests/components/smartthings/test_fan.py +++ b/tests/components/smartthings/test_fan.py @@ -301,7 +301,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: status={Attribute.switch: "off", Attribute.fan_speed: 0}, ) config_entry = await setup_platform(hass, FAN_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, "fan") # Assert diff --git a/tests/components/smartthings/test_light.py b/tests/components/smartthings/test_light.py index d2d0a133859..b6e4e5a107b 100644 --- a/tests/components/smartthings/test_light.py +++ b/tests/components/smartthings/test_light.py @@ -319,7 +319,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: }, ) config_entry = await setup_platform(hass, LIGHT_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, "light") # Assert diff --git a/tests/components/smartthings/test_lock.py b/tests/components/smartthings/test_lock.py index 58111087848..10981433c1d 100644 --- a/tests/components/smartthings/test_lock.py +++ b/tests/components/smartthings/test_lock.py @@ -120,7 +120,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: # Arrange device = device_factory("Lock_1", [Capability.lock], {Attribute.lock: "locked"}) config_entry = await setup_platform(hass, LOCK_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, "lock") # Assert diff --git a/tests/components/smartthings/test_scene.py b/tests/components/smartthings/test_scene.py index 39e387c7658..489ca87371c 100644 --- a/tests/components/smartthings/test_scene.py +++ b/tests/components/smartthings/test_scene.py @@ -44,7 +44,7 @@ async def test_unload_config_entry(hass: HomeAssistant, scene) -> None: """Test the scene is removed when the config entry is unloaded.""" # Arrange config_entry = await setup_platform(hass, SCENE_DOMAIN, scenes=[scene]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, SCENE_DOMAIN) # Assert diff --git a/tests/components/smartthings/test_sensor.py b/tests/components/smartthings/test_sensor.py index ab163360778..35d38dd33de 100644 --- a/tests/components/smartthings/test_sensor.py +++ b/tests/components/smartthings/test_sensor.py @@ -296,7 +296,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: # Arrange device = device_factory("Sensor 1", [Capability.battery], {Attribute.battery: 100}) config_entry = await setup_platform(hass, SENSOR_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, "sensor") # Assert diff --git a/tests/components/smartthings/test_switch.py b/tests/components/smartthings/test_switch.py index 437acb04f56..e9dd8ad1b68 100644 --- a/tests/components/smartthings/test_switch.py +++ b/tests/components/smartthings/test_switch.py @@ -106,7 +106,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None: # Arrange device = device_factory("Switch 1", [Capability.switch], {Attribute.switch: "on"}) config_entry = await setup_platform(hass, SWITCH_DOMAIN, devices=[device]) - config_entry.state = ConfigEntryState.LOADED + config_entry.mock_state(hass, ConfigEntryState.LOADED) # Act await hass.config_entries.async_forward_entry_unload(config_entry, "switch") # Assert