Skip to content

Commit 3fd6c08

Browse files
committed
retain birth/keepalive message
1 parent 63cc251 commit 3fd6c08

File tree

3 files changed

+147
-29
lines changed

3 files changed

+147
-29
lines changed

src/publisher/core.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,61 @@ def is_connected(self) -> bool:
5555

5656
@abstractmethod
5757
def publish_json(
58-
self, key: str, data: dict[str, Any], no_prefix: bool = False
58+
self,
59+
key: str,
60+
data: dict[str, Any],
61+
no_prefix: bool = False,
62+
retain: bool = False,
63+
qos: int = 0,
5964
) -> None:
6065
raise NotImplementedError
6166

6267
@abstractmethod
63-
def publish_str(self, key: str, value: str, no_prefix: bool = False) -> None:
68+
def publish_str(
69+
self,
70+
key: str,
71+
value: str,
72+
no_prefix: bool = False,
73+
retain: bool = False,
74+
qos: int = 0,
75+
) -> None:
6476
raise NotImplementedError
6577

6678
@abstractmethod
67-
def publish_int(self, key: str, value: int, no_prefix: bool = False) -> None:
79+
def publish_int(
80+
self,
81+
key: str,
82+
value: int,
83+
no_prefix: bool = False,
84+
retain: bool = False,
85+
qos: int = 0,
86+
) -> None:
6887
raise NotImplementedError
6988

7089
@abstractmethod
71-
def publish_bool(self, key: str, value: bool, no_prefix: bool = False) -> None:
90+
def publish_bool(
91+
self,
92+
key: str,
93+
value: bool,
94+
no_prefix: bool = False,
95+
retain: bool = False,
96+
qos: int = 0,
97+
) -> None:
7298
raise NotImplementedError
7399

74100
@abstractmethod
75-
def publish_float(self, key: str, value: float, no_prefix: bool = False) -> None:
101+
def publish_float(
102+
self,
103+
key: str,
104+
value: float,
105+
no_prefix: bool = False,
106+
retain: bool = False,
107+
qos: int = 0,
108+
) -> None:
76109
raise NotImplementedError
77110

78111
@abstractmethod
79-
def clear_topic(self, key: str, no_prefix: bool = False) -> None:
112+
def clear_topic(self, key: str, no_prefix: bool = False, qos: int = 0) -> None:
80113
raise NotImplementedError
81114

82115
def get_mqtt_account_prefix(self) -> str:
@@ -154,7 +187,9 @@ def __anonymize(self, data: T) -> T:
154187
return data
155188

156189
def keepalive(self) -> None:
157-
self.publish_str(mqtt_topics.INTERNAL_LWT, "online", False)
190+
self.publish_str(
191+
mqtt_topics.INTERNAL_LWT, "online", no_prefix=False, retain=True, qos=1
192+
)
158193

159194
@staticmethod
160195
def anonymize_str(value: str) -> str:

src/publisher/log_publisher.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,62 @@ def enable_commands(self) -> None:
2424

2525
@override
2626
def publish_json(
27-
self, key: str, data: dict[str, Any], no_prefix: bool = False
27+
self,
28+
key: str,
29+
data: dict[str, Any],
30+
no_prefix: bool = False,
31+
retain: bool = False,
32+
qos: int = 0,
2833
) -> None:
2934
anonymized_json = self.dict_to_anonymized_json(data)
3035
self.internal_publish(key, anonymized_json)
3136

3237
@override
33-
def publish_str(self, key: str, value: str, no_prefix: bool = False) -> None:
38+
def publish_str(
39+
self,
40+
key: str,
41+
value: str,
42+
no_prefix: bool = False,
43+
retain: bool = False,
44+
qos: int = 0,
45+
) -> None:
3446
self.internal_publish(key, value)
3547

3648
@override
37-
def publish_int(self, key: str, value: int, no_prefix: bool = False) -> None:
49+
def publish_int(
50+
self,
51+
key: str,
52+
value: int,
53+
no_prefix: bool = False,
54+
retain: bool = False,
55+
qos: int = 0,
56+
) -> None:
3857
self.internal_publish(key, value)
3958

4059
@override
41-
def publish_bool(self, key: str, value: bool, no_prefix: bool = False) -> None:
60+
def publish_bool(
61+
self,
62+
key: str,
63+
value: bool,
64+
no_prefix: bool = False,
65+
retain: bool = False,
66+
qos: int = 0,
67+
) -> None:
4268
self.internal_publish(key, value)
4369

4470
@override
45-
def publish_float(self, key: str, value: float, no_prefix: bool = False) -> None:
71+
def publish_float(
72+
self,
73+
key: str,
74+
value: float,
75+
no_prefix: bool = False,
76+
retain: bool = False,
77+
qos: int = 0,
78+
) -> None:
4679
self.internal_publish(key, value)
4780

4881
@override
49-
def clear_topic(self, key: str, no_prefix: bool = False) -> None:
82+
def clear_topic(self, key: str, no_prefix: bool = False, qos: int = 0) -> None:
5083
self.internal_publish(key, None)
5184

