diff --git a/mqtt-client/mqtt.png b/mqtt-client/mqtt.png new file mode 100644 index 0000000..0de7429 Binary files /dev/null and b/mqtt-client/mqtt.png differ diff --git a/mqtt-client/mqtt_client/Client.py b/mqtt-client/mqtt_client/Client.py new file mode 100644 index 0000000..134add5 --- /dev/null +++ b/mqtt-client/mqtt_client/Client.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- + +import json + +from base import \ + Plugin, \ + configuration, \ + ConfigurationNumber, \ + ConfigurationString, \ + implements, \ + ISignalObserver, \ + slot +import paho.mqtt.client as mqtt + +__name__ = 'MQTT' # pylint: disable=W0622 + +# On mips 0.1 might be represented as 0.10000000000000001. This is a workaround. +class FloatWrapper(float): + def __repr__(self): + return '%.15g' % self + +@configuration( + username=ConfigurationString( + defaultValue='', + title='Username', + ), + password=ConfigurationString( + defaultValue='', + title='Password', + ), + hostname=ConfigurationString( + defaultValue='', + title='Hostname', + ), + port=ConfigurationNumber( + defaultValue=1883, + title='Port', + ), +) +class Client(Plugin): + implements(ISignalObserver) + + def __init__(self): + self.client = mqtt.Client() + self.client.on_connect = self.onConnect + self.client.on_message = self.onMessage + self.client.on_publish = self.onPublish + self.client.on_subscribe = self.onSubscribe + if self.config('hostname') != '': + self.connect() + + def configWasUpdated(self, key, __value): + if key == 'hostname': + self.connect() + + def connect(self): + if self.config('username') != '': + self.client.username_pw_set(self.config('username'), self.config('password')) + self.client.connect_async(self.config('hostname'), self.config('port')) + self.client.loop_start() + + @slot('deviceStateChanged') + def onDeviceStateChanged(self, device, state, stateValue, origin=None): + del origin + self.client.publish('telldus/device/%s/state' % (device.id()), json.dumps({ + 'state': state, + 'stateValue': stateValue, + #'origin': origin, + })) + + def onConnect(self, client, userdata, flags, result): + pass + + def onMessage(self, client, userdata, msg): + pass + + def onPublish(self, client, obj, mid): + pass + + @slot('sensorValueUpdated') + def onSensorValueUpdated(self, device, valueType, value, scale): + self.client.publish('telldus/sensor/%s/value' % (device.id()), json.dumps({ + 'value': FloatWrapper(value), + 'valueType': valueType, + 'scale': scale, + })) + + def onSubscribe(self, client, obj, mid, granted_qos): + pass diff --git a/mqtt-client/mqtt_client/__init__.py b/mqtt-client/mqtt_client/__init__.py new file mode 100644 index 0000000..eb334d1 --- /dev/null +++ b/mqtt-client/mqtt_client/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from .Client import Client diff --git a/mqtt-client/requirements.txt b/mqtt-client/requirements.txt new file mode 100644 index 0000000..8579e8b --- /dev/null +++ b/mqtt-client/requirements.txt @@ -0,0 +1 @@ +paho-mqtt diff --git a/mqtt-client/setup.py b/mqtt-client/setup.py new file mode 100644 index 0000000..25f4b56 --- /dev/null +++ b/mqtt-client/setup.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +try: + from setuptools import setup +except ImportError: + from distutils.core import setup + +setup( + name='MQTT Client', + version='1.0', + description='Plugin to connect to a MQTT broker', + icon='mqtt.png', + color='#660066', + author='Telldus Technologies', + author_email='info.tech@telldus.se', + category='notifications', + packages=['mqtt_client'], + entry_points={ \ + 'telldus.startup': ['c = mqtt_client:Client [cREQ]'] + }, + extras_require=dict(cREQ='Base>=0.1\nTelldus>=0.1'), +)