Skip to content

Commit

Permalink
23.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Nov 23, 2023
1 parent 61d2032 commit 1c0a597
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
38 changes: 25 additions & 13 deletions src/HABApp/core/asyncio.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
4 changes: 2 additions & 2 deletions src/HABApp/openhab/process_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/HABApp/parameters/parameter_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ python =
3.9: py39
3.10: py310, flake, docs
3.11: py311
3.11: py312
3.12: py312


[testenv]
Expand Down

0 comments on commit 1c0a597

Please sign in to comment.