Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DiscoveryFlowHandler when discovery_function returns bool #133563

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions homeassistant/helpers/config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ async def async_step_confirm(
in_progress = self._async_in_progress()

if not (has_devices := bool(in_progress)):
has_devices = await cast(
"asyncio.Future[bool]", self._discovery_function(self.hass)
)
discovery_result = self._discovery_function(self.hass)
if isinstance(discovery_result, bool):
has_devices = discovery_result
else:
has_devices = await cast("asyncio.Future[bool]", discovery_result)

if not has_devices:
return self.async_abort(reason="no_devices_found")
Expand Down
18 changes: 14 additions & 4 deletions tests/helpers/test_config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@
from tests.common import MockConfigEntry, MockModule, mock_integration, mock_platform


@pytest.fixture
def discovery_flow_conf(hass: HomeAssistant) -> Generator[dict[str, bool]]:
@pytest.fixture(params=["async", "sync"])
def discovery_flow_conf(
request: pytest.FixtureRequest, hass: HomeAssistant
) -> Generator[dict[str, bool]]:
"""Register a handler."""
handler_conf = {"discovered": False}

async def has_discovered_devices(hass: HomeAssistant) -> bool:
async def has_discovered_devices_async(hass: HomeAssistant) -> bool:
"""Mock if we have discovered devices."""
return handler_conf["discovered"]

def has_discovered_devices_sync(hass: HomeAssistant) -> bool:
"""Mock if we have discovered devices."""
return handler_conf["discovered"]

with patch.dict(config_entries.HANDLERS):
config_entry_flow.register_discovery_flow(
"test", "Test", has_discovered_devices
"test",
"Test",
has_discovered_devices_sync
if request.param == "sync"
else has_discovered_devices_async,
bdraco marked this conversation as resolved.
Show resolved Hide resolved
)
yield handler_conf

Expand Down