Skip to content

Commit

Permalink
nohum#9 fix exception when finding chromecast devices before being co…
Browse files Browse the repository at this point in the history
…nnected to mqtt

(cherry picked from commit 4534f20)
  • Loading branch information
nohum authored and rickynils committed Aug 23, 2021
1 parent 6af842e commit 1b09691
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions handler/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 % "+")
Expand Down
12 changes: 10 additions & 2 deletions helper/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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))
Expand Down

0 comments on commit 1b09691

Please sign in to comment.