Automatically set up Awair during onboarding (#78632)

This commit is contained in:
Franck Nijhof 2022-09-17 15:04:09 +02:00 committed by GitHub
parent 9acea07d31
commit 24df3574bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -10,7 +10,7 @@ from python_awair.exceptions import AuthError, AwairError
from python_awair.user import AwairUser
import voluptuous as vol
from homeassistant.components import zeroconf
from homeassistant.components import onboarding, zeroconf
from homeassistant.config_entries import SOURCE_ZEROCONF, ConfigFlow
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_DEVICE, CONF_HOST
from homeassistant.core import callback
@ -60,7 +60,7 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Confirm discovery."""
if user_input is not None:
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
title = f"{self._device.model} ({self._device.device_id})"
return self.async_create_entry(
title=title,

View File

@ -399,3 +399,29 @@ async def test_zeroconf_discovery_update_configuration(
assert config_entry.data[CONF_HOST] == ZEROCONF_DISCOVERY.host
assert mock_setup_entry.call_count == 0
async def test_zeroconf_during_onboarding(
hass: HomeAssistant, local_devices: Any
) -> None:
"""Test the zeroconf creates an entry during onboarding."""
with patch(
"homeassistant.components.awair.async_setup_entry",
return_value=True,
) as mock_setup_entry, patch(
"python_awair.AwairClient.query", side_effect=[local_devices]
), patch(
"homeassistant.components.onboarding.async_is_onboarded",
return_value=False,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=ZEROCONF_DISCOVERY
)
assert result.get("type") == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result.get("title") == "Awair Element (24947)"
assert "data" in result
assert result["data"][CONF_HOST] == ZEROCONF_DISCOVERY.host
assert "result" in result
assert result["result"].unique_id == LOCAL_UNIQUE_ID
assert len(mock_setup_entry.mock_calls) == 1