mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Ensure solaredge can still be setup with an ignored entry (#68688)
This commit is contained in:
parent
23a567e135
commit
25bdb5304d
@ -9,22 +9,13 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_NAME
|
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.data_entry_flow import FlowResult
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
from .const import CONF_SITE_ID, DEFAULT_NAME, DOMAIN
|
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):
|
class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow."""
|
"""Handle a config flow."""
|
||||||
|
|
||||||
@ -34,9 +25,18 @@ class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"""Initialize the config flow."""
|
"""Initialize the config flow."""
|
||||||
self._errors = {}
|
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:
|
def _site_in_configuration_exists(self, site_id: str) -> bool:
|
||||||
"""Return True if site_id exists in configuration."""
|
"""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:
|
def _check_site(self, site_id: str, api_key: str) -> bool:
|
||||||
"""Check if we can connect to the soleredge api service."""
|
"""Check if we can connect to the soleredge api service."""
|
||||||
|
@ -6,7 +6,7 @@ from requests.exceptions import ConnectTimeout, HTTPError
|
|||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.solaredge.const import CONF_SITE_ID, DEFAULT_NAME, DOMAIN
|
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.const import CONF_API_KEY, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
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"}
|
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:
|
async def test_asserts(hass: HomeAssistant, test_api: Mock) -> None:
|
||||||
"""Test the _site_in_configuration_exists method."""
|
"""Test the _site_in_configuration_exists method."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user