Skip to content

Commit

Permalink
1.1.1 (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 authored Jun 16, 2023
1 parent 77a72e8 commit 311ed71
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ MyOpenhabRule()
```

# Changelog
#### 1.1.1 (2023-06-16)
- Fixed a bug where the rule context was not found

#### 1.1.0 (2023-06-15)
- This is a breaking change!
This is a breaking change!
- Renamed `GroupItemStateChangedEvent` to `GroupStateChangedEvent`
- Groups issue a `GroupStateUpdateEvent` when the state updates on OH3 (consistent with OH4 behavior)
- Groups work now with `ValueUpdateEvent` and `ValueChangedEvent` as expected
Expand Down
6 changes: 5 additions & 1 deletion run/conf_testing/lib/HABAppTests/test_rule/test_rule.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from pathlib import Path
from typing import Dict, Callable
from typing import List

Expand Down Expand Up @@ -179,8 +180,11 @@ def __run_tests(self) -> List[TestResult]:
TestResult(c_name, tc.name, f'{i + 1:{width}d}/{count}') for i, tc in enumerate(self._tests.values())
]

module_of_class = Path(self.__class__.__module__)
relative_path = module_of_class.relative_to(HABApp.CONFIG.directories.rules)

log.info('')
log.info(f'Running {count} tests for {c_name}')
log.info(f'Running {count} tests for {c_name} (from "{relative_path}")')

for res, tc in zip(results, self._tests.values()):
if self.config.skip_on_failure and self.__worst_result >= TestResultStatus.FAILED:
Expand Down
38 changes: 38 additions & 0 deletions run/conf_testing/rules/habapp/test_rule_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from time import sleep

from HABApp import Rule
from HABApp.core.internals import get_current_context
from HABAppTests import TestBaseRule


class OtherRule(Rule):

def check_context(self):
assert get_current_context() is self._habapp_ctx


other_rule = OtherRule()


def func_no_context(target_context):
assert get_current_context() is target_context


class TestContext(TestBaseRule):

def __init__(self):
super().__init__()
self.add_test('TestGetContext', self.test_get_context)
self.add_test('TestSchedulerContext', self.test_scheduler_context)

def test_get_context(self):
assert get_current_context() is self._habapp_ctx
other_rule.check_context()

def test_scheduler_context(self):
self.run.soon(func_no_context, self._habapp_ctx)
self.run.soon(other_rule.check_context)
sleep(0.1)


TestContext()
2 changes: 1 addition & 1 deletion src/HABApp/__version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version scheme:
# X.X[.X] or X.X[.X].DEV-X

__version__ = '1.1.0'
__version__ = '1.1.1'
13 changes: 8 additions & 5 deletions src/HABApp/core/internals/wrapped_function/wrapped_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Optional

from HABApp.core.const import loop
from HABApp.core.internals import HINT_CONTEXT_OBJ
from HABApp.core.internals import HINT_CONTEXT_OBJ, ContextProvidingObj
from .base import WrappedFunctionBase, default_logger

POOL: Optional[ThreadPoolExecutor] = None
Expand Down Expand Up @@ -48,9 +48,10 @@ async def run_in_thread_pool(func: Callable):
HINT_FUNC_SYNC = Callable[..., Any]


class PoolFunc:
def __init__(self, parent: 'WrappedThreadFunction',
func_obj: HINT_FUNC_SYNC, func_args: Tuple[Any, ...], func_kwargs: Dict[str, Any]):
class PoolFunc(ContextProvidingObj):
def __init__(self, parent: 'WrappedThreadFunction', func_obj: HINT_FUNC_SYNC, func_args: Tuple[Any, ...],
func_kwargs: Dict[str, Any], context: Optional[HINT_CONTEXT_OBJ] = None, **kwargs):
super().__init__(context=context, **kwargs)
self.parent: Final = parent
self.func_obj: Final = func_obj
self.func_args: Final = func_args
Expand Down Expand Up @@ -133,4 +134,6 @@ def __init__(self, func: HINT_FUNC_SYNC,
self.warn_too_long: bool = warn_too_long

def run(self, *args, **kwargs):
POOL.submit(PoolFunc(self, self.func, args, kwargs).run)
# we need to copy the context, so it's available when the function is run
pool_func = PoolFunc(self, self.func, args, kwargs, context=self._habapp_ctx)
POOL.submit(pool_func.run)
1 change: 1 addition & 0 deletions src/HABApp/rule/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def send_warnings_to_log(message, category, filename, lineno, file=None, line=No


class Rule(ContextProvidingObj):

def __init__(self):
super().__init__(context=HABApp.rule_ctx.HABAppRuleContext(self))

Expand Down

0 comments on commit 311ed71

Please sign in to comment.