Skip to content

Commit

Permalink
config: add support for qos and retain in mqtt
Browse files Browse the repository at this point in the history
mqtt:
    # ip/dns for the mqtt broker
    host: <broker_ip_or_dns_name>
    # uncomment to publish with retain
    retain: true
    # specify qos value (default is 0)
    qos: 1

Fixes: #7
Signed-off-by: Flavio Fernandes <[email protected]>
  • Loading branch information
flavio-fernandes committed Aug 18, 2023
1 parent 69d1ba8 commit 9269fba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mqtt:
host: 192.168.1.250
username: <optional mqtt username>
password: <optional mqtt password>
# retain: false
# qos: 0
globals:
# every location will be managed using a unique mqtt topic
# unless explicitly specified, this format will be used
Expand Down
4 changes: 4 additions & 0 deletions data/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ knobs:
mqtt:
# ip/dns for the mqtt broker
host: 192.168.1.250
# uncomment to publish with retain
# retain: true
# specify qos value (default is 0)
# qos: 1
globals:
# every location will be managed using a unique mqtt topic
# unless explicitly specified, this format will be used
Expand Down
14 changes: 14 additions & 0 deletions mqtt2kasa/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ def mqtt_password(self):
return attr.get("password", None)
return None

@property
def mqtt_retain(self):
attr = self._get_info().mqtt
if isinstance(attr, collections.abc.Mapping):
return attr.get("retain", False)
return False

@property
def mqtt_qos(self):
attr = self._get_info().mqtt
if isinstance(attr, collections.abc.Mapping):
return attr.get("qos", 0)
return 0

@property
def reconnect_interval(self):
attr = self._get_info().mqtt
Expand Down
11 changes: 10 additions & 1 deletion mqtt2kasa/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import asyncio

from mqtt2kasa import log
from mqtt2kasa.config import Cfg
from mqtt2kasa.events import MqttMsgEvent

logger = log.getLogger()


async def handle_mqtt_publish(client, mqtt_send_q: asyncio.Queue):
c = Cfg()
mqtt_qos = c.mqtt_qos
mqtt_retain = c.mqtt_retain
logger.info(
f"handle_mqtt_publish task started. Using retain:{mqtt_retain} and qos:{mqtt_qos}"
)
while True:
mqtt_msg = await mqtt_send_q.get()
topic, payload = mqtt_msg.topic, mqtt_msg.payload
# logger.debug(f"Publishing: {topic} {payload}")
try:
await client.publish(topic, payload, timeout=15)
await client.publish(
topic, payload, timeout=15, qos=mqtt_qos, retain=mqtt_retain
)
logger.debug(f"Published: {topic} {payload}")
except Exception as e:
logger.error("client failed publish mqtt %s %s : %s", topic, payload, e)
Expand Down

0 comments on commit 9269fba

Please sign in to comment.