Automatically onboard WLED (#73853)

pull/73867/head
Franck Nijhof 2022-06-22 22:37:36 +02:00 committed by GitHub
parent 320ef55085
commit ec119ae718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 9 deletions

View File

@ -6,7 +6,7 @@ from typing import Any
import voluptuous as vol
from wled import WLED, Device, WLEDConnectionError
from homeassistant.components import zeroconf
from homeassistant.components import onboarding, zeroconf
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_HOST, CONF_MAC
from homeassistant.core import callback
@ -97,7 +97,7 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initiated by zeroconf."""
if user_input is not None:
if user_input is not None or not onboarding.async_is_onboarded(self.hass):
return self.async_create_entry(
title=self.discovered_device.info.name,
data={

View File

@ -1,7 +1,7 @@
"""Fixtures for WLED integration tests."""
from collections.abc import Generator
import json
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from wled import Device as WLEDDevice
@ -25,10 +25,22 @@ def mock_config_entry() -> MockConfigEntry:
@pytest.fixture
def mock_setup_entry() -> Generator[None, None, None]:
def mock_setup_entry() -> Generator[None, AsyncMock, None]:
"""Mock setting up a config entry."""
with patch("homeassistant.components.wled.async_setup_entry", return_value=True):
yield
with patch(
"homeassistant.components.wled.async_setup_entry", return_value=True
) as mock_setup:
yield mock_setup
@pytest.fixture
def mock_onboarding() -> Generator[None, MagicMock, None]:
"""Mock that Home Assistant is currently onboarding."""
with patch(
"homeassistant.components.onboarding.async_is_onboarded",
return_value=False,
) as mock_onboarding:
yield mock_onboarding
@pytest.fixture

View File

@ -1,5 +1,5 @@
"""Tests for the WLED config flow."""
from unittest.mock import MagicMock
from unittest.mock import AsyncMock, MagicMock
from wled import WLEDConnectionError
@ -18,7 +18,7 @@ from tests.common import MockConfigEntry
async def test_full_user_flow_implementation(
hass: HomeAssistant, mock_wled_config_flow: MagicMock, mock_setup_entry: None
hass: HomeAssistant, mock_wled_config_flow: MagicMock, mock_setup_entry: AsyncMock
) -> None:
"""Test the full manual user flow from start to finish."""
result = await hass.config_entries.flow.async_init(
@ -43,7 +43,7 @@ async def test_full_user_flow_implementation(
async def test_full_zeroconf_flow_implementation(
hass: HomeAssistant, mock_wled_config_flow: MagicMock, mock_setup_entry: None
hass: HomeAssistant, mock_wled_config_flow: MagicMock, mock_setup_entry: AsyncMock
) -> None:
"""Test the full manual user flow from start to finish."""
result = await hass.config_entries.flow.async_init(
@ -84,6 +84,38 @@ async def test_full_zeroconf_flow_implementation(
assert result2["result"].unique_id == "aabbccddeeff"
async def test_zeroconf_during_onboarding(
hass: HomeAssistant,
mock_wled_config_flow: MagicMock,
mock_setup_entry: AsyncMock,
mock_onboarding: MagicMock,
) -> None:
"""Test we create a config entry when discovered during onboarding."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.123",
addresses=["192.168.1.123"],
hostname="example.local.",
name="mock_name",
port=None,
properties={CONF_MAC: "aabbccddeeff"},
type="mock_type",
),
)
assert result.get("title") == "WLED RGB Light"
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY
assert result.get("data") == {CONF_HOST: "192.168.1.123"}
assert "result" in result
assert result["result"].unique_id == "aabbccddeeff"
assert len(mock_setup_entry.mock_calls) == 1
assert len(mock_onboarding.mock_calls) == 1
async def test_connection_error(
hass: HomeAssistant, mock_wled_config_flow: MagicMock
) -> None: