Skip to content

Adds support for detection sensor events#176

Open
gwe8r7hw837rh87h wants to merge 2 commits into
meshtastic:mainfrom
gwe8r7hw837rh87h:detection-sensor-event
Open

Adds support for detection sensor events#176
gwe8r7hw837rh87h wants to merge 2 commits into
meshtastic:mainfrom
gwe8r7hw837rh87h:detection-sensor-event

Conversation

@gwe8r7hw837rh87h

@gwe8r7hw837rh87h gwe8r7hw837rh87h commented Jul 8, 2026

Copy link
Copy Markdown

Closes #72

Summary by CodeRabbit

  • New Features
    • Added support for Detection Sensor app messages.
    • These packets are now decoded as text and surfaced through a new Home Assistant event, including gateway and message ID details.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds 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.

Changes

Detection Sensor Event Support

Layer / File(s) Summary
Payload decoding
custom_components/meshtastic/aiomeshtastic/packet.py
Packet.app_payload adds a branch for PortNum.DETECTION_SENSOR_APP that decodes the payload as a string.
Event constant and listener registration
custom_components/meshtastic/api.py
Adds EVENT_MESHTASTIC_API_DETECTION_SENSOR constant and registers a listener routing DETECTION_SENSOR_APP packets to a new _on_detection_sensor callback.
Event callback and firing logic
custom_components/meshtastic/api.py
Implements _on_detection_sensor, mirroring text-message routing for broadcast vs. targeted packets, builds event data with message_id, and fires the new bus event.

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
Loading

Poem

A sensor blinks, a signal hops,
Through mesh and air, my rabbit ears go pop!
No more events left in the dark,
Now every motion leaves its mark. 🐇📡
Thump thump — hooray, the bus event fired!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding detection sensor event support.
Linked Issues check ✅ Passed The PR captures and surfaces Meshtastic detection sensor events in Home Assistant, matching issue #72's main requirement.
Out of Scope Changes check ✅ Passed The changes stay focused on detection sensor support and do not introduce unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
custom_components/meshtastic/api.py (1)

277-318: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

_on_detection_sensor duplicates _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

📥 Commits

Reviewing files that changed from the base of the PR and between 3594f35 and 4c5c41a.

📒 Files selected for processing (2)
  • custom_components/meshtastic/aiomeshtastic/packet.py
  • custom_components/meshtastic/api.py

@CLAassistant

CLAassistant commented Jul 8, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detection Module = Create Event and/or Detection Sensor

2 participants