home-assistant.io/source/_cookbook/python_component_mqtt_basic.markdown
Ville Skyttä b6a904933b Code blocks (#11648)
* Run black on Python code blocks, syntax fixes

https://github.com/scop/misc/blob/master/black_markdown.py

* Code block language marker fixes

* String formatting style tweaks
2020-01-07 12:58:15 +01:00

2.3 KiB

title, description, ha_category
title description ha_category
Basic MQTT Example Custom Python Component Examples

This example requires you to have the MQTT integration up and running.

This is a simple hello world example to show the basics of using MQTT in a custom integration. To use this example, create the file <config dir>/custom_components/hello_mqtt.py and copy the below example code.

This example follows a topic on MQTT and updates the state of an entity to the last message received on that topic. It will also register a service 'set_state' that will publish a message to the MQTT topic that we're listening to.

import homeassistant.loader as loader

# The domain of your component. Should be equal to the name of your component.
DOMAIN = "hello_mqtt"

# List of integration names (string) your integration depends upon.
DEPENDENCIES = ["mqtt"]


CONF_TOPIC = "topic"
DEFAULT_TOPIC = "home-assistant/hello_mqtt"


def setup(hass, config):
    """Set up the Hello MQTT component."""
    mqtt = hass.components.mqtt
    topic = config[DOMAIN].get(CONF_TOPIC, DEFAULT_TOPIC)
    entity_id = "hello_mqtt.last_message"

    # Listener to be called when we receive a message.
    # The msg parameter is a Message object with the following members:
    # - topic, payload, qos, retain
    def message_received(msg):
        """Handle new MQTT messages."""
        hass.states.set(entity_id, msg.payload)

    # Subscribe our listener to a topic.
    mqtt.subscribe(topic, message_received)

    # Set the initial 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(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

Load the integration by adding the following to your configuration.yaml. When your integration is loaded, a new entity should popup and there should be a new service available to call.

# configuration.yaml entry
hello_mqtt:
  topic: some_mqtt/topic/here

You can call the service with example payload:

{
  "new_state": "some new state"
}