33from __future__ import annotations
44
55from copy import deepcopy
6- from types import MappingProxyType
76from typing import Any , Self
87
98from plugwise import Smile
4241from homeassistant .core import HomeAssistant , callback
4342from homeassistant .helpers import config_validation as cv
4443from homeassistant .helpers .aiohttp_client import async_get_clientsession
45- from homeassistant .helpers .selector import TextSelector
4644
4745from .const import (
4846 ANNA_WITH_ADAM ,
7371
7472# Upstream basically the whole file (excluding the pw-beta options)
7573
74+ SMILE_RECONF_SCHEMA = vol .Schema (
75+ {
76+ vol .Required (CONF_HOST ): str ,
77+ vol .Optional (CONF_PORT , default = DEFAULT_PORT ): int ,
78+ }
79+ )
80+
7681
77- def base_schema (
82+ def SMILE_USER_SCHEMA (
7883 cf_input : ZeroconfServiceInfo | dict [str , Any ] | None ,
7984) -> vol .Schema :
8085 """Generate base schema for gateways."""
@@ -105,23 +110,10 @@ def base_schema(
105110 )
106111
107112
108- def reconfigure_schema (reconfigure_data : MappingProxyType [str , Any ]) -> vol .Schema :
109- """Generate reconfigure schema for gateways."""
110- return vol .Schema (
111- {
112- vol .Required (
113- CONF_HOST , default = reconfigure_data .get (CONF_HOST )
114- ): TextSelector (),
115- vol .Required (
116- CONF_PORT , default = reconfigure_data .get (CONF_PORT )
117- ): vol .Coerce (int ),
118- }
119- )
120-
121113async def validate_input (hass : HomeAssistant , data : dict [str , Any ]) -> Smile :
122114 """Validate whether the user input allows us to connect to the gateway.
123115
124- Data has the keys from base_schema() with values provided by the user.
116+ Data has the keys from the schema with values provided by the user.
125117 """
126118 websession = async_get_clientsession (hass , verify_ssl = False )
127119 api = Smile (
@@ -216,7 +208,7 @@ def is_matching(self, other_flow: Self) -> bool:
216208 async def _verify_connection (
217209 self , user_input : dict [str , Any ]
218210 ) -> tuple [Smile | None , dict [str , str ]]:
219- """Verify gateway connection helper function user and reconfiguration steps ."""
211+ """Verify and return the gateway connection using helper function ."""
220212 errors : dict [str , str ] = {}
221213
222214 try :
@@ -234,9 +226,6 @@ async def _verify_connection(
234226 except Exception : # noqa: BLE001
235227 errors [CONF_BASE ] = "unknown"
236228 else :
237- await self .async_set_unique_id (
238- api .smile_hostname or api .gateway_id , raise_on_progress = False
239- )
240229 return (api , errors )
241230 return (None , errors )
242231
@@ -255,12 +244,15 @@ async def async_step_user(
255244
256245 api , errors = await self ._verify_connection (user_input )
257246 if not errors and api :
247+ await self .async_set_unique_id (
248+ api .smile_hostname or api .gateway_id , raise_on_progress = False
249+ )
258250 self ._abort_if_unique_id_configured ()
259251 return self .async_create_entry (title = api .smile_name , data = user_input )
260252
261253 return self .async_show_form (
262254 step_id = SOURCE_USER ,
263- data_schema = base_schema (self .discovery_info ),
255+ data_schema = SMILE_USER_SCHEMA (self .discovery_info ),
264256 errors = errors ,
265257 )
266258
@@ -274,27 +266,31 @@ async def async_step_reconfigure(
274266
275267 if user_input :
276268 # Redefine ingest existing username and password
277- user_input = {
269+ full_input = {
278270 CONF_HOST : user_input .get (CONF_HOST ),
279271 CONF_PORT : user_input .get (CONF_PORT ),
280272 CONF_USERNAME : reconfigure_entry .data .get (CONF_USERNAME ),
281273 CONF_PASSWORD : reconfigure_entry .data .get (CONF_PASSWORD ),
282274 }
283275
284- _ , errors = await self ._verify_connection (user_input )
285- if not errors :
276+ api , errors = await self ._verify_connection (full_input )
277+ if not errors and api :
278+ await self .async_set_unique_id (
279+ api .smile_hostname or api .gateway_id , raise_on_progress = False
280+ )
286281 self ._abort_if_unique_id_mismatch (reason = "not_the_same_smile" )
287282 return self .async_update_reload_and_abort (
288283 self ._get_reconfigure_entry (),
289- data_updates = user_input ,
284+ data_updates = full_input ,
290285 )
291286
292287 return self .async_show_form (
293288 step_id = "reconfigure" ,
294- data_schema = reconfigure_schema (reconfigure_entry .data ),
295- description_placeholders = {
296- "title" : reconfigure_entry .title ,
297- },
289+ data_schema = self .add_suggested_values_to_schema (
290+ data_schema = SMILE_RECONF_SCHEMA ,
291+ suggested_values = reconfigure_entry .data ,
292+ ),
293+ description_placeholders = {"title" : reconfigure_entry .title },
298294 errors = errors ,
299295 )
300296
0 commit comments