mirror of
https://github.com/home-assistant/core.git
synced 2025-09-10 07:11:37 +02:00
Abort Nanoleaf discovery flows with user flow (#150818)
This commit is contained in:
committed by
GitHub
parent
a325596898
commit
9138930cb9
@@ -10,7 +10,12 @@ from typing import Any, Final, cast
|
||||
from aionanoleaf import InvalidToken, Nanoleaf, Unauthorized, Unavailable
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.config_entries import (
|
||||
SOURCE_REAUTH,
|
||||
SOURCE_USER,
|
||||
ConfigFlow,
|
||||
ConfigFlowResult,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, CONF_TOKEN
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.json import save_json
|
||||
@@ -200,7 +205,9 @@ class NanoleafConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_abort(reason="unknown")
|
||||
name = self.nanoleaf.name
|
||||
|
||||
await self.async_set_unique_id(name)
|
||||
await self.async_set_unique_id(
|
||||
name, raise_on_progress=self.source != SOURCE_USER
|
||||
)
|
||||
self._abort_if_unique_id_configured({CONF_HOST: self.nanoleaf.host})
|
||||
|
||||
if discovery_integration_import:
|
||||
|
@@ -10,6 +10,7 @@ import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.nanoleaf.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_HOST, CONF_TOKEN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
@@ -463,3 +464,59 @@ async def test_ssdp_discovery(hass: HomeAssistant) -> None:
|
||||
}
|
||||
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_abort_discovery_flow_with_user_flow(hass: HomeAssistant) -> None:
|
||||
"""Test abort discovery flow if user flow is already in progress."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nanoleaf.config_flow.load_json_object",
|
||||
return_value={},
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nanoleaf.config_flow.Nanoleaf",
|
||||
return_value=_mock_nanoleaf(TEST_HOST, TEST_TOKEN),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nanoleaf.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_SSDP},
|
||||
data=SsdpServiceInfo(
|
||||
ssdp_usn="mock_usn",
|
||||
ssdp_st="mock_st",
|
||||
upnp={},
|
||||
ssdp_headers={
|
||||
"_host": TEST_HOST,
|
||||
"nl-devicename": TEST_NAME,
|
||||
"nl-deviceid": TEST_DEVICE_ID,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
assert result["step_id"] == "link"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
)
|
||||
assert len(hass.config_entries.flow.async_progress(DOMAIN)) == 2
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {CONF_HOST: TEST_HOST}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "link"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
|
||||
# Verify the discovery flow was aborted
|
||||
assert not hass.config_entries.flow.async_progress(DOMAIN)
|
||||
|
Reference in New Issue
Block a user