1 """Config flow for the Amber Electric integration."""
3 from __future__
import annotations
6 from amberelectric.models.site
import Site
7 from amberelectric.models.site_status
import SiteStatus
8 import voluptuous
as vol
19 from .const
import CONF_SITE_ID, CONF_SITE_NAME, DOMAIN
21 API_URL =
"https://app.amber.com.au/developers"
25 """Generate the name to show in the site drop down in the configuration flow."""
28 if site.status == SiteStatus.CLOSED:
29 if site.closed_on
is None:
30 return f
"{nmi} (Closed)"
31 return f
"{nmi} (Closed: {site.closed_on.isoformat()})"
32 if site.status == SiteStatus.PENDING:
33 return f
"{nmi} (Pending)"
38 """Deduplicates the list of sites."""
39 filtered: list[Site] = []
40 filtered_nmi: set[str] = set()
42 for site
in sorted(sites, key=
lambda site: site.status):
43 if site.status == SiteStatus.ACTIVE
or site.nmi
not in filtered_nmi:
45 filtered_nmi.add(site.nmi)
51 """Handle a config flow."""
56 """Initialize the config flow."""
57 self.
_errors_errors: dict[str, str] = {}
58 self.
_sites_sites: list[Site] |
None =
None
62 configuration = amberelectric.Configuration(access_token=token)
63 api_client = amberelectric.ApiClient(configuration)
64 api = amberelectric.AmberApi(api_client)
68 except amberelectric.ApiException
as api_exception:
69 if api_exception.status == 403:
70 self.
_errors_errors[CONF_API_TOKEN] =
"invalid_api_token"
72 self.
_errors_errors[CONF_API_TOKEN] =
"unknown_error"
76 self.
_errors_errors[CONF_API_TOKEN] =
"no_site"
81 self, user_input: dict[str, str] |
None =
None
82 ) -> ConfigFlowResult:
83 """Step when user initializes a integration."""
88 if user_input
is not None:
89 token = user_input[CONF_API_TOKEN]
90 self.
_sites_sites = await self.hass.async_add_executor_job(
94 if self.
_sites_sites
is not None:
99 user_input = {CONF_API_TOKEN:
""}
103 description_placeholders={
"api_url": API_URL},
104 data_schema=vol.Schema(
107 CONF_API_TOKEN, default=user_input[CONF_API_TOKEN]
115 self, user_input: dict[str, str] |
None =
None
116 ) -> ConfigFlowResult:
117 """Step to select site."""
120 assert self.
_sites_sites
is not None
123 if user_input
is not None:
124 site_id = user_input[CONF_SITE_ID]
125 name = user_input.get(CONF_SITE_NAME, site_id)
128 data={CONF_SITE_ID: site_id, CONF_API_TOKEN: self.
_api_token_api_token},
133 data_schema=vol.Schema(
142 for site
in self.
_sites_sites
144 mode=SelectSelectorMode.DROPDOWN,
147 vol.Optional(CONF_SITE_NAME): str,
ConfigFlowResult async_step_site(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
list[Site]|None _fetch_sites(self, str token)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
str generate_site_selector_name(Site site)
list[Site] filter_sites(list[Site] sites)