Allow manual smlight user setup to override discovery (#137136)

Co-authored-by: J. Nick Koston <nick@koston.org>
pull/137183/head
TimL 2025-02-03 03:37:08 +11:00 committed by GitHub
parent a3d0ec4e6e
commit a98109614e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 6 deletions

View File

@ -10,7 +10,7 @@ from pysmlight.const import Devices
from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.config_entries import SOURCE_USER, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.device_registry import format_mac
@ -36,10 +36,7 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for SMLIGHT Zigbee.""" """Handle a config flow for SMLIGHT Zigbee."""
host: str host: str
client: Api2
def __init__(self) -> None:
"""Initialize the config flow."""
self.client: Api2
async def async_step_user( async def async_step_user(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
@ -199,7 +196,10 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] self, user_input: dict[str, Any]
) -> ConfigFlowResult: ) -> ConfigFlowResult:
info = await self.client.get_info() info = await self.client.get_info()
await self.async_set_unique_id(format_mac(info.MAC))
await self.async_set_unique_id(
format_mac(info.MAC), raise_on_progress=self.source != SOURCE_USER
)
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
if user_input.get(CONF_HOST) is None: if user_input.get(CONF_HOST) is None:

View File

@ -261,6 +261,44 @@ async def test_user_device_exists_abort(
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
@pytest.mark.usefixtures("mock_smlight_client")
async def test_user_flow_can_override_discovery(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_setup_entry: AsyncMock,
) -> None:
"""Test manual user flow can override discovery in progress."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "confirm_discovery"
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == SOURCE_USER
assert result["errors"] == {}
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_HOST: MOCK_HOST,
},
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
assert result2["context"]["source"] == SOURCE_USER
assert result2["data"] == {
CONF_HOST: MOCK_HOST,
}
assert result2["context"]["unique_id"] == "aa:bb:cc:dd:ee:ff"
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.usefixtures("mock_smlight_client") @pytest.mark.usefixtures("mock_smlight_client")
async def test_zeroconf_device_exists_abort( async def test_zeroconf_device_exists_abort(
hass: HomeAssistant, mock_config_entry: MockConfigEntry hass: HomeAssistant, mock_config_entry: MockConfigEntry