Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/roborock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: RoborockConfigEntry) ->
show_background=entry.options.get(CONF_SHOW_BACKGROUND, False),
map_scale=MAP_SCALE,
),
mqtt_session_unauthorized_hook=lambda: entry.async_start_reauth(hass),
Comment thread
allenporter marked this conversation as resolved.
)
except RoborockInvalidCredentials as err:
raise ConfigEntryAuthFailed(
Expand Down
38 changes: 38 additions & 0 deletions tests/components/roborock/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,44 @@ async def test_reauth_started(
assert flows[0]["step_id"] == "reauth_confirm"


async def test_mqtt_session_unauthorized_hook_called(
hass: HomeAssistant,
mock_roborock_entry: MockConfigEntry,
device_manager: AsyncMock,
) -> None:
"""Test that the mqtt session unauthorized hook is called on unauthorized event."""
device_manager_kwargs = {}

def create_device_manager(*args: Any, **kwargs: Any) -> AsyncMock:
nonlocal device_manager_kwargs
device_manager_kwargs = kwargs
return device_manager

with patch(
"homeassistant.components.roborock.create_device_manager",
side_effect=create_device_manager,
):
await hass.config_entries.async_setup(mock_roborock_entry.entry_id)
await hass.async_block_till_done()
assert mock_roborock_entry.state is ConfigEntryState.LOADED

flows = hass.config_entries.flow.async_progress()
assert not flows

# Simulate an unauthorized event by calling the captured hook
assert device_manager_kwargs
mqtt_session_unauthorized_hook = device_manager_kwargs.get(
"mqtt_session_unauthorized_hook"
)
assert mqtt_session_unauthorized_hook
mqtt_session_unauthorized_hook()
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After calling the mqtt_session_unauthorized_hook, you need to call await hass.async_block_till_done() to allow the async task created by async_start_reauth to complete. The async_start_reauth method creates a task via hass.async_create_task, and without waiting for pending tasks to complete, the reauth flow may not be fully initialized when you check for it.

Add await hass.async_block_till_done() after line 125 and before checking for flows.

Suggested change
mqtt_session_unauthorized_hook()
mqtt_session_unauthorized_hook()
await hass.async_block_till_done()

Copilot uses AI. Check for mistakes.

# Verify that reauth flow is started
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["step_id"] == "reauth_confirm"


@pytest.mark.parametrize("platforms", [[Platform.IMAGE]])
@pytest.mark.parametrize(
("exists", "is_dir", "rmtree_called"),
Expand Down
Loading