Support parsing mqtt messages via jsonpath
This commit is contained in:
1 parent
d25f58e650
commit
b4cf0e874a
4 files changed
+35
-5
No files matched your search
@@ -9,6 +9,8 @@ https://home-assistant.io/components/mqtt/
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import json
|
||||
import jsonpath_rw
|
||||
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import homeassistant.util as util
|
||||
@@ -127,6 +129,15 @@ def setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
class JsonFmtParser(object):
|
||||
""" Implements a json parser on xpath"""
|
||||
def __init__(self, jsonpath):
|
||||
self._expr = jsonpath_rw.parse(jsonpath)
|
||||
def __call__(self, payload):
|
||||
match = self._expr.find(json.loads(payload))
|
||||
return match[0].value if len(match) > 0 else None
|
||||
|
||||
|
||||
# This is based on one of the paho-mqtt examples:
|
||||
# http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/tree/examples/sub-class.py
|
||||
# pylint: disable=too-many-arguments
|
||||
|
||||
Reference in new issue
Block a user