@@ -103,23 +103,26 @@ def _mqtt_disconnect(self, client, userdata, rc):
103103 self .on_disconnect (self )
104104
105105 def _mqtt_message (self , client , userdata , msg ):
106- logger .debug ('Client on_message called.' )
107106 """Parse out the topic and call on_message callback
108107 assume topic looks like `username/topic/id`
109108 """
109+ logger .debug ('Client on_message called.' )
110110 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' )
123126 self .on_message (self , topic , payload )
124127
125128 def _mqtt_subscribe (client , userdata , mid , granted_qos ):
0 commit comments