aiohomematic-test-support 2025.12.7__tar.gz → 2025.12.19__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {aiohomematic_test_support-2025.12.7/aiohomematic_test_support.egg-info → aiohomematic_test_support-2025.12.19}/PKG-INFO +1 -1
  2. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/__init__.py +1 -1
  3. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19/aiohomematic_test_support.egg-info}/PKG-INFO +1 -1
  4. aiohomematic_test_support-2025.12.19/data/full_session_randomized_ccu.zip +0 -0
  5. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/factory.py +6 -6
  6. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/helper.py +1 -1
  7. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/mock.py +159 -7
  8. aiohomematic_test_support-2025.12.7/data/full_session_randomized_ccu.zip +0 -0
  9. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/MANIFEST.in +0 -0
  10. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/README.md +0 -0
  11. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/aiohomematic_test_support.egg-info/SOURCES.txt +0 -0
  12. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/aiohomematic_test_support.egg-info/dependency_links.txt +0 -0
  13. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/aiohomematic_test_support.egg-info/top_level.txt +0 -0
  14. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/const.py +0 -0
  15. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/data/full_session_randomized_pydevccu.zip +0 -0
  16. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/py.typed +0 -0
  17. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/pyproject.toml +0 -0
  18. {aiohomematic_test_support-2025.12.7 → aiohomematic_test_support-2025.12.19}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aiohomematic-test-support
3
- Version: 2025.12.7
3
+ Version: 2025.12.19
4
4
  Summary: Support-only package for AioHomematic (tests/dev). Not part of production builds.
5
5
  Author-email: SukramJ <sukramj@icloud.com>
6
6
  Project-URL: Homepage, https://github.com/SukramJ/aiohomematic
@@ -45,4 +45,4 @@ test dependencies to access test support functionality.
45
45
 
46
46
  from __future__ import annotations
47
47
 
48
- __version__ = "2025.12.7"
48
+ __version__ = "2025.12.19"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aiohomematic-test-support
3
- Version: 2025.12.7
3
+ Version: 2025.12.19
4
4
  Summary: Support-only package for AioHomematic (tests/dev). Not part of production builds.
5
5
  Author-email: SukramJ <sukramj@icloud.com>
6
6
  Project-URL: Homepage, https://github.com/SukramJ/aiohomematic
@@ -44,7 +44,7 @@ from aiohomematic.central import CentralConfig, CentralUnit
44
44
  from aiohomematic.central.event_bus import BackendSystemEventData, HomematicEvent
45
45
  from aiohomematic.client import ClientConfig, InterfaceConfig
46
46
  from aiohomematic.const import LOCAL_HOST, BackendSystemEvent, Interface, OptionalSettings
47
- from aiohomematic.interfaces import ClientProtocol
47
+ from aiohomematic.interfaces.client import ClientProtocol
48
48
  from aiohomematic_test_support import const
49
49
  from aiohomematic_test_support.mock import SessionPlayer, get_client_session, get_mock, get_xml_rpc_proxy
50
50
 
@@ -81,13 +81,13 @@ class FactoryWithClient:
81
81
  )
82
82
  self.system_event_mock = MagicMock()
83
83
  self.ha_event_mock = MagicMock()
84
- self._event_bus_unsubscribe_handlers: list[Callable[[], None]] = []
84
+ self._event_bus_unsubscribe_callbacks: list[Callable[[], None]] = []
85
85
 
86
86
  def cleanup_event_bus_subscriptions(self) -> None:
87
87
  """Clean up all event bus subscriptions."""
88
- for unsubscribe in self._event_bus_unsubscribe_handlers:
88
+ for unsubscribe in self._event_bus_unsubscribe_callbacks:
89
89
  unsubscribe()
90
- self._event_bus_unsubscribe_handlers.clear()
90
+ self._event_bus_unsubscribe_callbacks.clear()
91
91
 
92
92
  async def get_default_central(self, *, start: bool = True) -> CentralUnit:
93
93
  """Return a central based on give address_device_translation."""
@@ -146,12 +146,12 @@ class FactoryWithClient:
146
146
  """Handle homematic events."""
147
147
  self.ha_event_mock(event)
148
148
 
