Added support for JSON report instead of properties.

This commit is contained in:
Your Name
2018-02-16 11:08:17 +03:00
parent 076e70de6b
commit 8b4ea2a089
4 changed files with 21 additions and 13 deletions
-4
View File
@@ -25,8 +25,6 @@ jobs:
# push image # push image
- > - >
if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
docker images
echo $IMAGE_NAME
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
#1.0.0-arch #1.0.0-arch
@@ -57,8 +55,6 @@ jobs:
# push image # push image
- > - >
if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
docker images
echo $IMAGE_NAME
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
#1.0.0-arch #1.0.0-arch
+2
View File
@@ -4,6 +4,8 @@ mqtt:
username: username username: username
password: passw0rd password: passw0rd
prefix: home prefix: home
#send report as json
json: False
gateway: gateway:
password: passw0rd password: passw0rd
-1
View File
@@ -1,6 +1,5 @@
aqara: aqara:
image: monster1025/aqara-mqtt:1-armhf image: monster1025/aqara-mqtt:1-armhf
dockerfile: Dockerfile-armhf
container_name: aqara container_name: aqara
volumes: volumes:
- "./config:/app/config" - "./config:/app/config"
+19 -8
View File
@@ -8,6 +8,7 @@ import json
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
class Mqtt: class Mqtt:
event_based_sensors = ["switch", "cube"]
username = "" username = ""
password = "" password = ""
server = "localhost" server = "localhost"
@@ -38,6 +39,7 @@ class Mqtt:
self.server = mqttConfig.get("server", "localhost") self.server = mqttConfig.get("server", "localhost")
self.port = mqttConfig.get("port", 1883) self.port = mqttConfig.get("port", 1883)
self.prefix = mqttConfig.get("prefix", "home") self.prefix = mqttConfig.get("prefix", "home")
self.json = mqttConfig.get("json", False)
self._queue = Queue() self._queue = Queue()
self._threads = [] self._threads = []
@@ -72,8 +74,6 @@ class Mqtt:
model = sidprops.get("model",model) model = sidprops.get("model",model)
sid = sidprops.get("name",sid) sid = sidprops.get("name",sid)
# _LOGGER.info("data is " + format(data))
PATH_FMT = self.prefix + "/{model}/{sid}/{prop}"
for key, value in data.items(): for key, value in data.items():
# fix for latest motion value # fix for latest motion value
if (model == "motion" and key == "no_motion"): if (model == "motion" and key == "no_motion"):
@@ -82,18 +82,29 @@ class Mqtt:
if (model == "magnet" and key == "no_close"): if (model == "magnet" and key == "no_close"):
key="status" key="status"
value="open" value="open"
# do not retain event-based sensors (like switches and cubes). # do not retain event-based sensors (like switches and cubes).
if (model in ["switch", "cube"]): if (model in self.event_based_sensors):
retain = False retain = False
# fix for rgb format # fix for rgb format
if (key == "rgb" and self._is_int(value)): if (key == "rgb" and self._is_int(value)):
value = self._color_xiaomi_to_rgb(value) value = self._color_xiaomi_to_rgb(value)
topic = PATH_FMT.format(model=model, sid=sid, prop=key) if self.json == True:
_LOGGER.info("Publishing message to topic " + topic + ": " + str(value) + ".") PATH_FMT = self.prefix + "/{model}/{sid}/json"
self._client.publish(topic, payload=value, qos=0, retain=retain) topic = PATH_FMT.format(model=model, sid=sid)
values = {}
values['sid'] = sid
for key, value in data.items():
values[key] = value
jsondata = json.dumps(values)
_LOGGER.info("Publishing message to topic " + topic + ": " + str(jsondata) + ".")
self._client.publish(topic, payload=jsondata, qos=0, retain=retain)
else:
for key, value in data.items():
PATH_FMT = self.prefix + "/{model}/{sid}/{prop}"
topic = PATH_FMT.format(model=model, sid=sid, prop=key)
_LOGGER.info("Publishing message to topic " + topic + ": " + str(value) + ".")
self._client.publish(topic, payload=value, qos=0, retain=retain)
def _mqtt_on_connect(self, client, userdata, rc, unk): def _mqtt_on_connect(self, client, userdata, rc, unk):
_LOGGER.info("Connected to mqtt server.") _LOGGER.info("Connected to mqtt server.")