Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .Client import Client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
paho-mqtt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
) |
e0927e9
There was a problem hiding this comment.
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.
e0927e9
There was a problem hiding this comment.
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
e0927e9
There was a problem hiding this comment.
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).
e0927e9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://developer.telldus.com/blog/2017/04/04/testers-wanted-for-tellstick-plugins