diff --git a/handler/event.py b/handler/event.py index 5aea87b..36651dd 100644 --- a/handler/event.py +++ b/handler/event.py @@ -51,9 +51,13 @@ def __init__(self): self.processing_worker.daemon = True self.processing_worker.start() - def on_mqtt_connected(self, client): - self.logger.debug("mqtt connected callback has been invoked") + def on_mqtt_init_done(self, client): + self.logger.debug("mqtt object is available") self.mqtt_client = client + + def on_mqtt_connected(self): + self.logger.debug("mqtt connected callback has been invoked") + # insert + as identifier so that every command to every identifier (= friendly names) will be recognized self.mqtt_client.subscribe(TOPIC_COMMAND_VOLUME_LEVEL % "+") self.mqtt_client.subscribe(TOPIC_COMMAND_VOLUME_MUTED % "+") diff --git a/helper/mqtt.py b/helper/mqtt.py index 6cb0c1a..fc6eafd 100644 --- a/helper/mqtt.py +++ b/helper/mqtt.py @@ -4,7 +4,13 @@ class MqttConnectionCallback: - def on_mqtt_connected(self, client): + def on_mqtt_init_done(self, client): + """ + Called if there is a mqtt connection class available (but not necessarily connected yet) + """ + pass + + def on_mqtt_connected(self): pass def on_mqtt_message_received(self, topic, payload): @@ -33,6 +39,8 @@ def __init__(self, ip, port, username, password, cafile, connection_callback): self.connection_callback = connection_callback self.queue = [] + self.connection_callback.on_mqtt_init_done(self) + def _on_connect(self, client, userdata, flags, rc): """ The callback for when the client receives a CONNACK response from the server. @@ -42,7 +50,7 @@ def _on_connect(self, client, userdata, flags, rc): # subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. - self.connection_callback.on_mqtt_connected(self) + self.connection_callback.on_mqtt_connected() if len(self.queue) > 0: self.logger.debug("found %d queued messages" % len(self.queue))