Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions homeassistant/components/solaredge/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@

from homeassistant import config_entries
from homeassistant.const import CONF_API_KEY, CONF_NAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.util import slugify

from .const import CONF_SITE_ID, DEFAULT_NAME, DOMAIN


@callback
def solaredge_entries(hass: HomeAssistant):
"""Return the site_ids for the domain."""
return {
(entry.data[CONF_SITE_ID])
for entry in hass.config_entries.async_entries(DOMAIN)
}


class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow."""

Expand All @@ -34,9 +25,18 @@ def __init__(self) -> None:
"""Initialize the config flow."""
self._errors = {}

@callback
def _async_current_site_ids(self) -> set[str]:
"""Return the site_ids for the domain."""
return {
entry.data[CONF_SITE_ID]
for entry in self._async_current_entries(include_ignore=False)
if CONF_SITE_ID in entry.data
}

def _site_in_configuration_exists(self, site_id: str) -> bool:
"""Return True if site_id exists in configuration."""
return site_id in solaredge_entries(self.hass)
return site_id in self._async_current_site_ids()

def _check_site(self, site_id: str, api_key: str) -> bool:
"""Check if we can connect to the soleredge api service."""
Expand Down
27 changes: 26 additions & 1 deletion tests/components/solaredge/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant import data_entry_flow
from homeassistant.components.solaredge.const import CONF_SITE_ID, DEFAULT_NAME, DOMAIN
from homeassistant.config_entries import SOURCE_USER
from homeassistant.config_entries import SOURCE_IGNORE, SOURCE_USER
from homeassistant.const import CONF_API_KEY, CONF_NAME
from homeassistant.core import HomeAssistant

Expand Down Expand Up @@ -66,6 +66,31 @@ async def test_abort_if_already_setup(hass: HomeAssistant, test_api: str) -> Non
assert result.get("errors") == {CONF_SITE_ID: "already_configured"}


async def test_ignored_entry_does_not_cause_error(
hass: HomeAssistant, test_api: str
) -> None:
"""Test an ignored entry does not cause and error and we can still create an new entry."""
MockConfigEntry(
domain="solaredge",
data={CONF_NAME: DEFAULT_NAME, CONF_API_KEY: API_KEY},
source=SOURCE_IGNORE,
).add_to_hass(hass)

# user: Should fail, same SITE_ID
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={CONF_NAME: "test", CONF_SITE_ID: SITE_ID, CONF_API_KEY: "test"},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test"

data = result["data"]
assert data
assert data[CONF_SITE_ID] == SITE_ID
assert data[CONF_API_KEY] == "test"


async def test_asserts(hass: HomeAssistant, test_api: Mock) -> None:
"""Test the _site_in_configuration_exists method."""

Expand Down