5285
def internal_publish(self, key: str, value: Any) -> None:

src/publisher/mqtt_publisher.py

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,23 @@ async def __on_message_real(self, *, topic: str, payload: str) -> None:
220220
vin=vin, topic=topic, payload=payload
221221
)
222222

223-
def __publish(self, topic: str, payload: Any) -> None:
223+
def __publish(
224+
self, topic: str, payload: Any, retain: bool = False, qos: int = 0
225+
) -> None:
224226
LOG.debug("Publishing to MQTT topic %s with payload %s", topic, payload)
225227
loop = asyncio.get_running_loop()
226228
asyncio.run_coroutine_threadsafe(
227-
self.__async_publish(topic, payload, retain=True), loop
229+
self.__async_publish(topic, payload, retain=retain, qos=qos), loop
228230
)
229231

230-
async def __async_publish(self, topic: str, payload: Any, retain: bool) -> None:
232+
async def __async_publish(
233+
self, topic: str, payload: Any, retain: bool, qos: int
234+
) -> None:
231235
if not (self.client and self.is_connected()):
232236
LOG.error("Failed to publish: MQTT client is not connected")
233237
return
234238
try:
235-
await self.client.publish(topic, payload, retain)
239+
await self.client.publish(topic, payload, retain=retain, qos=qos)
236240
except aiomqtt.MqttError as e:
237241
LOG.error(
238242
f"Failed to publish to MQTT topic {topic} with payload {payload}: {e}"
@@ -244,30 +248,76 @@ def is_connected(self) -> bool:
244248

245249
@override
246250
def publish_json(
247-
self, key: str, data: dict[str, Any], no_prefix: bool = False
251+
self,
252+
key: str,
253+
data: dict[str, Any],
254+
no_prefix: bool = False,
255+
retain: bool = False,
256+
qos: int = 0,
248257
) -> None:
249258
payload = self.dict_to_anonymized_json(data)
250-
self.__publish(topic=self.get_topic(key, no_prefix), payload=payload)
259+
self.__publish(
260+
topic=self.get_topic(key, no_prefix),
261+
payload=payload,
262+
retain=retain,
263+
qos=qos,
264+
)
251265

252266
@override
253-
def publish_str(self, key: str, value: str, no_prefix: bool = False) -> None:
254-
self.__publish(topic=self.get_topic(key, no_prefix), payload=value)
267+
def publish_str(
268+
self,
269+
key: str,
270+
value: str,
271+
no_prefix: bool = False,
272+
retain: bool = False,
273+
qos: int = 0,
274+
) -> None:
275+
self.__publish(
276+
topic=self.get_topic(key, no_prefix), payload=value, retain=retain, qos=qos
277+
)
255278

256279
@override
257-
def publish_int(self, key: str, value: int, no_prefix: bool = False) -> None:
258-
self.__publish(topic=self.get_topic(key, no_prefix), payload=value)
280+
def publish_int(
281+
self,
282+
key: str,
283+
value: int,
284+
no_prefix: bool = False,
285+
retain: bool = False,
286+
qos: int = 0,
287+
) -> None:
288+
self.__publish(
289+
topic=self.get_topic(key, no_prefix), payload=value, retain=retain, qos=qos
290+
)
259291

260292
@override
261-
def publish_bool(self, key: str, value: bool, no_prefix: bool = False) -> None:
262-
self.__publish(topic=self.get_topic(key, no_prefix), payload=value)
293+
def publish_bool(
294+
self,
295+
key: str,
296+
value: bool,
297+
no_prefix: bool = False,
298+
retain: bool = False,
299+
qos: int = 0,
300+
) -> None:
301+
self.__publish(
302+
topic=self.get_topic(key, no_prefix), payload=value, retain=retain, qos=qos
303+
)
263304

264305
@override
265-
def publish_float(self, key: str, value: float, no_prefix: bool = False) -> None:
266-
self.__publish(topic=self.get_topic(key, no_prefix), payload=value)
306+
def publish_float(
307+
self,
308+
key: str,
309+
value: float,
310+
no_prefix: bool = False,
311+
retain: bool = False,
312+
qos: int = 0,
313+
) -> None:
314+
self.__publish(
315+
topic=self.get_topic(key, no_prefix), payload=value, retain=retain, qos=qos
316+
)
267317

268318
@override
269-
def clear_topic(self, key: str, no_prefix: bool = False) -> None:
270-
self.__publish(topic=self.get_topic(key, no_prefix), payload=None)
319+
def clear_topic(self, key: str, no_prefix: bool = False, qos: int = 0) -> None:
320+
self.__publish(topic=self.get_topic(key, no_prefix), payload=None, qos=qos)
271321

272322
def get_vin_from_topic(self, topic: str) -> str:
273323
global_topic_removed = topic[len(self.configuration.mqtt_topic) + 1 :]

0 commit comments

Comments
 (0)