aiohomematic-test-support 2025.12.13__py3-none-any.whl → 2025.12.20__py3-none-any.whl
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.
- aiohomematic_test_support/__init__.py +1 -1
- aiohomematic_test_support/data/full_session_randomized_ccu.zip +0 -0
- aiohomematic_test_support/factory.py +5 -5
- aiohomematic_test_support/mock.py +159 -7
- {aiohomematic_test_support-2025.12.13.dist-info → aiohomematic_test_support-2025.12.20.dist-info}/METADATA +1 -1
- aiohomematic_test_support-2025.12.20.dist-info/RECORD +12 -0
- aiohomematic_test_support-2025.12.13.dist-info/RECORD +0 -12
- {aiohomematic_test_support-2025.12.13.dist-info → aiohomematic_test_support-2025.12.20.dist-info}/WHEEL +0 -0
- {aiohomematic_test_support-2025.12.13.dist-info → aiohomematic_test_support-2025.12.20.dist-info}/top_level.txt +0 -0
|
Binary file
|
|
@@ -81,13 +81,13 @@ class FactoryWithClient:
|
|
|
81
81
|
)
|
|
82
82
|
self.system_event_mock = MagicMock()
|
|
83
83
|
self.ha_event_mock = MagicMock()
|
|
84
|
-
self.
|
|
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.
|
|
88
|
+
for unsubscribe in self._event_bus_unsubscribe_callbacks:
|
|
89
89
|
unsubscribe()
|
|
90
|
-
self.
|
|
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.
|
|
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.
|
|
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
|
|
|
@@ -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
|
-
"""
|
|
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
|
-
|
|
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
|
-
|
|
392
|
-
|
|
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:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aiohomematic-test-support
|
|
3
|
-
Version: 2025.12.
|
|
3
|
+
Version: 2025.12.20
|
|
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
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
aiohomematic_test_support/__init__.py,sha256=KSoWaxPrP-1EJlAEI1KxRB3KJqvj8mbFXWOcsa-sp3g,1623
|
|
2
|
+
aiohomematic_test_support/const.py,sha256=YXN3FvZCqjxOaqCs0E7KTz2PUa1zONzS9-W_6YqJrPI,19755
|
|
3
|
+
aiohomematic_test_support/factory.py,sha256=E3tHyeIAO1Id1c0ODP7WnUs_XOgnt14bMzPyow5SEJc,10772
|
|
4
|
+
aiohomematic_test_support/helper.py,sha256=Ue2tfy10_fiuMjYsc1jYPvo5sEtMF2WVKjvLnTZ0TzU,1360
|
|
5
|
+
aiohomematic_test_support/mock.py,sha256=CJmwrVXOGMjH43P7Av7rfUZAvphxUhnhWcvpXSyWxWw,30113
|
|
6
|
+
aiohomematic_test_support/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
aiohomematic_test_support/data/full_session_randomized_ccu.zip,sha256=kKHMtJouCskAHkXaP1hHRSoWZO2cZDJ9P_EwuJuywJQ,733017
|
|
8
|
+
aiohomematic_test_support/data/full_session_randomized_pydevccu.zip,sha256=_QFWSP03dkiMFdD_w-R98DS6ur4PYDQXw-DCkbJEGg4,1293240
|
|
9
|
+
aiohomematic_test_support-2025.12.20.dist-info/METADATA,sha256=Agz5LWI8hAQGZ17JcQ-b_fkrjQdz4ZqtLjl0EAQwuu8,536
|
|
10
|
+
aiohomematic_test_support-2025.12.20.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
11
|
+
aiohomematic_test_support-2025.12.20.dist-info/top_level.txt,sha256=KmK-OiDDbrmawIsIgPWNAkpkDfWQnOoumYd9MXAiTHc,26
|
|
12
|
+
aiohomematic_test_support-2025.12.20.dist-info/RECORD,,
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
aiohomematic_test_support/__init__.py,sha256=QZ6fA_Lk1iukixJUU6h8a4XcgF3OhzQaiVhUYc9VtTA,1623
|
|
2
|
-
aiohomematic_test_support/const.py,sha256=YXN3FvZCqjxOaqCs0E7KTz2PUa1zONzS9-W_6YqJrPI,19755
|
|
3
|
-
aiohomematic_test_support/factory.py,sha256=BT0bsplFO5nPqAJo2zwyI5QmNVDu32JcrgTCQbpB2dU,10767
|
|
4
|
-
aiohomematic_test_support/helper.py,sha256=Ue2tfy10_fiuMjYsc1jYPvo5sEtMF2WVKjvLnTZ0TzU,1360
|
|
5
|
-
aiohomematic_test_support/mock.py,sha256=z91_mjLMF09B5FprCnVeXwXpWHM1ZWe4h2nn97pGXR8,22713
|
|
6
|
-
aiohomematic_test_support/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
-
aiohomematic_test_support/data/full_session_randomized_ccu.zip,sha256=oN7g0CB_0kQX3qk-RSu-Rt28yW7BcplVqPYZCDqr0EU,734626
|
|
8
|
-
aiohomematic_test_support/data/full_session_randomized_pydevccu.zip,sha256=_QFWSP03dkiMFdD_w-R98DS6ur4PYDQXw-DCkbJEGg4,1293240
|
|
9
|
-
aiohomematic_test_support-2025.12.13.dist-info/METADATA,sha256=BbOorwMcviBXtN2mBdNtdJVpuQQ9SCJLLyyMMgvIzl0,536
|
|
10
|
-
aiohomematic_test_support-2025.12.13.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
11
|
-
aiohomematic_test_support-2025.12.13.dist-info/top_level.txt,sha256=KmK-OiDDbrmawIsIgPWNAkpkDfWQnOoumYd9MXAiTHc,26
|
|
12
|
-
aiohomematic_test_support-2025.12.13.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|