149
- self._event_bus_unsubscribe_handlers.append(
149
+ self._event_bus_unsubscribe_callbacks.append(
150
150
  central.event_bus.subscribe(
151
151
  event_type=BackendSystemEventData, event_key=None, handler=_system_event_handler
152
152
  )
153
153
  )
154
- self._event_bus_unsubscribe_handlers.append(
154
+ self._event_bus_unsubscribe_callbacks.append(
155
155
  central.event_bus.subscribe(event_type=HomematicEvent, event_key=None, handler=_ha_event_handler)
156
156
  )
157
157
 
@@ -11,7 +11,7 @@ import orjson
11
11
 
12
12
  from aiohomematic.central import CentralUnit
13
13
  from aiohomematic.const import UTF_8
14
- from aiohomematic.interfaces import CustomDataPointProtocol
14
+ from aiohomematic.interfaces.model import CustomDataPointProtocol
15
15
 
16
16
  _LOGGER = logging.getLogger(__name__)
17
17
 
@@ -37,6 +37,8 @@ from __future__ import annotations
37
37
 
38
38
  import asyncio
39
39
  from collections import defaultdict
40
+ from collections.abc import Callable
41
+ import contextlib
40
42
  import json
41
43
  import logging
42
44
  import os
@@ -52,7 +54,7 @@ from aiohomematic.client import BaseRpcProxy
52
54
  from aiohomematic.client.json_rpc import _JsonKey, _JsonRpcMethod
53
55
  from aiohomematic.client.rpc_proxy import _RpcMethod
54
56
  from aiohomematic.const import UTF_8, DataOperationResult, Parameter, ParamsetKey, RPCType
55
- from aiohomematic.store.persistent import _freeze_params, _unfreeze_params
57
+ from aiohomematic.store.persistent import _cleanup_params_for_session, _freeze_params, _unfreeze_params
56
58
  from aiohomematic_test_support import const
57
59
 
58
60
  _LOGGER = logging.getLogger(__name__)
@@ -376,25 +378,175 @@ def get_xml_rpc_proxy( # noqa: C901
376
378
  return cast(BaseRpcProxy, _AioXmlRpcProxyFromSession())
377
379
 
378
380
 
381
+ def _get_instance_attributes(instance: Any) -> set[str]:
382
+ """
383
+ Get all instance attribute names, supporting both __dict__ and __slots__.
384
+
385
+ For classes with __slots__, iterates through the class hierarchy to collect
386
+ all slot names. For classes with __dict__, returns the keys of __dict__.
387
+ Handles hybrid classes that have both __slots__ and __dict__.
388
+
389
+ Why this is needed:
390
+ Python classes can store instance attributes in two ways:
391
+ 1. __dict__: A dictionary attached to each instance (default behavior)
392
+ 2. __slots__: Pre-declared attribute names stored more efficiently
393
+
394
+ When copying attributes to a mock, we can't just use instance.__dict__
395
+ because __slots__-based classes don't have __dict__ (or have a limited one).
396
+ We must inspect the class hierarchy to find all declared slots.
397
+
398
+ Algorithm:
399
+ 1. Walk the Method Resolution Order (MRO) to find all classes in hierarchy
400
+ 2. For each class with __slots__, collect slot names (skip internal ones)
401
+ 3. Verify each slot actually has a value on this instance (getattr check)
402
+ 4. Also collect any __dict__ attributes if the instance has __dict__
403
+ 5. Return the union of all found attribute names
404
+ """
405
+ attrs: set[str] = set()
406
+
407
+ # Walk the class hierarchy via MRO (Method Resolution Order).
408
+ # __slots__ are inherited but each class defines its own slots separately,
409
+ # so we must check every class in the hierarchy.
410
+ for cls in type(instance).__mro__:
411
+ if hasattr(cls, "__slots__"):
412
+ for slot in cls.__slots__:
413
+ # Skip internal slots like __dict__ and __weakref__ which are
414
+ # automatically added by Python when a class uses __slots__
415
+ if not slot.startswith("__"):
416
+ try:
417
+ # Only include if the attribute actually exists on the instance.
418
+ # Slots can be declared but unset (raises AttributeError).
419
+ getattr(instance, slot)
420
+ attrs.add(slot)
421
+ except AttributeError:
422
+ # Slot is declared but not initialized on this instance
423
+ pass
424
+
425
+ # Also include __dict__ attributes if the instance has __dict__.
426
+ # Some classes have both __slots__ and __dict__ (e.g., if a parent class
427
+ # doesn't use __slots__, or if __dict__ is explicitly in __slots__).
428
+ if hasattr(instance, "__dict__"):
429
+ attrs.update(instance.__dict__.keys())
430
+
431
+ return attrs
432
+
433
+
379
434
  def get_mock(
380
435
  *, instance: Any, exclude_methods: set[str] | None = None, include_properties: set[str] | None = None, **kwargs: Any
381
436
  ) -> Any:
382
- """Create a mock and copy instance attributes over mock."""
437
+ """
438
+ Create a mock that wraps an instance with proper property delegation.
439
+
440
+ Supports both __dict__-based and __slots__-based classes. Properties are
441
+ delegated dynamically to the wrapped instance to ensure current values
442
+ are always returned.
443
+
444
+ Problem solved:
445
+ MagicMock(wraps=instance) only delegates method calls, not property access.
446
+ When you access mock.some_property, MagicMock returns the value that was
447
+ captured at mock creation time, not the current value on the wrapped instance.
448
+ This causes test failures when the wrapped instance's state changes after
449
+ the mock is created (e.g., client.available changes from False to True
450
+ after initialize_proxy() is called).
451
+
452
+ Solution:
453
+ Create a dynamic MagicMock subclass with property descriptors that delegate
454
+ to the wrapped instance on every access. This ensures properties always
455
+ return current values.
456
+
457
+ Algorithm:
458
+ 1. If already a Mock, just sync attributes from wrapped instance
459
+ 2. Identify all properties on the instance's class
460
+ 3. Create a MagicMock subclass with delegating property descriptors
461
+ 4. Create mock instance with spec and wraps
462
+ 5. Copy instance attributes (supports both __slots__ and __dict__)
463
+ 6. Copy non-mockable methods directly to mock
464
+ """
383
465
  if exclude_methods is None:
384
466
  exclude_methods = set()
385
467
  if include_properties is None:
386
468
  include_properties = set()
387
469
 
470
+ # Early return: if already a Mock, just refresh attributes from wrapped instance
388
471
  if isinstance(instance, Mock):
389
- instance.__dict__.update(instance._mock_wraps.__dict__)
472
+ if hasattr(instance, "_mock_wraps") and instance._mock_wraps is not None:
473
+ for attr in _get_instance_attributes(instance._mock_wraps):
474
+ with contextlib.suppress(AttributeError, TypeError):
475
+ setattr(instance, attr, getattr(instance._mock_wraps, attr))
390
476
  return instance
391
- mock = MagicMock(spec=instance, wraps=instance, **kwargs)
392
- mock.__dict__.update(instance.__dict__)
477
+
478
+ # Step 1: Identify all @property decorated attributes on the class
479
+ # These need special handling because MagicMock doesn't delegate property access
480
+ property_names = _get_properties(data_object=instance, decorator=property)
481
+
482
+ # Step 2: Create a dynamic MagicMock subclass
483
+ # We add property descriptors to this subclass that delegate to _mock_wraps.
484
+ # This is the key technique: property descriptors on the class take precedence
485
+ # over MagicMock's attribute access, allowing us to intercept property reads.
486
+ class _DynamicMock(MagicMock):
487
+ pass
488
+
489
+ # Helper factory functions to create closures with correct name binding.
490
+ # Using a factory function ensures each property gets its own 'name' variable,
491
+ # avoiding the classic lambda closure bug where all properties would share
492
+ # the last loop value.
493
+ def _make_getter(name: str) -> Callable[[Any], Any]:
494
+ """Create a getter that delegates to the wrapped instance."""
495
+
496
+ def getter(self: Any) -> Any:
497
+ # Access _mock_wraps which holds the original instance
498
+ return getattr(self._mock_wraps, name)
499
+
500
+ return getter
501
+
502
+ def _make_setter(name: str) -> Callable[[Any, Any], None]:
503
+ """Create a setter that delegates to the wrapped instance."""
504
+
505
+ def setter(self: Any, value: Any) -> None:
506
+ setattr(self._mock_wraps, name, value)
507
+
508
+ return setter
509
+
510
+ # Step 3: Add property descriptors to the dynamic subclass
511
+ for prop_name in property_names:
512
+ # Skip properties that should be mocked or overridden via kwargs
513
+ if prop_name not in include_properties and prop_name not in kwargs:
514
+ # Check if the original property has a setter (is writable)
515
+ prop_descriptor = getattr(type(instance), prop_name, None)
516
+ if prop_descriptor is not None and getattr(prop_descriptor, "fset", None) is not None:
517
+ # Writable property: create descriptor with both getter and setter
518
+ setattr(
519
+ _DynamicMock,
520
+ prop_name,
521
+ property(_make_getter(prop_name), _make_setter(prop_name)),
522
+ )
523
+ else:
524
+ # Read-only property: create descriptor with getter only
525
+ setattr(
526
+ _DynamicMock,
527
+ prop_name,
528
+ property(_make_getter(prop_name)),
529
+ )
530
+
531
+ # Step 4: Create the mock instance
532
+ # spec=instance: ensures mock only allows access to attributes that exist on instance
533
+ # wraps=instance: delegates method calls to the real instance
534
+ mock = _DynamicMock(spec=instance, wraps=instance, **kwargs)
535
+
536
+ # Step 5: Copy instance attributes to mock
537
+ # This handles both __slots__ and __dict__ based classes via _get_instance_attributes()
538
+ for attr in _get_instance_attributes(instance):
539
+ with contextlib.suppress(AttributeError, TypeError):
540
+ setattr(mock, attr, getattr(instance, attr))
541
+
542
+ # Step 6: Copy non-mockable methods directly
543
+ # Some methods (like bound methods or special attributes) need to be copied
544
+ # directly rather than being mocked
393
545
  try:
394
546
  for method_name in [
395
547
  prop
396
548
  for prop in _get_not_mockable_method_names(instance=instance, exclude_methods=exclude_methods)
397
- if prop not in include_properties and prop not in kwargs
549
+ if prop not in include_properties and prop not in kwargs and prop not in property_names
398
550
  ]:
399
551
  setattr(mock, method_name, getattr(instance, method_name))
400
552
  except Exception:
@@ -513,7 +665,7 @@ class SessionPlayer:
513
665
  return None
514
666
  if not (bucket_by_parameter := bucket_by_method.get(method)):
515
667
  return None
516
- frozen_params = _freeze_params(params=params)
668
+ frozen_params = _freeze_params(params=_cleanup_params_for_session(params=params))
517
669
 
518
670
  # For each parameter, choose the response at the latest timestamp.
519
671
  if (bucket_by_ts := bucket_by_parameter.get(frozen_params)) is None: