Adds support for detection sensor events#176
Conversation
📝 WalkthroughWalkthroughAdds decoding support for DETECTION_SENSOR_APP packets in the Meshtastic packet parser, and introduces a new Home Assistant bus event, listener registration, and callback in the API client to emit detection sensor events with gateway and message ID information. ChangesDetection Sensor Event Support
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant MeshNode
participant Packet
participant MeshtasticApiClient
participant HomeAssistantBus
MeshNode->>Packet: deliver DETECTION_SENSOR_APP packet
Packet->>Packet: app_payload() decodes payload string
Packet->>MeshtasticApiClient: pass decoded Packet
MeshtasticApiClient->>MeshtasticApiClient: _on_detection_sensor(node, packet)
MeshtasticApiClient->>MeshtasticApiClient: _build_event_data(node, packet)
MeshtasticApiClient->>HomeAssistantBus: fire EVENT_MESHTASTIC_API_DETECTION_SENSOR
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
custom_components/meshtastic/api.py (1)
277-318: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
_on_detection_sensorduplicates_on_text_message.The new callback is identical to
_on_text_message(Lines 277-296) apart from the fired event name. Extract the shared to/from/event-data construction into a helper to avoid maintaining two copies of the same logic.♻️ Proposed refactor
- async def _on_text_message(self, node: MeshNode, packet: Packet) -> None: - if packet.to_id == MeshInterface.BROADCAST_NUM: - to_channel = packet.channel_index - to_node = None - else: - to_channel = None - to_node = packet.to_id - - event_data = self._build_event_data( - node.id, - { - "from": packet.from_id, - "to": {"node": to_node, "channel": to_channel}, - "gateway": self.get_own_node()["num"], - "message": packet.app_payload, - }, - ) - - event_data["message_id"] = packet.mesh_packet.id - self._hass.bus.async_fire(EVENT_MESHTASTIC_API_TEXT_MESSAGE, event_data) - - async def _on_detection_sensor(self, node: MeshNode, packet: Packet) -> None: - if packet.to_id == MeshInterface.BROADCAST_NUM: - to_channel = packet.channel_index - to_node = None - else: - to_channel = None - to_node = packet.to_id - - event_data = self._build_event_data( - node.id, - { - "from": packet.from_id, - "to": {"node": to_node, "channel": to_channel}, - "gateway": self.get_own_node()["num"], - "message": packet.app_payload, - }, - ) - - event_data["message_id"] = packet.mesh_packet.id - self._hass.bus.async_fire(EVENT_MESHTASTIC_API_DETECTION_SENSOR, event_data) + def _build_message_event_data(self, node: MeshNode, packet: Packet) -> MutableMapping[str, Any]: + if packet.to_id == MeshInterface.BROADCAST_NUM: + to_channel = packet.channel_index + to_node = None + else: + to_channel = None + to_node = packet.to_id + + event_data = self._build_event_data( + node.id, + { + "from": packet.from_id, + "to": {"node": to_node, "channel": to_channel}, + "gateway": self.get_own_node()["num"], + "message": packet.app_payload, + }, + ) + event_data["message_id"] = packet.mesh_packet.id + return event_data + + async def _on_text_message(self, node: MeshNode, packet: Packet) -> None: + self._hass.bus.async_fire(EVENT_MESHTASTIC_API_TEXT_MESSAGE, self._build_message_event_data(node, packet)) + + async def _on_detection_sensor(self, node: MeshNode, packet: Packet) -> None: + self._hass.bus.async_fire( + EVENT_MESHTASTIC_API_DETECTION_SENSOR, self._build_message_event_data(node, packet) + )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@custom_components/meshtastic/api.py` around lines 277 - 318, The new _on_detection_sensor callback is duplicating the full message-building flow from _on_text_message, so extract the shared packet-to-event construction into a helper used by both methods. Keep the event-specific async_fire target separate, but move the repeated to_channel/to_node logic, _build_event_data call, and message_id assignment into a reusable helper in custom_components.meshtastic.api to avoid two copies of the same code.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@custom_components/meshtastic/api.py`:
- Around line 277-318: The new _on_detection_sensor callback is duplicating the
full message-building flow from _on_text_message, so extract the shared
packet-to-event construction into a helper used by both methods. Keep the
event-specific async_fire target separate, but move the repeated
to_channel/to_node logic, _build_event_data call, and message_id assignment into
a reusable helper in custom_components.meshtastic.api to avoid two copies of the
same code.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c0b8270c-7461-4656-94ff-20fcc406285a
📒 Files selected for processing (2)
custom_components/meshtastic/aiomeshtastic/packet.pycustom_components/meshtastic/api.py
Closes #72
Summary by CodeRabbit