diff options
Diffstat (limited to 'src/home/mqtt/mqtt.py')
-rw-r--r-- | src/home/mqtt/mqtt.py | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/home/mqtt/mqtt.py b/src/home/mqtt/mqtt.py index 4acd4f6..fad5d26 100644 --- a/src/home/mqtt/mqtt.py +++ b/src/home/mqtt/mqtt.py @@ -3,8 +3,8 @@ import paho.mqtt.client as mqtt import ssl import logging -from typing import Tuple from ..config import config +from ._payload import * def username_and_password() -> Tuple[str, str]: @@ -14,6 +14,8 @@ def username_and_password() -> Tuple[str, str]: class MqttBase: + _connected: bool + def __init__(self, clean_session=True): self._client = mqtt.Client(client_id=config['mqtt']['client_id'], protocol=mqtt.MQTTv311, @@ -24,6 +26,7 @@ class MqttBase: self._client.on_log = self.on_log self._client.on_publish = self.on_publish self._loop_started = False + self._connected = False self._logger = logging.getLogger(self.__class__.__name__) @@ -41,7 +44,9 @@ class MqttBase: 'assets', 'mqtt_ca.crt' )) - self._client.tls_set(ca_certs=ca_certs, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2) + self._client.tls_set(ca_certs=ca_certs, + cert_reqs=ssl.CERT_REQUIRED, + tls_version=ssl.PROTOCOL_TLSv1_2) def connect_and_loop(self, loop_forever=True): host = config['mqtt']['host'] @@ -61,9 +66,11 @@ class MqttBase: def on_connect(self, client: mqtt.Client, userdata, flags, rc): self._logger.info("Connected with result code " + str(rc)) + self._connected = True def on_disconnect(self, client: mqtt.Client, userdata, rc): self._logger.info("Disconnected with result code " + str(rc)) + self._connected = False def on_log(self, client: mqtt.Client, userdata, level, buf): level = mqtt.LOGGING_LEVEL[level] if level in mqtt.LOGGING_LEVEL else logging.INFO @@ -73,4 +80,15 @@ class MqttBase: self._logger.debug(msg.topic + ": " + str(msg.payload)) def on_publish(self, client: mqtt.Client, userdata, mid): - self._logger.debug(f'publish done, mid={mid}')
\ No newline at end of file + self._logger.debug(f'publish done, mid={mid}') + + +class MqttEspDevice: + id: str + secret: Optional[str] + + def __init__(self, + node_id: str, + secret: Optional[str] = None): + self.id = node_id + self.secret = secret |