99import voluptuous as vol
1010
1111from homeassistant .components import usb
12- from homeassistant .config_entries import SOURCE_USER , ConfigFlow
12+ from homeassistant .config_entries import SOURCE_USER , ConfigFlow , ConfigFlowResult
1313from homeassistant .const import CONF_BASE
1414from homeassistant .core import callback
1515from homeassistant .data_entry_flow import FlowResult
1616import serial .tools .list_ports
1717
1818from .const import CONF_MANUAL_PATH , CONF_USB_PATH , DOMAIN , MANUAL_PATH
1919
20+ STICK_RECONF_SCHEMA = vol .Schema (
21+ {
22+ vol .Required (CONF_USB_PATH ): str ,
23+ }
24+ )
25+
2026
2127@callback
2228def plugwise_stick_entries (hass ):
@@ -28,7 +34,7 @@ def plugwise_stick_entries(hass):
2834 ]
2935
3036
31- async def validate_usb_connection (self , device_path = None ) -> tuple [dict [str , str ], str ]:
37+ async def validate_usb_connection (self , device_path = None ) -> tuple [dict [str , str ], str | None ]:
3238 """Test if device_path is a real Plugwise USB-Stick."""
3339 errors = {}
3440
@@ -93,6 +99,7 @@ async def async_step_user(
9399 return self .async_create_entry (
94100 title = "Stick" , data = {CONF_USB_PATH : device_path }
95101 )
102+
96103 return self .async_show_form (
97104 step_id = SOURCE_USER ,
98105 data_schema = vol .Schema (
@@ -112,7 +119,10 @@ async def async_step_manual_path(
112119 )
113120 errors , mac_stick = await validate_usb_connection (self .hass , device_path )
114121 if not errors :
115- await self .async_set_unique_id (mac_stick )
122+ await self .async_set_unique_id (
123+ unique_id = mac_stick , raise_on_progress = False
124+ )
125+ self ._abort_if_unique_id_configured ()
116126 return self .async_create_entry (
117127 title = "Stick" , data = {CONF_USB_PATH : device_path }
118128 )
@@ -123,3 +133,35 @@ async def async_step_manual_path(
123133 ),
124134 errors = errors ,
125135 )
136+
137+ async def async_step_reconfigure (
138+ self , user_input : dict [str , Any ] | None = None
139+ ) -> ConfigFlowResult :
140+ """Handle reconfiguration of the integration."""
141+ errors : dict [str , str ] = {}
142+ reconfigure_entry = self ._get_reconfigure_entry ()
143+
144+ if user_input is not None :
145+ device_path = await self .hass .async_add_executor_job (
146+ usb .get_serial_by_id , user_input .get (CONF_USB_PATH )
147+ )
148+ errors , mac_stick = await validate_usb_connection (self .hass , device_path )
149+ if not errors :
150+ await self .async_set_unique_id (
151+ unique_id = mac_stick , raise_on_progress = False
152+ )
153+ self ._abort_if_unique_id_mismatch (reason = "not_the_same_stick" )
154+ return self .async_update_reload_and_abort (
155+ reconfigure_entry ,
156+ data_updates = {CONF_USB_PATH : device_path }
157+ )
158+
159+ return self .async_show_form (
160+ step_id = "reconfigure" ,
161+ data_schema = self .add_suggested_values_to_schema (
162+ data_schema = STICK_RECONF_SCHEMA ,
163+ suggested_values = reconfigure_entry .data ,
164+ ),
165+ description_placeholders = {"title" : reconfigure_entry .title },
166+ errors = errors ,
167+ )
0 commit comments