BREAKING: Remove constructor callbacks in favor of event emitter pattern#33
Merged
BREAKING: Remove constructor callbacks in favor of event emitter pattern#33
Conversation
- Remove on_connection_interrupted and on_connection_resumed parameters from NavienMqttClient
- Update examples to use event emitter pattern (mqtt_client.on('connection_interrupted', ...))
- Update documentation to reflect new pattern
- Add backward compatibility policy to Copilot instructions
- Update CHANGELOG.rst with migration guide
BREAKING CHANGE: Constructor callbacks removed. Use event emitter pattern instead:
# Old (removed)
mqtt_client = NavienMqttClient(auth, on_connection_interrupted=handler)
# New
mqtt_client = NavienMqttClient(auth)
mqtt_client.on('connection_interrupted', handler)
Benefits:
- Multiple listeners per event
- Consistent API with other events
- Dynamic listener management
- Async handler support
These variables are not legacy or backward compatibility code - they are essential connection state tracking used 33+ times throughout the codebase. - _connected: Simpler to check than _connection_manager.is_connected - _connection: Passed to MqttSubscriptionManager which needs raw connection - Both: Handle the case before connect() when _connection_manager is None The comment was misleading as it suggested these should be removed eventually, when in fact they serve important architectural purposes.
Changed: - 'Update legacy state for backward compatibility' → 'Update connection state' - 'Update legacy state' → 'Clear connection state' These variables are essential connection state tracking, not legacy code.
BREAKING CHANGE: Exception re-exports removed from api_client and auth modules. Removed: - APIError re-export from api_client module - AuthenticationError, InvalidCredentialsError, TokenExpiredError, TokenRefreshError re-exports from auth module Migration: # Old (removed) from nwp500.api_client import APIError from nwp500.auth import AuthenticationError # New (import from exceptions or package root) from nwp500.exceptions import APIError, AuthenticationError # OR from nwp500 import APIError, AuthenticationError Updated all examples to import exceptions from correct module. Rationale: Library is young with no external clients. Removing backward compatibility prevents accumulation of legacy patterns and maintains clean architecture.
- Fix line length in connection state comment (80 char limit) - Import exceptions from exceptions module (not auth/api_client) - Remove unused type: ignore comment All CI checks now pass: ✅ Ruff linting ✅ Ruff formatting ✅ Mypy type checking
- Fix test_auth.py to import exceptions from exceptions module - Update copilot instructions to make validation checks REQUIRED before completing any task - All checks now passing: ✅ Tests: 123/123 passed ✅ Linting: All checks passed ✅ Type checking: No errors
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR removes duplicate callback parameters from the NavienMqttClient constructor, standardizing on the event emitter pattern for all connection events. This is a breaking change that simplifies the API by eliminating redundant functionality and establishing a consistent event-driven architecture.
Key Changes:
- Removed
on_connection_interruptedandon_connection_resumedconstructor parameters - Standardized all examples to use the
.on()event emitter pattern - Cleaned up backward compatibility exception re-exports from
api_clientandauthmodules - Updated documentation to reflect the new event-only approach
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/nwp500/mqtt_client.py | Removed constructor callback parameters and internal callback invocation logic |
| src/nwp500/auth.py | Removed backward compatibility exception re-exports from __all__ |
| src/nwp500/api_client.py | Removed backward compatibility exception re-exports and simplified imports |
| tests/test_auth.py | Updated imports to use exceptions module |
| examples/reconnection_demo.py | Migrated from constructor callbacks to event emitter pattern |
| examples/command_queue_demo.py | Migrated from constructor callbacks to event emitter pattern |
| examples/test_api_client.py | Updated imports to use exceptions module |
| examples/device_status_callback_debug.py | Updated imports to use exceptions module |
| examples/device_status_callback.py | Updated imports to use exceptions module |
| examples/device_feature_callback.py | Updated imports to use exceptions module |
| docs/python_api/mqtt_client.rst | Updated API documentation and examples to show event emitter pattern |
| CHANGELOG.rst | Added v6.0.0 entry with breaking changes documentation and migration guide |
| .github/copilot-instructions.md | Added backward compatibility policy and enhanced validation requirements |
src/nwp500/mqtt_client.py
Outdated
Comment on lines
+179
to
+181
| # Connection state tracking | ||
| # Note: Simpler to check than _connection_manager.is_connected | ||
| # (which can be None before connect()) |
There was a problem hiding this comment.
[nitpick] The comment spans lines 179-181, but the actual implementation detail being explained is only on line 182. The comment should be condensed to a single line or reformatted to be more concise since it's explaining a simple state tracking variable.
Suggested change
| # Connection state tracking | |
| # Note: Simpler to check than _connection_manager.is_connected | |
| # (which can be None before connect()) | |
| # Tracks the current MQTT connection (simpler to check than _connection_manager.is_connected, which can be None before connect()) |
Address PR feedback: comment was unnecessarily verbose for simple state tracking variables. Reduced from 3 lines to 1 concise line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR removes the duplicate callback parameters from the
NavienMqttClientconstructor and standardizes on the event emitter pattern for all client events.Breaking Changes
Removed constructor parameters:
on_connection_interruptedon_connection_resumedMigration:
Benefits
temperature_changed,mode_changed, etc..on()and.off()Changes
Code Changes
Example Updates
Documentation Updates
Validation
✅ Type checking: No errors
✅ Linting: All checks passed
✅ Syntax: All files compile successfully
✅ References: No remaining old pattern usage
Version Impact
This requires a major version bump (v5.0.2 → v6.0.0) due to breaking API changes.
Rationale
The library is young with no external clients. This change eliminates duplicate functionality and establishes a single, consistent pattern for event handling across the entire API.