Refactor: Enforce strict typed configuration and eradicate **kwargs tunnel (Resolves #481)#505
Conversation
|
Hi @IMMRMKW, First off, great work on the Zigbee transport integration in #502! It is a brilliant addition to the project. I wanted to give you a quick heads-up about this PR. Over the last few weeks, we've been heavily refactoring the underlying architecture of As part of this cleanup, this PR completely removes the old What this means for Python I know you have ongoing/WIP integrations for As this is a breaking change, @silverailscolo is going to have to manage the version bump on both Upcoming Refactoring: I still have a few things left that I want to refactor in
Please take this in a positive way, as it is meant to be! I don't like to just moan in the comments; I prefer to get in and write code to help out. If there is anything in my upcoming PRs that you think I have got wrong or that causes issues for your Zigbee implementation, please let me know and I will be happy to change it. My only aim is to help build a robust foundation that reports errors cleanly. Thanks again for the great feature! Phil / PWhite-Eng |
… DTOs" -m "Introduce GatewayConfig, TransportConfig, and DeviceTraits models. Remove opaque **kwargs, hidden_kwargs, and self._extra data tunnels across the Gateway and Transport layers. Update Zigbee transport to consume app_context explicitly.
35a4fc5 to
17146ab
Compare
Minimum allowed line rate is |
@IMMRMKW @PWhite-Eng I suggest as follows: ramses_rf
ramses_cc(Easy path would be to merge PR 515 and add fixes later, but that would leave us with an unstable integration, not preferred)
|
The Problem:
Initialization of the
Gateway,Engine, and Transports relied heavily on unpacking dynamic**kwargsand tunneling data (such as Home Assistant'shassobject) through opaqueself._extraorhidden_kwargsdictionaries. This bypassed static type checking, hid the API contract from downstream consumers, and caused brittle integrations (Reference: Issue #481).Consequences:
Consumers (like
ramses_cc) could experience runtime crashes due to unvalidated configuration structures. Furthermore, static analysis tools like Mypy could not verify parameters, and the data flow to the transport layer (e.g., the Zigbee transport requiring thehassinstance) was entirely undocumented and untyped.The Fix:
Removed
**kwargsandself._extraentirely from the instantiation chain. Introduced strongly typed dataclasses (GatewayConfig,TransportConfig, andDeviceTraits) to handle configuration, application context, and device instantiation explicitly.Technical Implementation:
src/ramses_rf/models.pywith aDeviceTraitsDTO to enforce typed dictionary parsing for device instantiation.app_contextexplicitly toGatewayConfigandTransportConfig. This allows passing the Home Assistant instance down to transports (like Zigbee) securely and transparently.src/ramses_rf/gateway.pyandsrc/ramses_tx/gateway.pysignatures to completely drop**kwargsandhidden_kwargs.transport_factoryandZigbeeTransportto pullhassdirectly fromconfig.app_contextinstead of a genericextradictionary.Testing Performed:
pytestsuite locally to ensure no regressions in entity initialization, transport creation, or packet parsing. All tests pass.test_transport_zigbee.pyto mock the new typed config structures.mypyin strict mode against the entire codebase to confirm zero static analysis errors across the new architectural boundaries.Risks of NOT Implementing:
Leaving the codebase as-is means the public API remains undocumented and un-typeable. It preserves a "God-object" initialization anti-pattern that leaves downstream consumers highly vulnerable to hidden runtime crashes.
Risks of Implementing:
This introduces a breaking change for downstream consumers initializing the
Gatewaywith legacy**kwargs. Downstream systems (ramses_cc) MUST update their initialization calls to use the new typed configuration classes.Mitigation Steps:
Created clear, strictly-typed DTOs and configuration classes so the migration path for
ramses_ccis obvious and IDE-autocomplete friendly. For the internal device layer,Device.create_from_schematemporarily utilizes a.to_dict()bridge from the new DTO to prevent a massive blast radius across all internal device subclasses.AI Assistance Disclosure:
This contribution was developed with the assistance of Google Gemini 3.1 Pro for code generation and documentation. No Agentic AI systems were employed; all logic and implementations were reviewed, verified, and manually committed by the author.