Skip to content

Commit

Permalink
mqtt: implements last will and testament
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas authored and Arksine committed Nov 13, 2021
1 parent ca27c2c commit bed239c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion moonraker/components/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MQTT client implemenation for Moonraker
# MQTT client implementation for Moonraker
#
# Copyright (C) 2021 Eric Callahan <[email protected]>
#
Expand Down Expand Up @@ -191,6 +191,7 @@ def __init__(self, config: ConfigHelper) -> None:
self.api_request_topic = f"{self.instance_name}/moonraker/api/request"
self.api_resp_topic = f"{self.instance_name}/moonraker/api/response"
self.klipper_status_topic = f"{self.instance_name}/klipper/status"
self.moonraker_status_topic = f"{self.instance_name}/moonraker/status"
status_cfg = config.get("status_objects", None)
self.status_objs: Dict[str, Any] = {}
if status_cfg is not None:
Expand Down Expand Up @@ -235,6 +236,9 @@ async def component_init(self) -> None:
self.helper = AIOHelper(self.client)
if self.user_name is not None:
self.client.username_pw_set(self.user_name, self.password)
self.client.will_set(self.moonraker_status_topic,
payload=json.dumps({'server': 'offline'}),
qos=self.qos, retain=True)
retries = 5
for _ in range(retries):
try:
Expand Down Expand Up @@ -288,6 +292,8 @@ def _on_connect(self,
) -> None:
logging.info("MQTT Client Connected")
if reason_code == 0:
self.publish_topic(self.moonraker_status_topic,
{'server': 'online'}, retain=True)
subs = [(k, v[0]) for k, v in self.subscribed_topics.items()]
if subs:
res, msg_id = client.subscribe(subs)
Expand Down Expand Up @@ -619,6 +625,9 @@ async def close(self) -> None:
self.reconnect_task = None
if not self.is_connected():
return
await self.publish_topic(self.moonraker_status_topic,
{'server': 'offline'},
retain=True)
self.disconnect_evt = asyncio.Event()
self.client.disconnect()
try:
Expand Down

0 comments on commit bed239c

Please sign in to comment.