-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration flow to Whois (#63069)
Co-authored-by: Dave T <17680170+davet2001@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
255 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
"""The whois component.""" | ||
"""The Whois integration.""" | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .const import PLATFORMS | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up from a config entry.""" | ||
hass.config_entries.async_setup_platforms(entry, PLATFORMS) | ||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload a config entry.""" | ||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Config flow to configure the Whois integration.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigFlow | ||
from homeassistant.const import CONF_DOMAIN, CONF_NAME | ||
from homeassistant.data_entry_flow import FlowResult | ||
|
||
from .const import DOMAIN | ||
|
||
|
||
class WhoisFlowHandler(ConfigFlow, domain=DOMAIN): | ||
"""Config flow for Whois.""" | ||
|
||
VERSION = 1 | ||
|
||
imported_name: str | None = None | ||
|
||
async def async_step_user( | ||
self, user_input: dict[str, Any] | None = None | ||
) -> FlowResult: | ||
"""Handle a flow initialized by the user.""" | ||
if user_input is not None: | ||
await self.async_set_unique_id(user_input[CONF_DOMAIN].lower()) | ||
self._abort_if_unique_id_configured() | ||
return self.async_create_entry( | ||
title=self.imported_name or user_input[CONF_DOMAIN], | ||
data={ | ||
CONF_DOMAIN: user_input[CONF_DOMAIN].lower(), | ||
}, | ||
) | ||
|
||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_DOMAIN): str, | ||
} | ||
), | ||
) | ||
|
||
async def async_step_import(self, config: dict[str, Any]) -> FlowResult: | ||
"""Handle a flow initialized by importing a config.""" | ||
self.imported_name = config[CONF_NAME] | ||
return await self.async_step_user( | ||
user_input={ | ||
CONF_DOMAIN: config[CONF_DOMAIN], | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"data": { | ||
"domain": "Domain name" | ||
} | ||
} | ||
}, | ||
"abort": { | ||
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"config": { | ||
"abort": { | ||
"already_configured": "Service is already configured" | ||
}, | ||
"step": { | ||
"user": { | ||
"data": { | ||
"domain": "Domain name" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,6 +347,7 @@ | |
"waze_travel_time", | ||
"wemo", | ||
"whirlpool", | ||
"whois", | ||
"wiffi", | ||
"wilight", | ||
"withings", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Tests for the Whois integration.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Fixtures for Whois integration tests.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.whois.const import DOMAIN | ||
from homeassistant.const import CONF_DOMAIN | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
@pytest.fixture | ||
def mock_config_entry() -> MockConfigEntry: | ||
"""Return the default mocked config entry.""" | ||
return MockConfigEntry( | ||
title="Home Assistant", | ||
domain=DOMAIN, | ||
data={ | ||
CONF_DOMAIN: "Home-Assistant.io", | ||
}, | ||
unique_id="home-assistant.io", | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mock_setup_entry() -> Generator[AsyncMock, None, None]: | ||
"""Mock setting up a config entry.""" | ||
with patch( | ||
"homeassistant.components.whois.async_setup_entry", return_value=True | ||
) as mock_setup: | ||
yield mock_setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""Tests for the Whois config flow.""" | ||
|
||
from unittest.mock import AsyncMock | ||
|
||
from homeassistant.components.whois.const import DOMAIN | ||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER | ||
from homeassistant.const import CONF_DOMAIN, CONF_NAME | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.data_entry_flow import ( | ||
RESULT_TYPE_ABORT, | ||
RESULT_TYPE_CREATE_ENTRY, | ||
RESULT_TYPE_FORM, | ||
) | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def test_full_user_flow( | ||
hass: HomeAssistant, | ||
mock_setup_entry: AsyncMock, | ||
) -> None: | ||
"""Test the full user configuration flow.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER} | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_FORM | ||
assert result.get("step_id") == SOURCE_USER | ||
assert "flow_id" in result | ||
|
||
result2 = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={CONF_DOMAIN: "Example.com"}, | ||
) | ||
|
||
assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY | ||
assert result2.get("title") == "Example.com" | ||
assert result2.get("data") == {CONF_DOMAIN: "example.com"} | ||
|
||
assert len(mock_setup_entry.mock_calls) == 1 | ||
|
||
|
||
async def test_already_configured( | ||
hass: HomeAssistant, | ||
mock_setup_entry: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test we abort if already configured.""" | ||
mock_config_entry.add_to_hass(hass) | ||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, | ||
context={"source": SOURCE_USER}, | ||
data={CONF_DOMAIN: "HOME-Assistant.io"}, | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_ABORT | ||
assert result.get("reason") == "already_configured" | ||
|
||
assert len(mock_setup_entry.mock_calls) == 0 | ||
|
||
|
||
async def test_import_flow( | ||
hass: HomeAssistant, | ||
mock_setup_entry: AsyncMock, | ||
) -> None: | ||
"""Test the import configuration flow.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, | ||
context={"source": SOURCE_IMPORT}, | ||
data={CONF_DOMAIN: "Example.com", CONF_NAME: "My Example Domain"}, | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY | ||
assert result.get("title") == "My Example Domain" | ||
assert result.get("data") == { | ||
CONF_DOMAIN: "example.com", | ||
} | ||
|
||
assert len(mock_setup_entry.mock_calls) == 1 |