Skip to content

Commit

Permalink
Add MQTT client plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeprag committed Dec 1, 2017
1 parent b61bf01 commit e0927e9
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
Binary file added mqtt-client/mqtt.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions mqtt-client/mqtt_client/__init__.py
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from .Client import Client
1 change: 1 addition & 0 deletions mqtt-client/requirements.txt
@@ -0,0 +1 @@
paho-mqtt
23 changes: 23 additions & 0 deletions 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'),
)

4 comments on commit e0927e9

@fredrike
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, really impressive @mickeprag !

Too bad that it is not possible to enable this functionality on TellStick Lite devices.

@mickeprag
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand. Could you clarify?
This is for the TellStick ZNet family

@fredrike
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TellStick ZNet Lite does not seem to support Python plugins (according to http://tellstick-server.readthedocs.io/en/latest/python.html "Python plugins are only available for TellStick ZNet Pro. Python plugins cannot be run on TellStick ZNet Lite.") or is there something that can make the Lite version support python plugins (or atleast MQTT).

@mickeprag
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.