embyt / enocean-mqtt

Receives messages from an enOcean serial interface (USB) and provides selected messages to an MQTT broker.
GNU General Public License v3.0
42 stars 21 forks source link

Bug correction and proposal for full JSON support #34

Closed mak-gitdev closed 2 years ago

mak-gitdev commented 2 years ago

*Bug: Correction of a bug I introduced with my previous commit in function _handle_data_packet(). channel_id and channel_value are returned but where not always defined.

*JSON: Up to now, JSON format can be enabled only on a global basis and is only available for MQTT messages from EnOcean to the MQTT broker. With this commit, I propose to enable JSON on a per-device basis and to make JSON also available for MQTT messages from MQTT broker to EnOcean. MQTT messages from EnOcean are published using JSON format if flag 'publish_json' is set to True, true or 1 in the device configuration. For messages from the MQTT broker to EnOcean, enoceanmqtt can now automatically detect the payload format (JSON or not) and handle it accordingly.

romor commented 2 years ago

Thanks for the PR. Just a short comment from my side: I think we could reduce the number of invocations for json decoding (mqtt_json_payload = json.loads(msg.payload)) if we do this outside the loop or even get it already as an argument to _mqtt_message_json()?

mak-gitdev commented 2 years ago

Hello, I just committed a new version taking into account your comments. Now json.loads() is called only once in _on_mqtt_message().

But just to clarify, it is my understanding that one topic targets one and only one sensor. That's why in this PR, I also added break statement in the for loops. So json.loads() was called a first time in _on_mqtt_message() to determine the payload type and a second time in _mqtt_message_json(), only once, when the targeted sensor is found.