Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mqtt(still working on it) #162

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions amazon_dash/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
47 changes: 47 additions & 0 deletions amazon_dash/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys

import copy
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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion amazon_dash/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +20,7 @@
'homeassistant': ExecuteHomeAssistant,
'openhab': ExecuteOpenHab,
'ifttt': ExecuteIFTTT,
'mqtt': ExecuteMQTT,
}
"""
Execute classes registered.
Expand Down