From 1c0a5973ed82f6b5d1cc14bba2d5e60edfd8c113 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Thu, 23 Nov 2023 08:07:46 +0100 Subject: [PATCH] 23.11.0 --- src/HABApp/core/asyncio.py | 38 ++++++++++++++++-------- src/HABApp/openhab/process_events.py | 4 +-- src/HABApp/parameters/parameter_files.py | 4 ++- tox.ini | 2 +- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/HABApp/core/asyncio.py b/src/HABApp/core/asyncio.py index a3ed5442..a3f71eda 100644 --- a/src/HABApp/core/asyncio.py +++ b/src/HABApp/core/asyncio.py @@ -1,7 +1,10 @@ from asyncio import Future as _Future +from asyncio import Task as _Task from asyncio import run_coroutine_threadsafe as _run_coroutine_threadsafe -from contextvars import ContextVar as _ContextVar, Token -from typing import Any as _Any, Callable, Final, Optional +from contextvars import ContextVar as _ContextVar +from contextvars import Token +from typing import Any as _Any +from typing import Callable, Final, Optional from typing import Callable as _Callable from typing import Coroutine as _Coroutine from typing import Optional as _Optional @@ -10,6 +13,7 @@ from HABApp.core.const import loop from HABApp.core.const.const import PYTHON_310 + if PYTHON_310: from typing import ParamSpec as _ParamSpec else: @@ -52,22 +56,31 @@ def __str__(self): _tasks = set() -def create_task(coro: _Coroutine, name: _Optional[str] = None) -> _Future: +_T = _TypeVar('_T') + + +def create_task(coro: _Coroutine[_Any, _Any, _T], name: _Optional[str] = None) -> _Future[_T]: # https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task if async_context.get(None) is None: f = _run_coroutine_threadsafe(coro, loop) + _tasks.add(f) f.add_done_callback(_tasks.discard) return f - else: - t = loop.create_task(coro, name=name) - t.add_done_callback(_tasks.discard) - return t + + t = loop.create_task(coro, name=name) + _tasks.add(t) + t.add_done_callback(_tasks.discard) + return t -_CORO_RET = _TypeVar('_CORO_RET') +def create_task_from_async(coro: _Coroutine[_Any, _Any, _T], name: _Optional[str] = None) -> _Task[_T]: + t = loop.create_task(coro, name=name) + _tasks.add(t) + t.add_done_callback(_tasks.discard) + return t -def run_coro_from_thread(coro: _Coroutine[_Any, _Any, _CORO_RET], calling: _Callable) -> _CORO_RET: +def run_coro_from_thread(coro: _Coroutine[_Any, _Any, _T], calling: _Callable) -> _T: # This function call is blocking, so it can't be called in the async context if async_context.get(None) is not None: raise AsyncContextError(calling) @@ -76,11 +89,10 @@ def run_coro_from_thread(coro: _Coroutine[_Any, _Any, _CORO_RET], calling: _Call return fut.result() -P = _ParamSpec('P') -T = _TypeVar('T') +_P = _ParamSpec('_P') -def run_func_from_async(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: +def run_func_from_async(func: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T: # we already have an async context if async_context.get(None) is not None: return func(*args, **kwargs) @@ -92,5 +104,5 @@ def run_func_from_async(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) # return future.result() -async def _run_func_from_async_helper(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: +async def _run_func_from_async_helper(func: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T: return func(*args, **kwargs) diff --git a/src/HABApp/openhab/process_events.py b/src/HABApp/openhab/process_events.py index 415d7179..dbc4532c 100644 --- a/src/HABApp/openhab/process_events.py +++ b/src/HABApp/openhab/process_events.py @@ -4,7 +4,7 @@ import HABApp import HABApp.core import HABApp.openhab.events -from HABApp.core.asyncio import create_task +from HABApp.core.asyncio import create_task_from_async from HABApp.core.errors import ItemNotFoundException from HABApp.core.events import ValueChangeEvent, ValueUpdateEvent from HABApp.core.internals import uses_get_item, uses_post_event @@ -78,7 +78,7 @@ def on_sse_event(event_dict: dict, oh_3: bool): # Events that add items to the item registry # These events require that we query openHAB because of the metadata, so we have to do it in a task if isinstance(event, (ItemAddedEvent, ItemUpdatedEvent)): - create_task(item_event(event)) + create_task_from_async(item_event(event)) return None # Events that remove items from the item registry diff --git a/src/HABApp/parameters/parameter_files.py b/src/HABApp/parameters/parameter_files.py index e4f38dba..aad6688e 100644 --- a/src/HABApp/parameters/parameter_files.py +++ b/src/HABApp/parameters/parameter_files.py @@ -35,7 +35,8 @@ def save_file(file: str): assert isinstance(file, str), type(file) path = HABApp.CONFIG.directories.param if path is None: - raise ValueError('Parameter files are disabled! Configure a folder to use them!') + msg = 'Parameter files are disabled! Configure a folder to use them!' + raise ValueError(msg) filename = path / (file + '.yml') @@ -60,6 +61,7 @@ async def setup_param_files() -> bool: folder.add_file_type(HABAppParameterFile) watcher = folder.add_watch('.yml') await watcher.trigger_all() + return True def reload_param_file(name: str): diff --git a/tox.ini b/tox.ini index 05482857..4f8e4380 100644 --- a/tox.ini +++ b/tox.ini @@ -15,7 +15,7 @@ python = 3.9: py39 3.10: py310, flake, docs 3.11: py311 - 3.11: py312 + 3.12: py312 [testenv]