-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttconsumer.py
53 lines (42 loc) · 1.33 KB
/
mqttconsumer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import annotations
from abc import ABC, abstractmethod
from loguru import logger
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from settings import Settings
from main import Linux2MQTT
class MQTTConsumer(ABC):
def __init__(self, config: Settings, runtime: Linux2MQTT):
self.availability_topics = []
@abstractmethod
def on_connect(self, mqtt_client):
"""
Called when the MQTT client connects to the server.
Use this to subscribe to topics and publish initial values.
"""
pass
@abstractmethod
def on_disconnect(self, mqtt_client):
"""
Called before the MQTT client disconnects from the server.
Use this to publish empty values to topics that will become stale.
"""
pass
@abstractmethod
def update_mqtt(self, mqtt_client):
"""
Called in regular intervals to update the status of managed topics.
"""
pass
def connected(self, mqtt_client):
"""
Called when the MQTT client connects to the server.
DO NOT OVERRIDE
"""
self.on_connect(mqtt_client)
def disconnect(self, mqtt_client):
"""
Called before the MQTT client disconnects from the server.
DO NOT OVERRIDE
"""
self.on_disconnect(mqtt_client)