44
55import asyncio
66import logging
7+ from types import MappingProxyType
78
89import httpx
910import ollama
@@ -92,8 +93,12 @@ async def async_migrate_integration(hass: HomeAssistant) -> None:
9293
9394 for entry in entries :
9495 use_existing = False
96+ # Create subentry with model from entry.data and options from entry.options
97+ subentry_data = entry .options .copy ()
98+ subentry_data [CONF_MODEL ] = entry .data [CONF_MODEL ]
99+
95100 subentry = ConfigSubentry (
96- data = entry . options ,
101+ data = MappingProxyType ( subentry_data ) ,
97102 subentry_type = "conversation" ,
98103 title = entry .title ,
99104 unique_id = None ,
@@ -146,17 +151,19 @@ async def async_migrate_integration(hass: HomeAssistant) -> None:
146151 hass .config_entries .async_update_entry (
147152 entry ,
148153 title = DEFAULT_NAME ,
154+ # Update parent entry to only keep URL, remove model
155+ data = {CONF_URL : entry .data [CONF_URL ]},
149156 options = {},
150- version = 2 ,
151- minor_version = 2 ,
157+ version = 3 ,
158+ minor_version = 1 ,
152159 )
153160
154161
155162async def async_migrate_entry (hass : HomeAssistant , entry : OllamaConfigEntry ) -> bool :
156163 """Migrate entry."""
157164 _LOGGER .debug ("Migrating from version %s:%s" , entry .version , entry .minor_version )
158165
159- if entry .version > 2 :
166+ if entry .version > 3 :
160167 # This means the user has downgraded from a future version
161168 return False
162169
@@ -174,6 +181,25 @@ async def async_migrate_entry(hass: HomeAssistant, entry: OllamaConfigEntry) ->
174181
175182 hass .config_entries .async_update_entry (entry , minor_version = 2 )
176183
184+ if entry .version == 2 and entry .minor_version == 2 :
185+ # Update subentries to include the model
186+ for subentry in entry .subentries .values ():
187+ if subentry .subentry_type == "conversation" :
188+ updated_data = dict (subentry .data )
189+ updated_data [CONF_MODEL ] = entry .data [CONF_MODEL ]
190+
191+ hass .config_entries .async_update_subentry (
192+ entry , subentry , data = MappingProxyType (updated_data )
193+ )
194+
195+ # Update main entry to remove model and bump version
196+ hass .config_entries .async_update_entry (
197+ entry ,
198+ data = {CONF_URL : entry .data [CONF_URL ]},
199+ version = 3 ,
200+ minor_version = 1 ,
201+ )
202+
177203 _LOGGER .debug (
178204 "Migration to version %s:%s successful" , entry .version , entry .minor_version
179205 )
0 commit comments