mirror of
https://github.com/home-assistant/core.git
synced 2025-04-22 16:27:56 +00:00
Use references in config flow for solaredge (#43511)
This commit is contained in:
parent
0c30abda61
commit
059f1a35f2
@ -41,15 +41,14 @@ class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
api = solaredge.Solaredge(api_key)
|
||||
try:
|
||||
response = api.get_details(site_id)
|
||||
except (ConnectTimeout, HTTPError):
|
||||
self._errors[CONF_SITE_ID] = "could_not_connect"
|
||||
return False
|
||||
try:
|
||||
if response["details"]["status"].lower() != "active":
|
||||
self._errors[CONF_SITE_ID] = "site_not_active"
|
||||
return False
|
||||
except (ConnectTimeout, HTTPError):
|
||||
self._errors[CONF_SITE_ID] = "could_not_connect"
|
||||
return False
|
||||
except KeyError:
|
||||
self._errors[CONF_SITE_ID] = "api_failure"
|
||||
self._errors[CONF_SITE_ID] = "invalid_api_key"
|
||||
return False
|
||||
return True
|
||||
|
||||
@ -59,7 +58,7 @@ class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if user_input is not None:
|
||||
name = slugify(user_input.get(CONF_NAME, DEFAULT_NAME))
|
||||
if self._site_in_configuration_exists(user_input[CONF_SITE_ID]):
|
||||
self._errors[CONF_SITE_ID] = "site_exists"
|
||||
self._errors[CONF_SITE_ID] = "already_configured"
|
||||
else:
|
||||
site = user_input[CONF_SITE_ID]
|
||||
api = user_input[CONF_API_KEY]
|
||||
@ -94,5 +93,5 @@ class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
async def async_step_import(self, user_input=None):
|
||||
"""Import a config entry."""
|
||||
if self._site_in_configuration_exists(user_input[CONF_SITE_ID]):
|
||||
return self.async_abort(reason="site_exists")
|
||||
return self.async_abort(reason="already_configured")
|
||||
return await self.async_step_user(user_input)
|
||||
|
@ -11,10 +11,13 @@
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"site_exists": "This site_id is already configured"
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]",
|
||||
"site_not_active": "The site is not active",
|
||||
"could_not_connect": "Could not connect to the solaredge API"
|
||||
},
|
||||
"abort": {
|
||||
"site_exists": "This site_id is already configured"
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -85,14 +85,14 @@ async def test_abort_if_already_setup(hass, test_api):
|
||||
{CONF_NAME: DEFAULT_NAME, CONF_SITE_ID: SITE_ID, CONF_API_KEY: API_KEY}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "site_exists"
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
# user: Should fail, same SITE_ID
|
||||
result = await flow.async_step_user(
|
||||
{CONF_NAME: "test", CONF_SITE_ID: SITE_ID, CONF_API_KEY: "test"}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {CONF_SITE_ID: "site_exists"}
|
||||
assert result["errors"] == {CONF_SITE_ID: "already_configured"}
|
||||
|
||||
|
||||
async def test_asserts(hass, test_api):
|
||||
@ -113,7 +113,7 @@ async def test_asserts(hass, test_api):
|
||||
{CONF_NAME: NAME, CONF_API_KEY: API_KEY, CONF_SITE_ID: SITE_ID}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {CONF_SITE_ID: "api_failure"}
|
||||
assert result["errors"] == {CONF_SITE_ID: "invalid_api_key"}
|
||||
|
||||
# test with ConnectionTimeout
|
||||
test_api.get_details.side_effect = ConnectTimeout()
|
||||
|
Loading…
x
Reference in New Issue
Block a user