2019-09-08 19:49:20 +00:00
|
|
|
"""Config flow for the SolarEdge platform."""
|
2021-04-01 21:59:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2019-12-05 05:18:12 +00:00
|
|
|
from requests.exceptions import ConnectTimeout, HTTPError
|
2019-09-08 19:49:20 +00:00
|
|
|
import solaredge
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_NAME
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-29 11:40:51 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2019-09-08 19:49:20 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
2019-12-05 05:18:12 +00:00
|
|
|
from .const import CONF_SITE_ID, DEFAULT_NAME, DOMAIN
|
2019-09-08 19:49:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def solaredge_entries(hass: HomeAssistant):
|
|
|
|
"""Return the site_ids for the domain."""
|
2020-04-04 18:05:15 +00:00
|
|
|
return {
|
2019-09-08 19:49:20 +00:00
|
|
|
(entry.data[CONF_SITE_ID])
|
|
|
|
for entry in hass.config_entries.async_entries(DOMAIN)
|
2020-04-04 18:05:15 +00:00
|
|
|
}
|
2019-09-08 19:49:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialize the config flow."""
|
|
|
|
self._errors = {}
|
|
|
|
|
2021-04-01 21:59:26 +00:00
|
|
|
def _site_in_configuration_exists(self, site_id: str) -> bool:
|
2019-09-08 19:49:20 +00:00
|
|
|
"""Return True if site_id exists in configuration."""
|
2021-04-01 21:59:26 +00:00
|
|
|
return site_id in solaredge_entries(self.hass)
|
2019-09-08 19:49:20 +00:00
|
|
|
|
2021-04-01 21:59:26 +00:00
|
|
|
def _check_site(self, site_id: str, api_key: str) -> bool:
|
2019-09-08 19:49:20 +00:00
|
|
|
"""Check if we can connect to the soleredge api service."""
|
|
|
|
api = solaredge.Solaredge(api_key)
|
|
|
|
try:
|
|
|
|
response = api.get_details(site_id)
|
|
|
|
if response["details"]["status"].lower() != "active":
|
|
|
|
self._errors[CONF_SITE_ID] = "site_not_active"
|
|
|
|
return False
|
2020-11-23 20:34:46 +00:00
|
|
|
except (ConnectTimeout, HTTPError):
|
|
|
|
self._errors[CONF_SITE_ID] = "could_not_connect"
|
|
|
|
return False
|
2019-09-08 19:49:20 +00:00
|
|
|
except KeyError:
|
2020-11-23 20:34:46 +00:00
|
|
|
self._errors[CONF_SITE_ID] = "invalid_api_key"
|
2019-09-08 19:49:20 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-04-01 21:59:26 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2020-01-31 16:33:00 +00:00
|
|
|
"""Step when user initializes a integration."""
|
2019-09-08 19:49:20 +00:00
|
|
|
self._errors = {}
|
|
|
|
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]):
|
2020-11-23 20:34:46 +00:00
|
|
|
self._errors[CONF_SITE_ID] = "already_configured"
|
2019-09-08 19:49:20 +00:00
|
|
|
else:
|
|
|
|
site = user_input[CONF_SITE_ID]
|
|
|
|
api = user_input[CONF_API_KEY]
|
|
|
|
can_connect = await self.hass.async_add_executor_job(
|
|
|
|
self._check_site, site, api
|
|
|
|
)
|
|
|
|
if can_connect:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=name, data={CONF_SITE_ID: site, CONF_API_KEY: api}
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
2021-04-01 21:59:26 +00:00
|
|
|
user_input = {CONF_NAME: DEFAULT_NAME, CONF_SITE_ID: "", CONF_API_KEY: ""}
|
2019-09-08 19:49:20 +00:00
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(
|
|
|
|
CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME)
|
|
|
|
): str,
|
|
|
|
vol.Required(CONF_SITE_ID, default=user_input[CONF_SITE_ID]): str,
|
|
|
|
vol.Required(CONF_API_KEY, default=user_input[CONF_API_KEY]): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=self._errors,
|
|
|
|
)
|