@@ -91,13 +91,16 @@ async def update_yaml_config(hass, config_entry):
91
91
await hass .config_entries .flow .async_init (DOMAIN , context = {"source" : SOURCE_IMPORT }, data = config )
92
92
93
93
94
- def start_global_contexts ():
94
+ def start_global_contexts (global_ctx_only = None ):
95
95
"""Start all the file and apps global contexts."""
96
96
start_list = []
97
97
for global_ctx_name , global_ctx in GlobalContextMgr .items ():
98
98
idx = global_ctx_name .find ("." )
99
99
if idx < 0 or global_ctx_name [0 :idx ] not in {"file" , "apps" }:
100
100
continue
101
+ if global_ctx_only is not None :
102
+ if global_ctx_name != global_ctx_only and not global_ctx_name .startswith (global_ctx_only + "." ):
103
+ continue
101
104
global_ctx .set_auto_start (True )
102
105
start_list .append (global_ctx )
103
106
for global_ctx in start_list :
@@ -139,11 +142,17 @@ async def reload_scripts_handler(call):
139
142
await update_yaml_config (hass , config_entry )
140
143
State .set_pyscript_config (config_entry .data )
141
144
142
- await unload_scripts ( )
145
+ global_ctx_only = call . data . get ( "global_ctx" , None )
143
146
144
- await load_scripts (hass , config_entry .data )
147
+ if global_ctx_only is not None and not GlobalContextMgr .get (global_ctx_only ):
148
+ _LOGGER .error ("pyscript.reload: no global context '%s' to reload" , global_ctx_only )
149
+ return
145
150
146
- start_global_contexts ()
151
+ await unload_scripts (global_ctx_only = global_ctx_only )
152
+
153
+ await load_scripts (hass , config_entry .data , global_ctx_only = global_ctx_only )
154
+
155
+ start_global_contexts (global_ctx_only = global_ctx_only )
147
156
148
157
hass .services .async_register (DOMAIN , SERVICE_RELOAD , reload_scripts_handler )
149
158
@@ -224,22 +233,25 @@ async def async_unload_entry(hass, config_entry):
224
233
return True
225
234
226
235
227
- async def unload_scripts (unload_all = False ):
236
+ async def unload_scripts (global_ctx_only = None , unload_all = False ):
228
237
"""Unload all scripts from GlobalContextMgr with given name prefixes."""
229
238
ctx_delete = {}
230
239
for global_ctx_name , global_ctx in GlobalContextMgr .items ():
231
240
if not unload_all :
232
241
idx = global_ctx_name .find ("." )
233
242
if idx < 0 or global_ctx_name [0 :idx ] not in {"file" , "apps" , "modules" }:
234
243
continue
244
+ if global_ctx_only is not None :
245
+ if global_ctx_name != global_ctx_only and not global_ctx_name .startswith (global_ctx_only + "." ):
246
+ continue
235
247
global_ctx .stop ()
236
248
ctx_delete [global_ctx_name ] = global_ctx
237
249
for global_ctx_name , global_ctx in ctx_delete .items ():
238
250
await GlobalContextMgr .delete (global_ctx_name )
239
251
240
252
241
253
@bind_hass
242
- async def load_scripts (hass , data ):
254
+ async def load_scripts (hass , data , global_ctx_only = None ):
243
255
"""Load all python scripts in FOLDER."""
244
256
245
257
pyscript_dir = hass .config .path (FOLDER )
@@ -279,6 +291,9 @@ def glob_files(load_paths, data):
279
291
280
292
source_files = await hass .async_add_executor_job (glob_files , load_paths , data )
281
293
for global_ctx_name , source_file , rel_import_path , fq_mod_name in source_files :
294
+ if global_ctx_only is not None :
295
+ if global_ctx_name != global_ctx_only and not global_ctx_name .startswith (global_ctx_only + "." ):
296
+ continue
282
297
global_ctx = GlobalContext (
283
298
global_ctx_name ,
284
299
global_sym_table = {"__name__" : fq_mod_name },
0 commit comments