mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
make some entries optional, update payload handling, and use qos
This commit is contained in:
parent
eac5b19309
commit
26dbb5ca3f
@ -16,9 +16,7 @@ mqtt:
|
|||||||
port: 1883
|
port: 1883
|
||||||
topic: home-assistant
|
topic: home-assistant
|
||||||
keepalive: 60
|
keepalive: 60
|
||||||
client_id: home-assistant
|
|
||||||
qos: 0
|
qos: 0
|
||||||
retain: 0
|
|
||||||
|
|
||||||
Variables:
|
Variables:
|
||||||
|
|
||||||
@ -27,28 +25,21 @@ broker
|
|||||||
This is the IP address of your MQTT broker, e.g. 192.168.1.32.
|
This is the IP address of your MQTT broker, e.g. 192.168.1.32.
|
||||||
|
|
||||||
port
|
port
|
||||||
*Required
|
*Optional
|
||||||
The network port to connect to, e.g. 1883.
|
The network port to connect to. Default is 1883.
|
||||||
|
|
||||||
topic
|
topic
|
||||||
*Required
|
*Required
|
||||||
The MQTT topic to subscribe to, e.g. home-assistant.
|
The MQTT topic to subscribe to, e.g. home-assistant.
|
||||||
|
|
||||||
keepalive
|
keepalive
|
||||||
*Required
|
*Optional
|
||||||
The keep alive in seconds for this client, e.g. 60.
|
The keep alive in seconds for this client, e.g. 60.
|
||||||
|
|
||||||
client_id
|
qos
|
||||||
*Required
|
*Optional
|
||||||
A name for this client, e.g. home-assistant.
|
Quality of service level to use for the subscription.
|
||||||
|
0, 1, or 2, defaults to 0.
|
||||||
qos: 0
|
|
||||||
*Required
|
|
||||||
Quality of service level to use for the subscription. 0, 1, or 2.
|
|
||||||
|
|
||||||
retain: 0
|
|
||||||
*Required
|
|
||||||
If message should be retained. 0 or 1.
|
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -66,20 +57,30 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
DOMAIN = "mqtt"
|
DOMAIN = "mqtt"
|
||||||
DEPENDENCIES = []
|
DEPENDENCIES = []
|
||||||
MQTT_CLIENT = None
|
MQTT_CLIENT = None
|
||||||
|
MQTT_CLIENT_ID = 'home-assistant'
|
||||||
|
MQTT_DEFAULT_PORT = 1883
|
||||||
|
MQTT_DEFAULT_KEEPALIVE = 60
|
||||||
|
MQTT_DEFAULT_QOS = 0
|
||||||
MQTT_SEND = 'mqtt_send'
|
MQTT_SEND = 'mqtt_send'
|
||||||
EVENT_MQTT_MESSAGE_RECEIVED = 'MQTT_MESSAGE_RECEIVED'
|
EVENT_MQTT_MESSAGE_RECEIVED = 'MQTT_MESSAGE_RECEIVED'
|
||||||
REQUIREMENTS = ['paho-mqtt>=1.1']
|
REQUIREMENTS = ['paho-mqtt>=1.1']
|
||||||
|
|
||||||
|
ATTR_SUBTOPIC = 'subtopic'
|
||||||
|
ATTR_PAYLOAD = 'payload'
|
||||||
|
|
||||||
|
|
||||||
|
def send_message(hass, subtopic, payload):
|
||||||
|
""" Send an MQTT message. """
|
||||||
|
hass.services.call(DOMAIN, MQTT_SEND, {ATTR_SUBTOPIC: subtopic,
|
||||||
|
ATTR_PAYLOAD: payload})
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
""" Get the MQTT protocol service. """
|
""" Get the MQTT protocol service. """
|
||||||
|
|
||||||
if not validate_config(config,
|
if not validate_config(config,
|
||||||
{DOMAIN: ['broker',
|
{DOMAIN: ['broker',
|
||||||
'port',
|
'topic']},
|
||||||
'topic',
|
|
||||||
'keepalive',
|
|
||||||
'client_id']},
|
|
||||||
_LOGGER):
|
_LOGGER):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -89,12 +90,14 @@ def setup(hass, config):
|
|||||||
|
|
||||||
global MQTT_CLIENT
|
global MQTT_CLIENT
|
||||||
|
|
||||||
MQTT_CLIENT = MQTT(hass,
|
broker = config[DOMAIN]['broker']
|
||||||
config[DOMAIN]['broker'],
|
port = config[DOMAIN].get('port', MQTT_DEFAULT_PORT)
|
||||||
config[DOMAIN]['port'],
|
topic = config[DOMAIN]['topic']
|
||||||
config[DOMAIN]['topic'],
|
keepalive = config[DOMAIN].get('keepalive', MQTT_DEFAULT_KEEPALIVE)
|
||||||
config[DOMAIN]['keepalive'],
|
qos = config[DOMAIN].get('qos', MQTT_DEFAULT_QOS)
|
||||||
config[DOMAIN]['client_id'])
|
|
||||||
|
MQTT_CLIENT = MQTT(hass, broker, port, topic, keepalive, qos,
|
||||||
|
MQTT_CLIENT_ID)
|
||||||
|
|
||||||
def stop_mqtt(event):
|
def stop_mqtt(event):
|
||||||
""" Stop MQTT component. """
|
""" Stop MQTT component. """
|
||||||
@ -113,15 +116,18 @@ def setup(hass, config):
|
|||||||
|
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_mqtt)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_mqtt)
|
||||||
|
|
||||||
def send_message(call):
|
def mqtt_message(call):
|
||||||
""" Sending an MQTT message. """
|
""" Handle sending MQTT message service calls. """
|
||||||
subtopic = 'master'
|
subtopic = call.data.get(ATTR_SUBTOPIC)
|
||||||
complete_topic = 'home-assistant/{}'.format(str(subtopic))
|
complete_topic = '{}/{}'.format(str(topic), str(subtopic))
|
||||||
MQTT_CLIENT.publish(complete_topic, str(call))
|
|
||||||
|
payload = call.data.get(ATTR_PAYLOAD)
|
||||||
|
|
||||||
|
MQTT_CLIENT.publish(complete_topic, payload=payload)
|
||||||
|
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_mqtt)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_mqtt)
|
||||||
|
|
||||||
hass.services.register(DOMAIN, MQTT_SEND, send_message)
|
hass.services.register(DOMAIN, MQTT_SEND, mqtt_message)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -131,14 +137,14 @@ def setup(hass, config):
|
|||||||
# pylint: disable=too-many-arguments, invalid-name
|
# pylint: disable=too-many-arguments, invalid-name
|
||||||
class MQTT(object):
|
class MQTT(object):
|
||||||
""" Implements messaging service for MQTT. """
|
""" Implements messaging service for MQTT. """
|
||||||
def __init__(self, hass, broker, port, topic, keepalive, clientid=None):
|
def __init__(self, hass, broker, port, topic, keepalive, qos, clientid):
|
||||||
|
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._broker = broker
|
self._broker = broker
|
||||||
self._port = port
|
self._port = port
|
||||||
self._topic = topic
|
self._topic = topic
|
||||||
self._keepalive = keepalive
|
self._keepalive = keepalive
|
||||||
self.msg = None
|
self._qos = qos
|
||||||
|
|
||||||
self._mqttc = mqtt.Client(clientid)
|
self._mqttc = mqtt.Client(clientid)
|
||||||
self._mqttc.on_message = self.mqtt_on_message
|
self._mqttc.on_message = self.mqtt_on_message
|
||||||
@ -156,7 +162,7 @@ class MQTT(object):
|
|||||||
|
|
||||||
def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
|
def mqtt_on_subscribe(self, mqttc, obj, mid, granted_qos):
|
||||||
""" Subscribe callback """
|
""" Subscribe callback """
|
||||||
complete_topic = self._topic + '/#'
|
complete_topic = '{}/#'.format(self._topic)
|
||||||
_LOGGER.info('Subscribed to %s', complete_topic)
|
_LOGGER.info('Subscribed to %s', complete_topic)
|
||||||
|
|
||||||
def mqtt_on_message(self, mqttc, obj, msg):
|
def mqtt_on_message(self, mqttc, obj, msg):
|
||||||
@ -169,7 +175,7 @@ class MQTT(object):
|
|||||||
|
|
||||||
def subscribe(self, topic):
|
def subscribe(self, topic):
|
||||||
""" Subscribe to a topic. """
|
""" Subscribe to a topic. """
|
||||||
self._mqttc.subscribe(self._topic, 0)
|
self._mqttc.subscribe(self._topic, qos=self._qos)
|
||||||
|
|
||||||
def unsubscribe(self, topic):
|
def unsubscribe(self, topic):
|
||||||
""" Unsubscribe from topic. """
|
""" Unsubscribe from topic. """
|
||||||
@ -184,7 +190,7 @@ class MQTT(object):
|
|||||||
self._mqttc.connect(self._broker,
|
self._mqttc.connect(self._broker,
|
||||||
int(self._port),
|
int(self._port),
|
||||||
int(self._keepalive))
|
int(self._keepalive))
|
||||||
self._mqttc.subscribe(self._topic + '/#', 0)
|
self._mqttc.subscribe('{}/#'.format(self._topic), qos=self._qos)
|
||||||
self._mqttc.loop_start()
|
self._mqttc.loop_start()
|
||||||
|
|
||||||
def publish(self, topic, payload):
|
def publish(self, topic, payload):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user