41
41
42
42
async def async_setup (hass , config ):
43
43
"""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 ( )
50
50
51
51
path = hass .config .path (FOLDER )
52
52
@@ -60,14 +60,7 @@ def check_isdir(path):
60
60
hass .data .setdefault (DOMAIN , {})
61
61
hass .data [DOMAIN ]["allow_all_imports" ] = config [DOMAIN ].get (CONF_ALLOW_ALL_IMPORTS )
62
62
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 )
71
64
72
65
_LOGGER .debug ("adding reload handler" )
73
66
@@ -78,25 +71,18 @@ async def reload_scripts_handler(call):
78
71
)
79
72
80
73
ctx_delete = {}
81
- for global_ctx_name , global_ctx in global_ctx_mgr .items ():
74
+ for global_ctx_name , global_ctx in GlobalContextMgr .items ():
82
75
if not global_ctx_name .startswith ("file." ):
83
76
continue
84
77
await global_ctx .stop ()
85
78
global_ctx .set_auto_start (False )
86
79
ctx_delete [global_ctx_name ] = global_ctx
87
80
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 )
98
82
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 ():
100
86
if not global_ctx_name .startswith ("file." ):
101
87
continue
102
88
await global_ctx .start ()
@@ -107,29 +93,15 @@ async def jupyter_kernel_start(call):
107
93
"""Handle Jupyter kernel start call."""
108
94
_LOGGER .debug ("service call to jupyter_kernel_start: %s" , call .data )
109
95
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 = {})
120
98
global_ctx .set_auto_start (True )
121
99
122
- global_ctx_mgr .set (global_ctx_name , global_ctx )
100
+ GlobalContextMgr .set (global_ctx_name , global_ctx )
123
101
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 )
133
105
await kernel .session_start ()
134
106
hass .states .async_set (call .data ["state_var" ], json .dumps (kernel .get_ports ()))
135
107
@@ -158,20 +130,20 @@ async def state_changed(event):
158
130
"value" : new_val ,
159
131
"old_value" : old_val ,
160
132
}
161
- await state_func .update (new_vars , func_args )
133
+ await State .update (new_vars , func_args )
162
134
163
135
async def start_triggers (event ):
164
136
_LOGGER .debug ("adding state changed listener and starting triggers" )
165
137
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 ():
167
139
if not global_ctx_name .startswith ("file." ):
168
140
continue
169
141
await global_ctx .start ()
170
142
global_ctx .set_auto_start (True )
171
143
172
144
async def stop_triggers (event ):
173
145
_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 ():
175
147
if not global_ctx_name .startswith ("file." ):
176
148
continue
177
149
await global_ctx .stop ()
@@ -183,14 +155,7 @@ async def stop_triggers(event):
183
155
184
156
185
157
@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 ):
194
159
"""Compile all python scripts in FOLDER."""
195
160
196
161
path = hass .config .path (FOLDER )
@@ -213,25 +178,11 @@ def read_file(path):
213
178
source = await hass .async_add_executor_job (read_file , file )
214
179
215
180
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 = {})
225
182
global_ctx .set_auto_start (False )
226
183
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 )
235
186
236
187
if not ast_ctx .parse (source , filename = file ):
237
188
exc = ast_ctx .get_exception_long ()
@@ -242,4 +193,4 @@ def read_file(path):
242
193
if exc is not None :
243
194
ast_ctx .get_logger ().error (exc )
244
195
continue
245
- global_ctx_mgr .set (global_ctx_name , global_ctx )
196
+ GlobalContextMgr .set (global_ctx_name , global_ctx )
0 commit comments