99from homeassistant import config_entries
1010from homeassistant .const import CONF_CODE , CONF_HOST
1111from homeassistant .core import HomeAssistant
12+ from homeassistant .config_entries import ConfigFlowResult
1213from homeassistant .data_entry_flow import FlowResult
1314
1415from .const import DOMAIN
1516
1617_LOGGER = logging .getLogger (__name__ )
1718
18- STEP_USER_DATA_SCHEMA = vol .Schema (
19+ def _bwt_schema (
20+ host : str | None = None ,
21+ code : str | None = None ,
22+ ): return vol .Schema (
1923 {
20- vol .Required (CONF_HOST ): str ,
21- vol .Required (CONF_CODE ) : str ,
24+ vol .Required (CONF_HOST , default = host ): str ,
25+ vol .Required (CONF_CODE , default = code ) : str ,
2226 }
2327)
2428
2529
2630async def validate_input (hass : HomeAssistant , data : dict [str , Any ]) -> dict [str , Any ]:
2731 """Validate the user input allows us to connect.
2832
29- Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
33+ Data has the keys from _bwt_schema with values provided by the user.
3034 """
3135 async with BwtApi (data [CONF_HOST ], data [CONF_CODE ]) as api :
3236 await api .get_current_data ()
@@ -40,6 +44,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
4044
4145 VERSION = 2
4246
47+
4348 async def async_step_user (
4449 self , user_input : dict [str , Any ] | None = None
4550 ) -> FlowResult :
@@ -60,5 +65,33 @@ async def async_step_user(
6065 errors ["base" ] = "unknown"
6166
6267 return self .async_show_form (
63- step_id = "user" , data_schema = STEP_USER_DATA_SCHEMA , errors = errors
68+ step_id = "user" , data_schema = _bwt_schema (), errors = errors
69+ )
70+
71+
72+ async def async_step_reconfigure (self , user_input : dict [str , Any ] | None = None ):
73+ """Manual reconfiguration to change a setting."""
74+ current = self ._get_reconfigure_entry ()
75+ errors : dict [str , str ] = {}
76+ if user_input is not None :
77+ try :
78+ await validate_input (self .hass , user_input )
79+ self .hass .config_entries .async_update_entry (current , data = user_input )
80+ await self .hass .config_entries .async_reload (current .entry_id )
81+ return self .async_abort (reason = "reconfiguration_successful" )
82+ except ConnectException :
83+ _LOGGER .exception ("Connection error setting up the Bwt Api" )
84+ errors ["base" ] = "cannot_connect"
85+ except WrongCodeException :
86+ _LOGGER .exception ("Wrong user code passed to bwt api" )
87+ errors ["base" ] = "invalid_auth"
88+ except Exception : # pylint: disable=broad-except
89+ _LOGGER .exception ("Unexpected exception" )
90+ errors ["base" ] = "unknown"
91+
92+ return self .async_show_form (
93+ step_id = "reconfigure" , data_schema = _bwt_schema (
94+ host = current .data [CONF_HOST ],
95+ code = current .data [CONF_CODE ],
96+ ), errors = errors
6497 )
0 commit comments