forked from Arksine/moonraker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mqtt: implements last will and testament
Signed-off-by: Pedro Lamas <[email protected]>
- Loading branch information
1 parent
ca27c2c
commit bed239c
Showing
1 changed file
with
10 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
# | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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) | ||
|
@@ -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: | ||
|