Avoid directly changing config entry state in tests (#110048)
parent
531f2e8443
commit
02efe41564
|
@ -944,6 +944,27 @@ class MockConfigEntry(config_entries.ConfigEntry):
|
||||||
"""Test helper to add entry to entry manager."""
|
"""Test helper to add entry to entry manager."""
|
||||||
manager._entries[self.entry_id] = self
|
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):
|
def patch_yaml_files(files_dict, endswith=True):
|
||||||
"""Patch load_yaml with a dictionary of yaml files."""
|
"""Patch load_yaml with a dictionary of yaml files."""
|
||||||
|
|
|
@ -179,7 +179,7 @@ async def test_service_pin_called_with_unloaded_entry(
|
||||||
mock_config_entry.add_to_hass(hass)
|
mock_config_entry.add_to_hass(hass)
|
||||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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)
|
hass.config.is_allowed_path = Mock(return_value=True)
|
||||||
mock_blink_api.cameras = {CAMERA_NAME: AsyncMock()}
|
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)
|
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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)
|
hass.config.is_allowed_path = Mock(return_value=True)
|
||||||
mock_blink_api.cameras = {CAMERA_NAME: AsyncMock()}
|
mock_blink_api.cameras = {CAMERA_NAME: AsyncMock()}
|
||||||
|
|
||||||
|
|
|
@ -179,7 +179,7 @@ async def setup_bridge(hass, mock_bridge_v1):
|
||||||
hass.config.components.add(hue.DOMAIN)
|
hass.config.components.add(hue.DOMAIN)
|
||||||
config_entry = create_config_entry()
|
config_entry = create_config_entry()
|
||||||
config_entry.add_to_hass(hass)
|
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}
|
config_entry.options = {CONF_ALLOW_HUE_GROUPS: True}
|
||||||
mock_bridge_v1.config_entry = config_entry
|
mock_bridge_v1.config_entry = config_entry
|
||||||
hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge_v1}
|
hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge_v1}
|
||||||
|
|
|
@ -277,7 +277,7 @@ async def test_form_valid_reauth(
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that we can handle a valid reauth."""
|
"""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)
|
mock_config_entry.async_start_reauth(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
@ -326,7 +326,7 @@ async def test_form_valid_reauth_with_mfa(
|
||||||
"utility": "Consolidated Edison (ConEd)",
|
"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)
|
mock_config_entry.async_start_reauth(hass)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None:
|
||||||
"Motion Sensor 1", [Capability.motion_sensor], {Attribute.motion: "inactive"}
|
"Motion Sensor 1", [Capability.motion_sensor], {Attribute.motion: "inactive"}
|
||||||
)
|
)
|
||||||
config_entry = await setup_platform(hass, BINARY_SENSOR_DOMAIN, devices=[device])
|
config_entry = await setup_platform(hass, BINARY_SENSOR_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -243,7 +243,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None:
|
||||||
"Garage", [Capability.garage_door_control], {Attribute.door: "open"}
|
"Garage", [Capability.garage_door_control], {Attribute.door: "open"}
|
||||||
)
|
)
|
||||||
config_entry = await setup_platform(hass, COVER_DOMAIN, devices=[device])
|
config_entry = await setup_platform(hass, COVER_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, COVER_DOMAIN)
|
await hass.config_entries.async_forward_entry_unload(config_entry, COVER_DOMAIN)
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -301,7 +301,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None:
|
||||||
status={Attribute.switch: "off", Attribute.fan_speed: 0},
|
status={Attribute.switch: "off", Attribute.fan_speed: 0},
|
||||||
)
|
)
|
||||||
config_entry = await setup_platform(hass, FAN_DOMAIN, devices=[device])
|
config_entry = await setup_platform(hass, FAN_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "fan")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "fan")
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -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 = await setup_platform(hass, LIGHT_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "light")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "light")
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -120,7 +120,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None:
|
||||||
# Arrange
|
# Arrange
|
||||||
device = device_factory("Lock_1", [Capability.lock], {Attribute.lock: "locked"})
|
device = device_factory("Lock_1", [Capability.lock], {Attribute.lock: "locked"})
|
||||||
config_entry = await setup_platform(hass, LOCK_DOMAIN, devices=[device])
|
config_entry = await setup_platform(hass, LOCK_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "lock")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "lock")
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -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."""
|
"""Test the scene is removed when the config entry is unloaded."""
|
||||||
# Arrange
|
# Arrange
|
||||||
config_entry = await setup_platform(hass, SCENE_DOMAIN, scenes=[scene])
|
config_entry = await setup_platform(hass, SCENE_DOMAIN, scenes=[scene])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, SCENE_DOMAIN)
|
await hass.config_entries.async_forward_entry_unload(config_entry, SCENE_DOMAIN)
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -296,7 +296,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None:
|
||||||
# Arrange
|
# Arrange
|
||||||
device = device_factory("Sensor 1", [Capability.battery], {Attribute.battery: 100})
|
device = device_factory("Sensor 1", [Capability.battery], {Attribute.battery: 100})
|
||||||
config_entry = await setup_platform(hass, SENSOR_DOMAIN, devices=[device])
|
config_entry = await setup_platform(hass, SENSOR_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
||||||
# Assert
|
# Assert
|
||||||
|
|
|
@ -106,7 +106,7 @@ async def test_unload_config_entry(hass: HomeAssistant, device_factory) -> None:
|
||||||
# Arrange
|
# Arrange
|
||||||
device = device_factory("Switch 1", [Capability.switch], {Attribute.switch: "on"})
|
device = device_factory("Switch 1", [Capability.switch], {Attribute.switch: "on"})
|
||||||
config_entry = await setup_platform(hass, SWITCH_DOMAIN, devices=[device])
|
config_entry = await setup_platform(hass, SWITCH_DOMAIN, devices=[device])
|
||||||
config_entry.state = ConfigEntryState.LOADED
|
config_entry.mock_state(hass, ConfigEntryState.LOADED)
|
||||||
# Act
|
# Act
|
||||||
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
||||||
# Assert
|
# Assert
|
||||||
|
|
Loading…
Reference in New Issue