Skip to content

Commit c637adb

Browse files
committed
task.executor raises an exception when called with a pyscript function.
1 parent a33862c commit c637adb

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

custom_components/pyscript/function.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Function call handling."""
22

33
import asyncio
4-
import functools
54
import logging
65
import traceback
76

@@ -84,7 +83,6 @@ def init(cls, hass):
8483
"event.fire": cls.event_fire,
8584
"service.call": cls.service_call,
8685
"service.has_service": cls.service_has_service,
87-
"task.executor": cls.task_executor,
8886
"task.cancel": cls.user_task_cancel,
8987
"task.current_task": cls.user_task_current_task,
9088
"task.remove_done_callback": cls.user_task_remove_done_callback,
@@ -253,13 +251,6 @@ async def task_unique(name, kill_me=False):
253251

254252
return task_unique
255253

256-
@classmethod
257-
async def task_executor(cls, func, *args, **kwargs):
258-
"""Implement task.executor()."""
259-
if asyncio.iscoroutinefunction(func) or not callable(func):
260-
raise TypeError("function is not callable by task.executor()")
261-
return await cls.hass.async_add_executor_job(functools.partial(func, **kwargs), *args)
262-
263254
@classmethod
264255
async def user_task_cancel(cls, task=None):
265256
"""Implement task.cancel()."""

custom_components/pyscript/trigger.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import datetime as dt
5+
import functools
56
import locale
67
import logging
78
import math
@@ -185,6 +186,7 @@ async def user_task_add_done_callback(task, callback, *args, **kwargs):
185186

186187
funcs = {
187188
"task.add_done_callback": user_task_add_done_callback,
189+
"task.executor": cls.user_task_executor,
188190
}
189191
Function.register(funcs)
190192

@@ -513,6 +515,17 @@ async def wait_until(
513515
raise exc
514516
return ret
515517

518+
@classmethod
519+
async def user_task_executor(cls, func, *args, **kwargs):
520+
"""Implement task.executor()."""
521+
if asyncio.iscoroutinefunction(func) or not callable(func):
522+
raise TypeError(f"function {func} is not callable by task.executor")
523+
if isinstance(func, EvalFuncVar):
524+
raise TypeError(
525+
"pyscript functions can't be called from task.executor - must be a regular python function"
526+
)
527+
return await cls.hass.async_add_executor_job(functools.partial(func, **kwargs), *args)
528+
516529
@classmethod
517530
def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
518531
"""Parse a date time string, returning datetime."""

tests/test_unit_eval.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,11 @@ async def test_eval(hass):
12291229
["continue", "Exception in test line 1 column 0: continue statement outside loop"],
12301230
["raise", "Exception in test line 1 column 0: No active exception to reraise"],
12311231
["yield", "Exception in test line 1 column 0: test: not implemented ast ast_yield"],
1232-
["task.executor(5)", "Exception in test line 1 column 14: function is not callable by task.executor()"],
1232+
["task.executor(5)", "Exception in test line 1 column 14: function 5 is not callable by task.executor"],
1233+
[
1234+
"task.executor(task.sleep)",
1235+
"Exception in test line 1 column 14: function <bound method Function.async_sleep of <class 'custom_components.pyscript.function.Function'>> is not callable by task.executor",
1236+
],
12331237
["task.name2id('notask')", "Exception in test line 1 column 13: task name 'notask' is unknown"],
12341238
[
12351239
"state.get('pyscript.xyz1.abc')",

0 commit comments

Comments
 (0)