diff options
Diffstat (limited to 'src/home/mqtt/mqtt.py')
-rw-r--r-- | src/home/mqtt/mqtt.py | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/src/home/mqtt/mqtt.py b/src/home/mqtt/mqtt.py index b360d22..b3334b5 100644 --- a/src/home/mqtt/mqtt.py +++ b/src/home/mqtt/mqtt.py @@ -17,18 +17,18 @@ def username_and_password() -> Tuple[str, str]: class MQTTBase: def __init__(self, clean_session=True): - self.client = mqtt.Client(client_id=config['mqtt']['client_id'], - protocol=mqtt.MQTTv311, - clean_session=clean_session) - self.client.on_connect = self.on_connect - self.client.on_disconnect = self.on_disconnect - self.client.on_message = self.on_message + self._client = mqtt.Client(client_id=config['mqtt']['client_id'], + protocol=mqtt.MQTTv311, + clean_session=clean_session) + self._client.on_connect = self.on_connect + self._client.on_disconnect = self.on_disconnect + self._client.on_message = self.on_message - self.home_id = 1 + self._logger = logging.getLogger(self.__class__.__name__) username, password = username_and_password() if username and password: - self.client.username_pw_set(username, password) + self._client.username_pw_set(username, password) def configure_tls(self): ca_certs = os.path.realpath(os.path.join( @@ -39,23 +39,23 @@ 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'] port = config['mqtt']['port'] - self.client.connect(host, port, 60) + self._client.connect(host, port, 60) if loop_forever: - self.client.loop_forever() + self._client.loop_forever() else: - self.client.loop_start() + self._client.loop_start() def on_connect(self, client: mqtt.Client, userdata, flags, rc): - logger.info("Connected with result code " + str(rc)) + self._logger.info("Connected with result code " + str(rc)) def on_disconnect(self, client: mqtt.Client, userdata, rc): - logger.info("Disconnected with result code " + str(rc)) + self._logger.info("Disconnected with result code " + str(rc)) def on_message(self, client: mqtt.Client, userdata, msg): - logger.info(msg.topic + ": " + str(msg.payload)) + self._logger.info(msg.topic + ": " + str(msg.payload)) |