1 """Config flow for Tesla Powerwall integration."""
3 from __future__
import annotations
6 from collections.abc
import Mapping
10 from aiohttp
import CookieJar
11 from tesla_powerwall
import (
13 MissingAttributeError,
15 PowerwallUnreachableError,
18 import voluptuous
as vol
33 from .
import async_last_update_was_successful
34 from .const
import DOMAIN
36 _LOGGER = logging.getLogger(__name__)
39 ENTRY_FAILURE_STATES = {
40 ConfigEntryState.SETUP_ERROR,
41 ConfigEntryState.SETUP_RETRY,
46 power_wall: Powerwall, password: str
47 ) -> tuple[SiteInfoResponse, str]:
48 """Login to the powerwall and fetch the base info."""
49 if password
is not None:
50 await power_wall.login(password)
52 return await asyncio.gather(
53 power_wall.get_site_info(), power_wall.get_gateway_din()
58 """Check if the powerwall is reachable."""
60 async
with Powerwall(ip_address)
as power_wall:
61 await power_wall.login(password)
62 except AccessDeniedError:
64 except PowerwallUnreachableError:
69 async
def validate_input(hass: HomeAssistant, data: dict[str, str]) -> dict[str, str]:
70 """Validate the user input allows us to connect.
72 Data has the keys from schema with values provided by the user.
75 hass, verify_ssl=
False, cookie_jar=CookieJar(unsafe=
True)
77 async
with Powerwall(data[CONF_IP_ADDRESS], http_session=session)
as power_wall:
78 password = data[CONF_PASSWORD]
84 except MissingAttributeError
as err:
86 _LOGGER.error(
str(err))
87 raise WrongVersion
from err
90 return {
"title": site_info.site_name,
"unique_id": gateway_din.upper()}
94 """Handle a config flow for Tesla Powerwall."""
99 """Initialize the powerwall flow."""
101 self.
titletitle: str |
None =
None
104 """Check if the power wall is offline.
106 We define offline by the config entry
107 is in a failure/retry state or the updates
108 are failing and the powerwall is unreachable
109 since device may be updating.
111 ip_address = entry.data[CONF_IP_ADDRESS]
112 password = entry.data[CONF_PASSWORD]
114 entry.state
in ENTRY_FAILURE_STATES
119 self, discovery_info: dhcp.DhcpServiceInfo
120 ) -> ConfigFlowResult:
121 """Handle dhcp discovery."""
123 gateway_din = discovery_info.hostname.upper()
127 if entry.data[CONF_IP_ADDRESS] == discovery_info.ip:
128 if entry.unique_id
is not None and is_ip_address(entry.unique_id):
129 if self.hass.config_entries.async_update_entry(
130 entry, unique_id=gateway_din
132 self.hass.config_entries.async_schedule_reload(entry.entry_id)
134 if entry.unique_id == gateway_din:
136 if self.hass.config_entries.async_update_entry(
137 entry, data={**entry.data, CONF_IP_ADDRESS: self.
ip_addressip_address}
139 self.hass.config_entries.async_schedule_reload(entry.entry_id)
143 self.context[
"title_placeholders"] = {
148 {CONF_IP_ADDRESS: self.
ip_addressip_address, CONF_PASSWORD: gateway_din[-5:]}
151 if CONF_PASSWORD
in errors:
156 assert info
is not None
161 self, user_input: dict[str, Any]
162 ) -> tuple[dict[str, Any] |
None, dict[str, str] |
None, dict[str, str]]:
163 """Try to connect to the powerwall."""
165 errors: dict[str, str] = {}
166 description_placeholders: dict[str, str] = {}
169 except (PowerwallUnreachableError, TimeoutError)
as ex:
170 errors[CONF_IP_ADDRESS] =
"cannot_connect"
171 description_placeholders = {
"error":
str(ex)}
172 except WrongVersion
as ex:
173 errors[
"base"] =
"wrong_version"
174 description_placeholders = {
"error":
str(ex)}
175 except AccessDeniedError
as ex:
176 errors[CONF_PASSWORD] =
"invalid_auth"
177 description_placeholders = {
"error":
str(ex)}
178 except Exception
as ex:
179 _LOGGER.exception(
"Unexpected exception")
180 errors[
"base"] =
"unknown"
181 description_placeholders = {
"error":
str(ex)}
183 return errors, info, description_placeholders
186 self, user_input: dict[str, Any] |
None =
None
187 ) -> ConfigFlowResult:
188 """Confirm a discovered powerwall."""
190 assert self.
titletitle
is not None
191 assert self.
unique_idunique_id
is not None
192 if user_input
is not None:
194 title=self.
titletitle,
197 CONF_PASSWORD: self.
unique_idunique_id[-5:],
202 self.context[
"title_placeholders"] = {
203 "name": self.
titletitle,
207 step_id=
"confirm_discovery",
208 description_placeholders={
209 "name": self.
titletitle,
215 self, user_input: dict[str, Any] |
None =
None
216 ) -> ConfigFlowResult:
217 """Handle the initial step."""
218 errors: dict[str, str] |
None = {}
219 description_placeholders: dict[str, str] = {}
220 if user_input
is not None:
221 errors, info, description_placeholders = await self.
_async_try_connect_async_try_connect(
225 assert info
is not None
226 if info[
"unique_id"]:
228 info[
"unique_id"], raise_on_progress=
False
231 updates={CONF_IP_ADDRESS: user_input[CONF_IP_ADDRESS]}
238 data_schema=vol.Schema(
240 vol.Required(CONF_IP_ADDRESS, default=self.
ip_addressip_address): str,
241 vol.Optional(CONF_PASSWORD): str,
245 description_placeholders=description_placeholders,
249 self, user_input: dict[str, Any] |
None =
None
250 ) -> ConfigFlowResult:
251 """Handle reauth confirmation."""
252 errors: dict[str, str] |
None = {}
253 description_placeholders: dict[str, str] = {}
255 if user_input
is not None:
256 errors, _, description_placeholders = await self.
_async_try_connect_async_try_connect(
257 {CONF_IP_ADDRESS: reauth_entry.data[CONF_IP_ADDRESS], **user_input}
261 reauth_entry, data_updates=user_input
264 self.context[
"title_placeholders"] = {
265 "name": reauth_entry.title,
266 "ip_address": reauth_entry.data[CONF_IP_ADDRESS],
269 step_id=
"reauth_confirm",
270 data_schema=vol.Schema({vol.Optional(CONF_PASSWORD): str}),
272 description_placeholders=description_placeholders,
276 self, entry_data: Mapping[str, Any]
277 ) -> ConfigFlowResult:
278 """Handle configuration by re-auth."""
283 """Error indicating we cannot interact with the powerwall software version."""
bool _async_powerwall_is_offline(self, ConfigEntry entry)
ConfigFlowResult async_step_confirm_discovery(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
tuple[dict[str, Any]|None, dict[str, str]|None, dict[str, str]] _async_try_connect(self, dict[str, Any] user_input)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
None _set_confirm_only(self)
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
bool _powerwall_is_reachable(str ip_address, str password)
tuple[SiteInfoResponse, str] _login_and_fetch_site_info(Powerwall power_wall, str password)
dict[str, str] validate_input(HomeAssistant hass, dict[str, str] data)
bool async_last_update_was_successful(HomeAssistant hass, PowerwallConfigEntry entry)
aiohttp.ClientSession async_create_clientsession()
bool is_ip_address(str address)