Skip to content

Commit 352e2c8

Browse files
committed
Rework from core
1 parent 746210f commit 352e2c8

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

custom_components/plugwise/config_flow.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from copy import deepcopy
6+
import logging
67
from typing import Any, Self
78

89
from plugwise import Smile
@@ -69,25 +70,25 @@
6970

7071
type PlugwiseConfigEntry = ConfigEntry[PlugwiseDataUpdateCoordinator]
7172

73+
_LOGGER = logging.getLogger(__name__)
74+
7275
# Upstream basically the whole file (excluding the pw-beta options)
7376

7477
SMILE_RECONF_SCHEMA = vol.Schema(
7578
{
7679
vol.Required(CONF_HOST): str,
77-
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
7880
}
7981
)
8082

8183

82-
def SMILE_USER_SCHEMA(
83-
cf_input: ZeroconfServiceInfo | dict[str, Any] | None,
84-
) -> vol.Schema:
84+
def smile_user_schema(cf_input: ZeroconfServiceInfo | dict[str, Any] | None) -> vol.Schema:
8585
"""Generate base schema for gateways."""
8686
if not cf_input: # no discovery- or user-input available
8787
return vol.Schema(
8888
{
8989
vol.Required(CONF_HOST): str,
9090
vol.Required(CONF_PASSWORD): str,
91+
# Port under investigation for removal (hence not added in #132878)
9192
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
9293
vol.Required(CONF_USERNAME, default=SMILE): vol.In(
9394
{SMILE: FLOW_SMILE, STRETCH: FLOW_STRETCH}
@@ -127,6 +128,32 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> Smile:
127128
return api
128129

129130

131+
async def verify_connection(
132+
hass: HomeAssistant, user_input: dict[str, Any]
133+
) -> tuple[Smile | None, dict[str, str]]:
134+
"""Verify and return the gateway connection or an error."""
135+
errors: dict[str, str] = {}
136+
137+
try:
138+
return (await validate_input(hass, user_input), errors)
139+
except ConnectionFailedError:
140+
errors[CONF_BASE] = "cannot_connect"
141+
except InvalidAuthentication:
142+
errors[CONF_BASE] = "invalid_auth"
143+
except InvalidSetupError:
144+
errors[CONF_BASE] = "invalid_setup"
145+
except (InvalidXMLError, ResponseError):
146+
errors[CONF_BASE] = "response_error"
147+
except UnsupportedDeviceError:
148+
errors[CONF_BASE] = "unsupported"
149+
except Exception: # noqa: BLE001
150+
_LOGGER.exception(
151+
"Unknown exception while verifying connection with your Plugwise Smile"
152+
)
153+
errors[CONF_BASE] = "unknown"
154+
return (None, errors)
155+
156+
130157
class PlugwiseConfigFlow(ConfigFlow, domain=DOMAIN):
131158
"""Handle a config flow for Plugwise Smile."""
132159

@@ -205,31 +232,6 @@ def is_matching(self, other_flow: Self) -> bool:
205232
return False
206233

207234

208-
async def _verify_connection(
209-
self, user_input: dict[str, Any]
210-
) -> tuple[Smile | None, dict[str, str]]:
211-
"""Verify and return the gateway connection using helper function."""
212-
errors: dict[str, str] = {}
213-
214-
try:
215-
api = await validate_input(self.hass, user_input)
216-
except ConnectionFailedError:
217-
errors[CONF_BASE] = "cannot_connect"
218-
except InvalidAuthentication:
219-
errors[CONF_BASE] = "invalid_auth"
220-
except InvalidSetupError:
221-
errors[CONF_BASE] = "invalid_setup"
222-
except (InvalidXMLError, ResponseError):
223-
errors[CONF_BASE] = "response_error"
224-
except UnsupportedDeviceError:
225-
errors[CONF_BASE] = "unsupported"
226-
except Exception: # noqa: BLE001
227-
errors[CONF_BASE] = "unknown"
228-
else:
229-
return (api, errors)
230-
return (None, errors)
231-
232-
233235
async def async_step_user(
234236
self, user_input: dict[str, Any] | None = None
235237
) -> ConfigFlowResult:
@@ -242,8 +244,8 @@ async def async_step_user(
242244
user_input[CONF_PORT] = self.discovery_info.port
243245
user_input[CONF_USERNAME] = self._username
244246

245-
api, errors = await self._verify_connection(user_input)
246-
if not errors and api:
247+
api, errors = await verify_connection(self.hass, user_input)
248+
if api:
247249
await self.async_set_unique_id(
248250
api.smile_hostname or api.gateway_id, raise_on_progress=False
249251
)
@@ -252,7 +254,7 @@ async def async_step_user(
252254

253255
return self.async_show_form(
254256
step_id=SOURCE_USER,
255-
data_schema=SMILE_USER_SCHEMA(self.discovery_info),
257+
data_schema=smile_user_schema(self.discovery_info),
256258
errors=errors,
257259
)
258260

@@ -268,19 +270,19 @@ async def async_step_reconfigure(
268270
# Redefine ingest existing username and password
269271
full_input = {
270272
CONF_HOST: user_input.get(CONF_HOST),
271-
CONF_PORT: user_input.get(CONF_PORT),
273+
CONF_PORT: reconfigure_entry.data.get(CONF_PORT),
272274
CONF_USERNAME: reconfigure_entry.data.get(CONF_USERNAME),
273275
CONF_PASSWORD: reconfigure_entry.data.get(CONF_PASSWORD),
274276
}
275277

276-
api, errors = await self._verify_connection(full_input)
277-
if not errors and api:
278+
api, errors = await verify_connection(self.hass, full_input)
279+
if api:
278280
await self.async_set_unique_id(
279281
api.smile_hostname or api.gateway_id, raise_on_progress=False
280282
)
281283
self._abort_if_unique_id_mismatch(reason="not_the_same_smile")
282284
return self.async_update_reload_and_abort(
283-
self._get_reconfigure_entry(),
285+
reconfigure_entry,
284286
data_updates=full_input,
285287
)
286288

0 commit comments

Comments
 (0)