diff --git a/samsung_appliance/state_cache.py b/samsung_appliance/state_cache.py index 852c34f..f44eef7 100644 --- a/samsung_appliance/state_cache.py +++ b/samsung_appliance/state_cache.py @@ -8,26 +8,26 @@ from __future__ import annotations import threading import time -from typing import Callable, Optional, TYPE_CHECKING - -if TYPE_CHECKING: - from .appliances.base import ApplianceDescriptor +from typing import Callable, Optional class StateCache: - def __init__(self, descriptor: 'ApplianceDescriptor'): - self.descriptor = descriptor + def __init__(self): self.links: dict[str, dict] = {} self.last_updated: dict[str, float] = {} self.source: dict[str, str] = {} self.descriptor_state: dict = {} self._on_change: Optional[Callable[[bool, str], None]] = None + self._on_observation: Optional[Callable[[dict, str, dict], None]] = None self._lock = threading.RLock() def set_on_change(self, cb: Callable[[bool, str], None]) -> None: self._on_change = cb + def set_on_observation(self, hook: Optional[Callable[[dict, str, dict], None]]) -> None: + self._on_observation = hook + def apply_rep(self, href: str, rep: dict, source: str) -> bool: if not isinstance(rep, dict): return False @@ -37,7 +37,7 @@ class StateCache: self.links[href] = rep self.last_updated[href] = time.time() self.source[href] = source - hook = self.descriptor.on_observation + hook = self._on_observation if hook is not None: try: hook(self.descriptor_state, href, rep) diff --git a/tests/test_state_cache.py b/tests/test_state_cache.py new file mode 100644 index 0000000..b5b9560 --- /dev/null +++ b/tests/test_state_cache.py @@ -0,0 +1,15 @@ +from samsung_appliance.state_cache import StateCache + + +def test_state_cache_constructs_without_descriptor(): + c = StateCache() + assert c.links == {} + assert c.descriptor_state == {} + + +def test_on_observation_hook_fires(): + c = StateCache() + seen = [] + c.set_on_observation(lambda st, href, rep: seen.append((href, rep))) + c.apply_rep('/x/vs/0', {'a': 1}, source='seed') + assert seen == [('/x/vs/0', {'a': 1})]