forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config flow for sun (home-assistant#68295)
Co-authored-by: Franck Nijhof <[email protected]>
- Loading branch information
1 parent
bc862e9
commit 2fcced3
Showing
8 changed files
with
236 additions
and
8 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
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,31 @@ | ||
"""Config flow to configure the Sun integration.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from homeassistant.config_entries import ConfigFlow | ||
from homeassistant.data_entry_flow import FlowResult | ||
|
||
from .const import DEFAULT_NAME, DOMAIN | ||
|
||
|
||
class SunConfigFlow(ConfigFlow, domain=DOMAIN): | ||
"""Config flow for Sun.""" | ||
|
||
VERSION = 1 | ||
|
||
async def async_step_user( | ||
self, user_input: dict[str, Any] | None = None | ||
) -> FlowResult: | ||
"""Handle a flow initialized by the 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=DEFAULT_NAME, data={}) | ||
|
||
return self.async_show_form(step_id="user") | ||
|
||
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult: | ||
"""Handle import from configuration.yaml.""" | ||
return await self.async_step_user(user_input) |
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,6 @@ | ||
"""Constants for the Sun integration.""" | ||
from typing import Final | ||
|
||
DOMAIN: Final = "sun" | ||
|
||
DEFAULT_NAME: Final = "Sun" |
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 |
---|---|---|
|
@@ -323,6 +323,7 @@ | |
"steamist", | ||
"stookalert", | ||
"subaru", | ||
"sun", | ||
"surepetcare", | ||
"switch_as_x", | ||
"switchbot", | ||
|
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,75 @@ | ||
"""Tests for the Sun config flow.""" | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.sun.const import DOMAIN | ||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER | ||
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) -> 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 | ||
|
||
with patch( | ||
"homeassistant.components.sun.async_setup_entry", | ||
return_value=True, | ||
) as mock_setup_entry: | ||
result = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={}, | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY | ||
assert result.get("title") == "Sun" | ||
assert result.get("data") == {} | ||
assert result.get("options") == {} | ||
assert len(mock_setup_entry.mock_calls) == 1 | ||
|
||
|
||
@pytest.mark.parametrize("source", [SOURCE_USER, SOURCE_IMPORT]) | ||
async def test_single_instance_allowed( | ||
hass: HomeAssistant, | ||
source: str, | ||
) -> None: | ||
"""Test we abort if already setup.""" | ||
mock_config_entry = MockConfigEntry(domain=DOMAIN) | ||
|
||
mock_config_entry.add_to_hass(hass) | ||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": source} | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_ABORT | ||
assert result.get("reason") == "single_instance_allowed" | ||
|
||
|
||
async def test_import_flow( | ||
hass: HomeAssistant, | ||
) -> None: | ||
"""Test the import configuration flow.""" | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, | ||
context={"source": SOURCE_IMPORT}, | ||
data={}, | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_CREATE_ENTRY | ||
assert result.get("title") == "Sun" | ||
assert result.get("data") == {} | ||
assert result.get("options") == {} |
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