Avoid directly changing config entry state in tests (#110048)

pull/102884/head^2
J. Nick Koston 2024-02-08 16:31:17 -06:00 committed by GitHub
parent 531f2e8443
commit 02efe41564
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 34 additions and 13 deletions

View File

@ -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."""

View File

@ -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()}

View File

@ -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}

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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