diff --git a/homeassistant/components/otbr/config_flow.py b/homeassistant/components/otbr/config_flow.py index f4e129c68ea..56daba335b6 100644 --- a/homeassistant/components/otbr/config_flow.py +++ b/homeassistant/components/otbr/config_flow.py @@ -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(): diff --git a/homeassistant/components/otbr/strings.json b/homeassistant/components/otbr/strings.json new file mode 100644 index 00000000000..76823df4f89 --- /dev/null +++ b/homeassistant/components/otbr/strings.json @@ -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%]" + } + } +} diff --git a/tests/components/otbr/test_config_flow.py b/tests/components/otbr/test_config_flow.py index a4508585716..2580ab2e24a 100644 --- a/tests/components/otbr/test_config_flow.py +++ b/tests/components/otbr/test_config_flow.py @@ -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(