From 1b6d111ef87dfa611d7736539cf67cbc0479b398 Mon Sep 17 00:00:00 2001 From: "Tsuyoshi, Mizuno" Date: Wed, 26 Aug 2020 16:10:08 +0900 Subject: [PATCH 1/2] Add mqtt --- amazon_dash/config.py | 15 +++++++++++++ amazon_dash/execute.py | 47 +++++++++++++++++++++++++++++++++++++++++ amazon_dash/listener.py | 3 ++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/amazon_dash/config.py b/amazon_dash/config.py index 5b99d01..e201485 100644 --- a/amazon_dash/config.py +++ b/amazon_dash/config.py @@ -88,6 +88,21 @@ "ifttt": { "type": "string" }, + "mqtt": { + "type": "string" + }, + "mqttport": { + "type": "string" + }, + "clientid": { + "type": "string" + }, + "topic": { + "type": "string" + }, + "message": { + "type": "string" + }, "event": { "type": "string" }, diff --git a/amazon_dash/execute.py b/amazon_dash/execute.py index 610dc8b..075baf3 100644 --- a/amazon_dash/execute.py +++ b/amazon_dash/execute.py @@ -6,6 +6,7 @@ import sys import copy +import paho.mqtt.client as mqtt # pip install paho-mqtt from requests import request, RequestException from amazon_dash._compat import JSONDecodeError from amazon_dash.exceptions import SecurityException, InvalidConfig, ExecuteError @@ -171,6 +172,52 @@ def execute(self, root_allowed=False): if output: return output[0] +class ExecuteMQTT(Execute): + """Publish a mqtt + """ + mqtt_client = mqtt.Client(protocol=mqtt.MQTTv311) + default_host = 'localhost' #: default broker host + default_port = 1883 #: default MQTT port + default_clientID = 'amazon-dash' #: default content type to send + + def __init__(self, name, data): + """ + + :param str name: name or mac address + :param data: data on device section + """ + super(ExecuteMQTT, self).__init__(name, data) + self.host = data.get('mqtt', default_host) + self.port = data.get('mqttport', default_host) + self.clientID = data.get('clientid', default_clientID) + + def validate(self): + """Check self.data. Raise InvalidConfig on error + + :return: None + """ + try: + mqtt_client.connect(self.host, self.port, keepalive=60) + mqtt_client.disconnect() + except: + raise InvalidConfig( + extra_body='Invalid MQTT broker on {} device.'.format(self.name) + ) + + def execute(self, root_allowed=False): + + topic = self.data.get('topic', None) + message = self.data.get('message', None) + + if (topic is not None) and (message is not None): + try: + mqtt_client.connect(self.host, self.port, keepalive=60) + mqtt_client.publish(topic, message) + mqtt_client.disconnect() + except: + raise ExecuteError('Exception on publish to {}: {}'.format(self.host, e)) + + class ExecuteUrl(Execute): """Call a url diff --git a/amazon_dash/listener.py b/amazon_dash/listener.py index 7867dcb..272eb37 100644 --- a/amazon_dash/listener.py +++ b/amazon_dash/listener.py @@ -6,7 +6,7 @@ from amazon_dash.config import Config from amazon_dash.confirmations import get_confirmation from amazon_dash.exceptions import InvalidConfig, InvalidDevice -from amazon_dash.execute import logger, ExecuteCmd, ExecuteUrl, ExecuteHomeAssistant, ExecuteOpenHab, ExecuteIFTTT +from amazon_dash.execute import logger, ExecuteCmd, ExecuteUrl, ExecuteHomeAssistant, ExecuteOpenHab, ExecuteIFTTT, ExecuteMQTT from amazon_dash.scan import scan_devices DEFAULT_DELAY = 10 @@ -20,6 +20,7 @@ 'homeassistant': ExecuteHomeAssistant, 'openhab': ExecuteOpenHab, 'ifttt': ExecuteIFTTT, + 'mqtt': ExecuteMQTT, } """ Execute classes registered. From 513dba60888da21f379ce4200d3feb01072ee174 Mon Sep 17 00:00:00 2001 From: "Tsuyoshi, Mizuno" Date: Wed, 26 Aug 2020 16:30:04 +0900 Subject: [PATCH 2/2] delete unnecessary comment --- amazon_dash/execute.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/amazon_dash/execute.py b/amazon_dash/execute.py index 075baf3..8d78e7a 100644 --- a/amazon_dash/execute.py +++ b/amazon_dash/execute.py @@ -6,7 +6,7 @@ import sys import copy -import paho.mqtt.client as mqtt # pip install paho-mqtt +import paho.mqtt.client as mqtt from requests import request, RequestException from amazon_dash._compat import JSONDecodeError from amazon_dash.exceptions import SecurityException, InvalidConfig, ExecuteError