Allow manually setting up the Thread integration (#86087)

This commit is contained in:
Erik Montnemery 2023-01-17 18:50:29 +01:00 committed by GitHub
parent 65c4e63e30
commit cf68d081ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -1,8 +1,11 @@
"""Config flow for the Open Thread Border Router integration."""
from __future__ import annotations
import voluptuous as vol
from homeassistant.components.hassio import HassioServiceInfo
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_URL
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN
@ -13,6 +16,22 @@ class OTBRConfigFlow(ConfigFlow, domain=DOMAIN):
VERSION = 1
async def async_step_user(
self, user_input: dict[str, str] | None = None
) -> FlowResult:
"""Set up by user."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
if user_input is not None:
return self.async_create_entry(
title="Thread",
data={"url": user_input[CONF_URL]},
)
data_schema = vol.Schema({CONF_URL: str})
return self.async_show_form(step_id="user", data_schema=data_schema)
async def async_step_hassio(self, discovery_info: HassioServiceInfo) -> FlowResult:
"""Handle hassio discovery."""
if self._async_current_entries():

View File

@ -0,0 +1,15 @@
{
"config": {
"step": {
"user": {
"data": {
"url": "[%key:common::config_flow::data::url%]"
},
"description": "Provide URL for the Open Thread Border Router's REST API"
}
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
}
}
}

View File

@ -14,6 +14,40 @@ HASSIO_DATA = hassio.HassioServiceInfo(
)
async def test_user_flow(hass: HomeAssistant) -> None:
"""Test the user flow."""
result = await hass.config_entries.flow.async_init(
otbr.DOMAIN, context={"source": "user"}
)
expected_data = {"url": "http://custom_url:1234"}
assert result["type"] == FlowResultType.FORM
assert result["errors"] is None
with patch(
"homeassistant.components.otbr.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"url": "http://custom_url:1234",
},
)
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "Thread"
assert result["data"] == expected_data
assert result["options"] == {}
assert len(mock_setup_entry.mock_calls) == 1
config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0]
assert config_entry.data == expected_data
assert config_entry.options == {}
assert config_entry.title == "Thread"
assert config_entry.unique_id is None
async def test_hassio_discovery_flow(hass: HomeAssistant) -> None:
"""Test the hassio discovery flow."""
with patch(