@@ -46,13 +46,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4646 # Ensure migration from v3 single bike
4747 if "bike_id" not in entry .data :
4848 bikedata = await stromer .stromer_detect ()
49- new_data = {** entry .data , "bike_id" : bikedata [0 ]["bikeid" ]}
49+ new_data = {
50+ ** entry .data ,
51+ "bike_id" : bikedata [0 ]["bikeid" ],
52+ "nickname" : bikedata [0 ]["nickname" ],
53+ "model" : bikedata [0 ]["biketype" ]
54+ }
5055 hass .config_entries .async_update_entry (entry , data = new_data )
51- new_data = {** entry .data , "nickname" : bikedata [0 ]["nickname" ]}
52- hass .config_entries .async_update_entry (entry , data = new_data )
53- new_data = {** entry .data , "model" : bikedata [0 ]["biketype" ]}
54- hass .config_entries .async_update_entry (entry , data = new_data )
55- await hass .config_entries .async_reload (entry .entry_id )
5656
5757 # Set specific bike (instead of all bikes) introduced with morebikes PR
5858 stromer .bike_id = entry .data ["bike_id" ]
@@ -72,25 +72,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
7272
7373 # Add bike to the HA device registry
7474 device_registry = dr .async_get (hass )
75- device_registry .async_get_or_create (
75+ device = device_registry .async_get_or_create (
7676 config_entry_id = entry .entry_id ,
7777 identifiers = {(DOMAIN , str (stromer .bike_id ))},
7878 manufacturer = "Stromer" ,
7979 name = f"{ stromer .bike_name } " ,
8080 model = f"{ stromer .bike_model } " ,
8181 )
8282
83- # Remove stale via_device
84- device = device_registry .async_get_device (
85- identifiers = {(DOMAIN , entry .entry_id )}
83+ # Remove non-existing via device
84+ device_registry .async_update_device (
85+ device .id ,
86+ via_device_id = None
8687 )
8788
88- if device and "via_device" in device .via_device_id :
89- device_registry .async_update_device (
90- device .id ,
91- via_device_id = None
92- )
93-
9489 # Set up platforms (i.e. sensors, binary_sensors)
9590 await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
9691
0 commit comments