From 25bdb5304d9bf51d51f98f38bb25aee3844fba70 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 26 Mar 2022 02:15:47 -1000 Subject: [PATCH] Ensure solaredge can still be setup with an ignored entry (#68688) --- .../components/solaredge/config_flow.py | 22 +++++++-------- .../components/solaredge/test_config_flow.py | 27 ++++++++++++++++++- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/solaredge/config_flow.py b/homeassistant/components/solaredge/config_flow.py index 523c75116e9..58e5577ccfa 100644 --- a/homeassistant/components/solaredge/config_flow.py +++ b/homeassistant/components/solaredge/config_flow.py @@ -9,22 +9,13 @@ import voluptuous as vol 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.""" @@ -34,9 +25,18 @@ class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """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.""" diff --git a/tests/components/solaredge/test_config_flow.py b/tests/components/solaredge/test_config_flow.py index 059e10c7662..d0f0ac01235 100644 --- a/tests/components/solaredge/test_config_flow.py +++ b/tests/components/solaredge/test_config_flow.py @@ -6,7 +6,7 @@ from requests.exceptions import ConnectTimeout, HTTPError 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 @@ -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."""