4141
4242async def async_setup (hass , config ):
4343 """Initialize the pyscript component."""
44- handler_func = Handler (hass )
45- event_func = Event (hass )
46- trig_time_func = TrigTime (hass , handler_func )
47- state_func = State (hass , handler_func )
48- state_func .register_functions ()
49- global_ctx_mgr = GlobalContextMgr ( handler_func )
44+ Handler . init (hass )
45+ Event . init (hass )
46+ TrigTime . init (hass )
47+ State . init (hass )
48+ State .register_functions ()
49+ GlobalContextMgr . init ( )
5050
5151 path = hass .config .path (FOLDER )
5252
@@ -60,14 +60,7 @@ def check_isdir(path):
6060 hass .data .setdefault (DOMAIN , {})
6161 hass .data [DOMAIN ]["allow_all_imports" ] = config [DOMAIN ].get (CONF_ALLOW_ALL_IMPORTS )
6262
63- await compile_scripts ( # pylint: disable=unused-variable
64- hass ,
65- event_func = event_func ,
66- state_func = state_func ,
67- handler_func = handler_func ,
68- trig_time_func = trig_time_func ,
69- global_ctx_mgr = global_ctx_mgr ,
70- )
63+ await compile_scripts (hass )
7164
7265 _LOGGER .debug ("adding reload handler" )
7366
@@ -78,25 +71,18 @@ async def reload_scripts_handler(call):
7871 )
7972
8073 ctx_delete = {}
81- for global_ctx_name , global_ctx in global_ctx_mgr .items ():
74+ for global_ctx_name , global_ctx in GlobalContextMgr .items ():
8275 if not global_ctx_name .startswith ("file." ):
8376 continue
8477 await global_ctx .stop ()
8578 global_ctx .set_auto_start (False )
8679 ctx_delete [global_ctx_name ] = global_ctx
8780 for global_ctx_name , global_ctx in ctx_delete .items ():
88- await global_ctx_mgr .delete (global_ctx_name )
89-
90- await compile_scripts (
91- hass ,
92- event_func = event_func ,
93- state_func = state_func ,
94- handler_func = handler_func ,
95- trig_time_func = trig_time_func ,
96- global_ctx_mgr = global_ctx_mgr ,
97- )
81+ await GlobalContextMgr .delete (global_ctx_name )
9882
99- for global_ctx_name , global_ctx in global_ctx_mgr .items ():
83+ await compile_scripts (hass )
84+
85+ for global_ctx_name , global_ctx in GlobalContextMgr .items ():
10086 if not global_ctx_name .startswith ("file." ):
10187 continue
10288 await global_ctx .start ()
@@ -107,29 +93,15 @@ async def jupyter_kernel_start(call):
10793 """Handle Jupyter kernel start call."""
10894 _LOGGER .debug ("service call to jupyter_kernel_start: %s" , call .data )
10995
110- global_ctx_name = global_ctx_mgr .new_name ("jupyter_" )
111- global_ctx = GlobalContext (
112- global_ctx_name ,
113- hass ,
114- global_sym_table = {},
115- state_func = state_func ,
116- event_func = event_func ,
117- handler_func = handler_func ,
118- trig_time_func = trig_time_func ,
119- )
96+ global_ctx_name = GlobalContextMgr .new_name ("jupyter_" )
97+ global_ctx = GlobalContext (global_ctx_name , hass , global_sym_table = {})
12098 global_ctx .set_auto_start (True )
12199
122- global_ctx_mgr .set (global_ctx_name , global_ctx )
100+ GlobalContextMgr .set (global_ctx_name , global_ctx )
123101
124- ast_ctx = AstEval (
125- global_ctx_name ,
126- global_ctx = global_ctx ,
127- state_func = state_func ,
128- event_func = event_func ,
129- handler_func = handler_func ,
130- )
131- handler_func .install_ast_funcs (ast_ctx )
132- kernel = Kernel (call .data , ast_ctx , global_ctx_name , global_ctx_mgr )
102+ ast_ctx = AstEval (global_ctx_name , global_ctx )
103+ Handler .install_ast_funcs (ast_ctx )
104+ kernel = Kernel (call .data , ast_ctx , global_ctx_name )
133105 await kernel .session_start ()
134106 hass .states .async_set (call .data ["state_var" ], json .dumps (kernel .get_ports ()))
135107
@@ -158,20 +130,20 @@ async def state_changed(event):
158130 "value" : new_val ,
159131 "old_value" : old_val ,
160132 }
161- await state_func .update (new_vars , func_args )
133+ await State .update (new_vars , func_args )
162134
163135 async def start_triggers (event ):
164136 _LOGGER .debug ("adding state changed listener and starting triggers" )
165137 hass .bus .async_listen (EVENT_STATE_CHANGED , state_changed )
166- for global_ctx_name , global_ctx in global_ctx_mgr .items ():
138+ for global_ctx_name , global_ctx in GlobalContextMgr .items ():
167139 if not global_ctx_name .startswith ("file." ):
168140 continue
169141 await global_ctx .start ()
170142 global_ctx .set_auto_start (True )
171143
172144 async def stop_triggers (event ):
173145 _LOGGER .debug ("stopping triggers" )
174- for global_ctx_name , global_ctx in global_ctx_mgr .items ():
146+ for global_ctx_name , global_ctx in GlobalContextMgr .items ():
175147 if not global_ctx_name .startswith ("file." ):
176148 continue
177149 await global_ctx .stop ()
@@ -183,14 +155,7 @@ async def stop_triggers(event):
183155
184156
185157@bind_hass
186- async def compile_scripts (
187- hass ,
188- event_func = None ,
189- state_func = None ,
190- handler_func = None ,
191- trig_time_func = None ,
192- global_ctx_mgr = None ,
193- ):
158+ async def compile_scripts (hass ):
194159 """Compile all python scripts in FOLDER."""
195160
196161 path = hass .config .path (FOLDER )
@@ -213,25 +178,11 @@ def read_file(path):
213178 source = await hass .async_add_executor_job (read_file , file )
214179
215180 global_ctx_name = f"file.{ name } "
216- global_ctx = GlobalContext (
217- global_ctx_name ,
218- hass ,
219- global_sym_table = {},
220- state_func = state_func ,
221- event_func = event_func ,
222- handler_func = handler_func ,
223- trig_time_func = trig_time_func ,
224- )
181+ global_ctx = GlobalContext (global_ctx_name , hass , global_sym_table = {})
225182 global_ctx .set_auto_start (False )
226183
227- ast_ctx = AstEval (
228- global_ctx_name ,
229- global_ctx = global_ctx ,
230- state_func = state_func ,
231- event_func = event_func ,
232- handler_func = handler_func ,
233- )
234- handler_func .install_ast_funcs (ast_ctx )
184+ ast_ctx = AstEval (global_ctx_name , global_ctx )
185+ Handler .install_ast_funcs (ast_ctx )
235186
236187 if not ast_ctx .parse (source , filename = file ):
237188 exc = ast_ctx .get_exception_long ()
@@ -242,4 +193,4 @@ def read_file(path):
242193 if exc is not None :
243194 ast_ctx .get_logger ().error (exc )
244195 continue
245- global_ctx_mgr .set (global_ctx_name , global_ctx )
196+ GlobalContextMgr .set (global_ctx_name , global_ctx )
0 commit comments