Skip to content

Commit

Permalink
#9 fix exception when finding chromecast devices before being connect…
Browse files Browse the repository at this point in the history
…ed to mqtt
  • Loading branch information
nohum committed Aug 20, 2017
1 parent bb55a47 commit 4534f20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 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
16 changes: 12 additions & 4 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 All @@ -28,6 +34,8 @@ def __init__(self, ip, port, username, password, 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 @@ -36,7 +44,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 Expand Up @@ -80,11 +88,11 @@ def _internal_send_message(self, topic, payload, queue):
self.logger.debug("sending topic %s with value \"%s\"" % (topic, payload))
result = self.mqtt.publish(topic, payload, retain=True)

if result == MQTT_ERR_NO_CONN and queue:
if result[0] == MQTT_ERR_NO_CONN and queue:
self.logger.debug("no connection, saving message with topic %s to queue" % topic)
self.queue.append([topic, payload])
elif result[0] != MQTT_ERR_SUCCESS:
self.logger.warn("failed sending message %s, mqtt error %s" % (topic, result))
self.logger.warning("failed sending message %s, mqtt error %s" % (topic, result))
return False

return True
Expand Down

0 comments on commit 4534f20

Please sign in to comment.