Skip to content

Commit

Permalink
Move context var to own module (#1748)
Browse files Browse the repository at this point in the history
* Fix rx_mode lazy_config

* Move context var to own module
  • Loading branch information
SukramJ authored Oct 6, 2024
1 parent 54c73bb commit 7440d74
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
8 changes: 7 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Version 2024.10.1 (2024-10-04)

- Fix rx_mode lazy_config
- Make DEFAULT optional due to homegear support
- Move context var to own module

# Version 2024.10.0 (2024-10-03)

- Add config option for max read workers
- Disable collector for stop events
- Improve logging when raising exception
- Log exception at the most outer service
- Make UPDATEABLE and DEFAULT optional due to homegear support
- Make UPDATEABLE optional due to homegear support
- Raise exception on set_value, put_paramset
- Remove command queue

Expand Down
4 changes: 0 additions & 4 deletions hahomematic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from __future__ import annotations

import asyncio
from contextvars import ContextVar
import logging
import signal
import sys
Expand All @@ -17,9 +16,6 @@

from hahomematic import central as hmcu

# context var for storing if call is running within a service
IN_SERVICE_VAR: ContextVar[bool] = ContextVar("in_service_var", default=False)

if sys.stdout.isatty():
logging.basicConfig(level=logging.INFO)

Expand Down
2 changes: 1 addition & 1 deletion hahomematic/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class RxMode(IntEnum):
BURST = 2
CONFIG = 4
WAKEUP = 8
LAZY_CONFIG = 10
LAZY_CONFIG = 16


class CommandRxMode(StrEnum):
Expand Down
8 changes: 8 additions & 0 deletions hahomematic/context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Collection of context variables."""

from __future__ import annotations

from contextvars import ContextVar

# context var for storing if call is running within a service
IN_SERVICE_VAR: ContextVar[bool] = ContextVar("in_service_var", default=False)
12 changes: 6 additions & 6 deletions hahomematic/platforms/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
from typing import Any, ParamSpec, TypeVar

import hahomematic
from hahomematic.context import IN_SERVICE_VAR
from hahomematic.exceptions import BaseHomematicException
from hahomematic.support import reduce_args

Expand Down Expand Up @@ -141,17 +141,17 @@ def service_decorator(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[
async def service_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
"""Wrap service to log exception."""
token: Token | None = None
if not hahomematic.IN_SERVICE_VAR.get():
token = hahomematic.IN_SERVICE_VAR.set(True)
if not IN_SERVICE_VAR.get():
token = IN_SERVICE_VAR.set(True)
try:
return_value = await func(*args, **kwargs)
if token:
hahomematic.IN_SERVICE_VAR.reset(token)
IN_SERVICE_VAR.reset(token)
return return_value # noqa: TRY300
except BaseHomematicException as bhe:
if token:
hahomematic.IN_SERVICE_VAR.reset(token)
if not hahomematic.IN_SERVICE_VAR.get() and log_level > logging.NOTSET:
IN_SERVICE_VAR.reset(token)
if not IN_SERVICE_VAR.get() and log_level > logging.NOTSET:
logging.getLogger(args[0].__module__).log(
level=log_level, msg=reduce_args(args=bhe.args)
)
Expand Down
16 changes: 8 additions & 8 deletions hahomematic/platforms/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import voluptuous as vol

import hahomematic
from hahomematic import central as hmcu, client as hmcl, support as hms
from hahomematic.async_support import loop_check
from hahomematic.config import WAIT_FOR_CALLBACK
Expand Down Expand Up @@ -41,6 +40,7 @@
ParameterType,
ParamsetKey,
)
from hahomematic.context import IN_SERVICE_VAR
from hahomematic.exceptions import BaseHomematicException, HaHomematicException
from hahomematic.platforms import device as hmd
from hahomematic.platforms.decorators import config_property, get_service_calls, state_property
Expand Down Expand Up @@ -815,13 +815,13 @@ def bind_decorator[_CallableT: Callable[..., Any]](func: _CallableT) -> _Callabl
async def bind_wrapper(*args: Any, **kwargs: Any) -> Any:
"""Wrap method to add collector."""
token: Token | None = None
if not hahomematic.IN_SERVICE_VAR.get():
token = hahomematic.IN_SERVICE_VAR.set(True)
if not IN_SERVICE_VAR.get():
token = IN_SERVICE_VAR.set(True)
try:
if not enabled:
return_value = await func(*args, **kwargs)
if token:
hahomematic.IN_SERVICE_VAR.reset(token)
IN_SERVICE_VAR.reset(token)
return return_value
try:
collector_exists = args[argument_index] is not None
Expand All @@ -831,19 +831,19 @@ async def bind_wrapper(*args: Any, **kwargs: Any) -> Any:
if collector_exists:
return_value = await func(*args, **kwargs)
if token:
hahomematic.IN_SERVICE_VAR.reset(token)
IN_SERVICE_VAR.reset(token)
return return_value
collector = CallParameterCollector(client=args[0].channel.device.client)
kwargs[_COLLECTOR_ARGUMENT_NAME] = collector
return_value = await func(*args, **kwargs)
await collector.send_data(wait_for_callback=wait_for_callback)
if token:
hahomematic.IN_SERVICE_VAR.reset(token)
IN_SERVICE_VAR.reset(token)
return return_value # noqa:TRY300
except BaseHomematicException as bhe:
if token:
hahomematic.IN_SERVICE_VAR.reset(token)
in_service = hahomematic.IN_SERVICE_VAR.get()
IN_SERVICE_VAR.reset(token)
in_service = IN_SERVICE_VAR.get()
if not in_service and log_level > logging.NOTSET:
logging.getLogger(args[0].__module__).log(
level=log_level, msg=reduce_args(args=bhe.args)
Expand Down
4 changes: 2 additions & 2 deletions hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ def _make_value_hashable(value: Any) -> Any:
def get_rx_modes(mode: int) -> tuple[RxMode, ...]:
"""Convert int to rx modes."""
rx_modes: set[RxMode] = set()
if mode == 10:
if mode & RxMode.LAZY_CONFIG:
mode -= RxMode.LAZY_CONFIG
rx_modes.add(RxMode.LAZY_CONFIG)
return tuple(rx_modes)
if mode & RxMode.WAKEUP:
mode -= RxMode.WAKEUP
rx_modes.add(RxMode.WAKEUP)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hahomematic"
version = "2024.10.0"
version = "2024.10.1"
license = {text = "MIT License"}
description = "Homematic interface for Home Assistant running on Python 3."
readme = "README.md"
Expand Down

0 comments on commit 7440d74

Please sign in to comment.