MQTT (aka MQ Telemetry Transport) is a machine-to-machine or “Internet of Things” connectivity protocol on top of TCP/IP. It allows extremely lightweight publish/subscribe messaging transport. To integrate MQTT into Home Assistant... MQTT
- https://github.com/KmanOz/Sonoff-HomeAssistant
- http://groundp.in/2016/10/18/step-by-step-guide-to-setting-up-esp-easy-with-home-assistant/
user@server:~$ nano ~/.homeassistant/configuration.yaml
mqtt:
broker: iot.eclipse.org
port: 1883
client_id: edznahomeassistant
keepalive: 60
MQTT example component
user@server:~$ nano ~/.homeassistant/custom_components/hello_mqtt.py
import homeassistant.loader as loader
# The domain of your component. Should be equal to the name of your component.
DOMAIN = "hello_mqtt"
# List of component names (string) your component depends upon.
DEPENDENCIES = ['mqtt']
CONF_TOPIC = 'topic'
DEFAULT_TOPIC = 'edzna/hello'
def setup(hass, config):
"""Setup the Hello MQTT component."""
mqtt = loader.get_component('mqtt')
topic = config[DOMAIN].get('topic', DEFAULT_TOPIC)
entity_id = 'hello_mqtt.last_message'
# Listener to be called when we receive a message.
def message_received(topic, payload, qos):
"""A new MQTT message has been received."""
hass.states.set(entity_id, payload)
# Subscribe our listener to a topic.
mqtt.subscribe(hass, topic, message_received)
# Set the intial state
hass.states.set(entity_id, 'No messages')
# Service to publish a message on MQTT.
def set_state_service(call):
"""Service to send a message."""
mqtt.publish(hass, topic, call.data.get('new_state'))
# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'set_state', set_state_service)
# Return boolean to indicate that initialization was successfully.
return True
user@server:~$ nano ~/.homeassistant/configuration.yaml
hello_mqtt:
topic: edzna/hello
user@server:~$ mosquitto_pub -h test.mosquitto.org -p 1883 -t edzna/hello -m "ThisIsIt"