fix: mqtt subscription format

- Use gmqtt's Subscription class for MQTT subscriptions
- Updated both _post_connect and subscribe methods
This commit is contained in:
Josh Finlay 2025-01-08 10:05:53 +10:00
parent 92daf9c8ad
commit 7a22813d8e
1 changed files with 6 additions and 3 deletions

View File

@ -3,6 +3,7 @@ import json
import asyncio import asyncio
from typing import Optional, Callable, Union from typing import Optional, Callable, Union
from gmqtt import Client as MQTTClient from gmqtt import Client as MQTTClient
from gmqtt.mqtt.subscription import Subscription
import logging import logging
# Get logger # Get logger
@ -102,8 +103,9 @@ class HomeAssistantMQTT:
logger.info("Connected to MQTT broker") logger.info("Connected to MQTT broker")
self._connected = True self._connected = True
try: try:
# Subscribe to command topic using a dict # Subscribe to command topic using gmqtt's format
await self.client.subscribe([{'topic': self.command_topic, 'qos': 1}]) sub = Subscription(self.command_topic, qos=1)
await self.client.subscribe([sub])
logger.info(f"Subscribed to command topic: {self.command_topic}") logger.info(f"Subscribed to command topic: {self.command_topic}")
# Publish discovery config # Publish discovery config
@ -192,7 +194,8 @@ class HomeAssistantMQTT:
"""Subscribe to a topic""" """Subscribe to a topic"""
if self.client and self._connected: if self.client and self._connected:
try: try:
await self.client.subscribe([{'topic': topic, 'qos': 1}]) sub = Subscription(topic, qos=1)
await self.client.subscribe([sub])
except Exception as e: except Exception as e:
logger.error(f"Failed to subscribe to topic: {e}") logger.error(f"Failed to subscribe to topic: {e}")