@@ -103,23 +103,26 @@ def _mqtt_disconnect(self, client, userdata, rc):
103
103
self .on_disconnect (self )
104
104
105
105
def _mqtt_message (self , client , userdata , msg ):
106
- logger .debug ('Client on_message called.' )
107
106
"""Parse out the topic and call on_message callback
108
107
assume topic looks like `username/topic/id`
109
108
"""
109
+ logger .debug ('Client on_message called.' )
110
110
parsed_topic = msg .topic .split ('/' )
111
- if self .on_message is not None and parsed_topic [2 ] == 'weather' :
112
- topic = parsed_topic [4 ] # parse out the forecast type
113
- payload = '' if msg .payload is None else msg .payload .decode ('utf-8' )
114
- elif self .on_message is not None and parsed_topic [0 ] == 'time' :
115
- topic = parsed_topic [0 ]
116
- payload = msg .payload .decode ('utf-8' )
117
- elif self .on_message is not None and parsed_topic [1 ] == 'groups' :
118
- topic = parsed_topic [3 ]
119
- payload = msg .payload .decode ('utf-8' )
120
- else : # default topic
121
- topic = parsed_topic [2 ]
122
- payload = '' if msg .payload is None else msg .payload .decode ('utf-8' )
111
+ if self .on_message is not None :
112
+ if parsed_topic [0 ] == 'time' :
113
+ topic = parsed_topic [0 ]
114
+ payload = msg .payload .decode ('utf-8' )
115
+ elif parsed_topic [1 ] == 'groups' :
116
+ topic = parsed_topic [3 ]
117
+ payload = msg .payload .decode ('utf-8' )
118
+ elif parsed_topic [2 ] == 'weather' :
119
+ topic = parsed_topic [4 ]
120
+ payload = '' if msg .payload is None else msg .payload .decode ('utf-8' )
121
+ else :
122
+ topic = parsed_topic [2 ]
123
+ payload = '' if msg .payload is None else msg .payload .decode ('utf-8' )
124
+ else :
125
+ raise ValueError ('on_message not defined' )
123
126
self .on_message (self , topic , payload )
124
127
125
128
def _mqtt_subscribe (client , userdata , mid , granted_qos ):
0 commit comments