Add Marstek MQTT responder to answer CT002/CT003 polls locally#328
Draft
Add Marstek MQTT responder to answer CT002/CT003 polls locally#328
Conversation
When [MARSTEK] credentials are configured, the managed fake CT MAC returned by ensure_managed_fake_device() is now used to answer the Marstek CT002/CT003 MQTT poll protocol (cd=1 on hame_energy/marstek_energy topics) on the same broker as MQTT Insights. Combined with hame-relay, this makes the emulator's readings visible as a CT in the Marstek app. Enabled by default via MARSTEK_MQTT_ENABLED in [MQTT_INSIGHTS]; opt out by setting it to false. Without Marstek credentials the responder stays silent (one info log per CT device). https://claude.ai/code/session_01K5ypPxYASWXJewf7Lk9a1e
The MQTT Insights listener is a single async-for loop. When a Marstek poll handler awaited binding.get_values() inline it could block every subsequent message (other CT polls, Insights commands) for as long as the powermeter took to yield a reading. Spawn each response in its own task instead, and track/cancel those tasks on disconnect/shutdown. Also snapshot _marstek_bindings under the lock before scanning in _find_marstek_binding, and drop the type: ignore on the topic helpers by returning an explicit 2-tuple. Adds two integration tests: register-before-start populates subscriptions on first connect, and a slow handler on one binding doesn't block a concurrent fast poll on another binding. https://claude.ai/code/session_01K5ypPxYASWXJewf7Lk9a1e
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
Adds a Marstek MQTT responder feature to the MQTT Insights service that allows AstraMeter to answer Marstek CT002/CT003 poll requests on the local MQTT broker. When combined with hame-relay bridging to the Marstek cloud, this surfaces the emulator in the Marstek mobile app as if it were a real device.
Key Changes
New
marstek_mqtt.pymodule: Pure helper functions and dataclass for the Marstek MQTT protocolMarstekMqttBinding: Dataclass holding per-device registration (device_id, ct_type, MAC, get_values callback, wifi_rssi, ver_v)app_topics_for(),device_topics_for(),parse_app_topic()is_poll_payload(),build_response(),normalize_mac()marstek_mqtt_test.pyExtended
MqttInsightsService:marstek_mqtt_enabledconfig option (default: true)register_marstek()andunregister_marstek()for device lifecycle management_handle_marstek_message()that dispatches polls quickly_serve_marstek_poll()that offloads to async tasks to prevent slow powermeters from blocking the listener loopIntegration in
main.py:marstek_macthrough device lifecycleConfiguration:
MARSTEK_MQTT_ENABLEDconfig option to[MQTT_INSIGHTS]sectionImplementation Details
hame_energy/andmarstek_energy/topic prefixes for compatibilityhttps://claude.ai/code/session_01K5ypPxYASWXJewf7Lk9a1e