Skip to content

Commit 0f699f2

Browse files
author
Tom Lasswell
committed
fix: Publish ptReal to device topic instead of account topic
- Publish commands to GD/{device_id} topic instead of account subscription topic - Include device ID and SKU in the ptReal payload for proper targeting - Handle non-state messages gracefully (ignore command responses) This fixes the disconnection that occurred after every publish when sending to the account topic we were subscribed to.
1 parent 5298295 commit 0f699f2

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

custom_components/govee/api/mqtt.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ async def _connection_loop(self) -> None:
314314
async def _handle_message(self, message: Any) -> None:
315315
"""Handle incoming AWS IoT MQTT message.
316316
317-
Message format from PCAP analysis:
317+
Message format from PCAP analysis (state updates):
318318
{
319319
"device": "XX:XX:XX:XX:XX:XX:XX:XX",
320320
"sku": "H6072",
@@ -325,18 +325,30 @@ async def _handle_message(self, message: Any) -> None:
325325
"colorTemInKelvin": 0
326326
}
327327
}
328+
329+
Command responses and other messages are silently ignored.
328330
"""
329331
try:
330332
raw_payload = message.payload
331333
payload_str = raw_payload.decode() if isinstance(raw_payload, bytes) else str(raw_payload)
332334

333335
data = json.loads(payload_str)
334336

337+
# Ignore command messages (our own publishes or responses)
338+
if "msg" in data:
339+
_LOGGER.debug("Ignoring command/response message")
340+
return
341+
335342
device_id = data.get("device")
336343
state = data.get("state", {})
337344

345+
# Only process messages with device ID and state
338346
if not device_id:
339-
_LOGGER.debug("AWS IoT message missing device ID")
347+
_LOGGER.debug("AWS IoT message missing device ID, ignoring")
348+
return
349+
350+
if not state:
351+
_LOGGER.debug("AWS IoT message missing state for %s, ignoring", device_id)
340352
return
341353

342354
_LOGGER.debug(
@@ -377,27 +389,30 @@ async def async_publish_ptreal(
377389
_LOGGER.warning("Cannot publish ptReal: MQTT not connected")
378390
return False
379391

380-
# Build ptReal payload
392+
# Build ptReal payload with device targeting
381393
payload = {
382394
"msg": {
383395
"cmd": "ptReal",
384396
"data": {
385397
"command": [ble_packet_base64],
398+
"device": device_id,
399+
"sku": sku,
386400
},
387401
"cmdVersion": 0,
388402
"transaction": f"v_{int(time.time() * 1000)}",
389403
"type": 1,
390404
}
391405
}
392406

393-
# Publish to account topic (same as subscription topic)
394-
topic = self._credentials.account_topic
407+
# Build device-specific topic for commands
408+
# Format: GD/{device_id} for device commands (vs GA/{account} for account-level)
409+
device_topic = f"GD/{device_id}"
395410

396411
try:
397-
await self._client.publish(topic, json.dumps(payload))
412+
await self._client.publish(device_topic, json.dumps(payload))
398413
_LOGGER.debug(
399414
"Published ptReal to %s for device %s (sku=%s)",
400-
topic[:30] + "...",
415+
device_topic,
401416
device_id,
402417
sku,
403418
)

0 commit comments

Comments
 (0)