Automatically onboard Wiz (#73851)

This commit is contained in:
Franck Nijhof 2022-06-22 20:17:28 +02:00 committed by GitHub
parent 8d66623036
commit 9229d14962
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 8 deletions

View File

@ -9,7 +9,7 @@ from pywizlight.discovery import DiscoveredBulb
from pywizlight.exceptions import WizLightConnectionError, WizLightTimeOutError from pywizlight.exceptions import WizLightConnectionError, WizLightTimeOutError
import voluptuous as vol import voluptuous as vol
from homeassistant.components import dhcp from homeassistant.components import dhcp, onboarding
from homeassistant.config_entries import ConfigFlow from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_HOST from homeassistant.const import CONF_HOST
from homeassistant.data_entry_flow import AbortFlow, FlowResult from homeassistant.data_entry_flow import AbortFlow, FlowResult
@ -29,11 +29,12 @@ class WizConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1 VERSION = 1
_discovered_device: DiscoveredBulb
_name: str
def __init__(self) -> None: def __init__(self) -> None:
"""Initialize the config flow.""" """Initialize the config flow."""
self._discovered_device: DiscoveredBulb | None = None
self._discovered_devices: dict[str, DiscoveredBulb] = {} self._discovered_devices: dict[str, DiscoveredBulb] = {}
self._name: str | None = None
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult: async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle discovery via dhcp.""" """Handle discovery via dhcp."""
@ -54,7 +55,6 @@ class WizConfigFlow(ConfigFlow, domain=DOMAIN):
async def _async_handle_discovery(self) -> FlowResult: async def _async_handle_discovery(self) -> FlowResult:
"""Handle any discovery.""" """Handle any discovery."""
device = self._discovered_device device = self._discovered_device
assert device is not None
_LOGGER.debug("Discovered device: %s", device) _LOGGER.debug("Discovered device: %s", device)
ip_address = device.ip_address ip_address = device.ip_address
mac = device.mac_address mac = device.mac_address
@ -66,7 +66,6 @@ class WizConfigFlow(ConfigFlow, domain=DOMAIN):
async def _async_connect_discovered_or_abort(self) -> None: async def _async_connect_discovered_or_abort(self) -> None:
"""Connect to the device and verify its responding.""" """Connect to the device and verify its responding."""
device = self._discovered_device device = self._discovered_device
assert device is not None
bulb = wizlight(device.ip_address) bulb = wizlight(device.ip_address)
try: try:
bulbtype = await bulb.get_bulbtype() bulbtype = await bulb.get_bulbtype()
@ -84,10 +83,8 @@ class WizConfigFlow(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> FlowResult: ) -> FlowResult:
"""Confirm discovery.""" """Confirm discovery."""
assert self._discovered_device is not None
assert self._name is not None
ip_address = self._discovered_device.ip_address ip_address = self._discovered_device.ip_address
if user_input is not None: if user_input is not None or not onboarding.async_is_onboarded(self.hass):
# Make sure the device is still there and # Make sure the device is still there and
# update the name if the firmware has auto # update the name if the firmware has auto
# updated since discovery # updated since discovery

View File

@ -506,3 +506,34 @@ async def test_discovery_with_firmware_update(hass):
} }
assert len(mock_setup.mock_calls) == 1 assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.parametrize(
"source, data",
[
(config_entries.SOURCE_DHCP, DHCP_DISCOVERY),
(config_entries.SOURCE_INTEGRATION_DISCOVERY, INTEGRATION_DISCOVERY),
],
)
async def test_discovered_during_onboarding(hass, source, data):
"""Test dhcp or discovery during onboarding creates the config entry."""
with _patch_wizlight(), patch(
"homeassistant.components.wiz.async_setup_entry",
return_value=True,
) as mock_setup_entry, patch(
"homeassistant.components.wiz.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.onboarding.async_is_onboarded", return_value=False
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": source}, data=data
)
await hass.async_block_till_done()
assert result["type"] == "create_entry"
assert result["title"] == "WiZ Dimmable White ABCABC"
assert result["data"] == {
CONF_HOST: "1.1.1.1",
}
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1