Skip to content

Commit 733cc04

Browse files
committed
Add float support from MQTT messages
Up to now, only integer values can be retrieved from MQTT messages. This commit add support for float values for both JSON and non-JSON MQTT payloads.
1 parent 11af5ae commit 733cc04

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

enoceanmqtt/communicator.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,29 @@ def _mqtt_message_normal(self, msg):
148148
self._send_message(cur_sensor, clear)
149149

150150
else:
151-
found_topic = True
152151
# parse message content
153152
value = None
154153
try:
155154
value = int(msg.payload)
156155
except ValueError:
157-
logging.warning("Cannot parse int value for %s: %s", msg.topic, msg.payload)
158-
# Prevent storing undefined value, as it will trigger exception in EnOcean library
159-
return
156+
try:
157+
value = float(msg.payload)
158+
except ValueError:
159+
logging.warning("Cannot parse int nor float value for %s: %s",
160+
msg.topic, msg.payload)
161+
# Prevent storing undefined value, as it will
162+
# trigger exception in EnOcean library
163+
break
160164
# store received data
161165
logging.debug("%s: %s=%s", cur_sensor['name'], prop, value)
166+
found_topic = True
162167
if 'data' not in cur_sensor:
163168
cur_sensor['data'] = {}
164169
cur_sensor['data'][prop] = value
165170

171+
# The targeted sensor has been found and the MQTT message has been handled
172+
break
173+
166174
return found_topic
167175

168176
def _mqtt_message_json(self, mqtt_topic, mqtt_json_payload):
@@ -189,13 +197,25 @@ def _mqtt_message_json(self, mqtt_topic, mqtt_json_payload):
189197
del mqtt_json_payload['send']
190198

191199
# Parse message content
192-
for topic in mqtt_json_payload:
193-
try:
194-
mqtt_json_payload[topic] = int(mqtt_json_payload[topic])
195-
except ValueError:
196-
logging.warning("Cannot parse int value for %s: %s", topic, mqtt_json_payload[topic])
197-
# Prevent storing undefined value, as it will trigger exception in EnOcean library
200+
for topic in tuple(mqtt_json_payload.keys()):
201+
if not isinstance(mqtt_json_payload[topic], (int,float,str)):
202+
logging.warning("Cannot parse int nor float value for %s: %s",
203+
topic, mqtt_json_payload[topic])
204+
# Prevent storing undefined value, as it will
205+
# trigger exception in EnOcean library
198206
del mqtt_json_payload[topic]
207+
elif isinstance(mqtt_json_payload[topic], str):
208+
try:
209+
mqtt_json_payload[topic] = int(mqtt_json_payload[topic])
210+
except ValueError:
211+
try:
212+
mqtt_json_payload[topic] = float(mqtt_json_payload[topic])
213+
except ValueError:
214+
logging.warning("Cannot parse int nor float value for %s: %s",
215+
topic, mqtt_json_payload[topic])
216+
# Prevent storing undefined value, as it will
217+
# trigger exception in EnOcean library
218+
del mqtt_json_payload[topic]
199219

200220
# Append received data to cur_sensor['data'].
201221
# This will keep the possibility to pass single topic/payload as done with
@@ -207,7 +227,7 @@ def _mqtt_message_json(self, mqtt_topic, mqtt_json_payload):
207227
cur_sensor['data'].update(mqtt_json_payload)
208228

209229
# Finally, send the message
210-
if send == True:
230+
if send:
211231
self._send_message(cur_sensor, clear)
212232

213233
# The targeted sensor has been found and the MQTT message has been handled
@@ -242,7 +262,7 @@ def _send_message(self, sensor, clear):
242262
self._send_packet(sensor, destination, command)
243263

244264
# Clear sent data, if requested by the sent message
245-
if clear == True:
265+
if clear:
246266
logging.debug('Clearing data buffer.')
247267
del sensor['data']
248268

@@ -261,11 +281,11 @@ def _get_command_id(self, packet, sensor):
261281
if profile:
262282
# Loop over profile contents
263283
for source in profile.contents:
264-
if not source.name:
265-
continue
266-
# Check the current shortcut matches the command shortcut
267-
if source['shortcut'] == sensor.get('command'):
268-
return packet.eep._get_raw(source, packet._bit_data)
284+
if not source.name:
285+
continue
286+
# Check the current shortcut matches the command shortcut
287+
if source['shortcut'] == sensor.get('command'):
288+
return packet.eep._get_raw(source, packet._bit_data)
269289

270290
# If profile or command shortcut not found,
271291
# return None for default handling of the packet

0 commit comments

Comments
 (0)