home-assistant.io/source/_cookbook/python_component_mqtt_basic.markdown
Franck Nijhof c464056402
Making our website faster, cleaner and prettier ()
* 🔥 Removes octopress.js

* 🔥 Removes use of root_url var

* 🔥 Removes Octopress generator reference from feed

* 🔥 Removes delicious support

* 🔥 Removes support for Pinboard

* 🔥 Removes support for Disqus

* 🔥 Removes support for Google Plus

* ↩️ Migrate custom after_footer to default template

* ↩️ Migrate custom footer to default template

* ↩️ Migrate custom header to default template

* 🔥 Removes unused template files

* 🚀 Places time to read directly in post template

* 🚀 Removes unneeded capture from archive_post.html template

* 🔥 🚀 Removes unused, but heaving sorting call in component page

* 🚀 Merged javascripts into a single file

* 🔥 Removes more uses of root_url

* 🚀 Removal of unneeded captures from head

* 🔥 🚀 Removal of expensive liquid HTML compressor

* 🔥 Removes unneeded templates

* 🚀 Replaces kramdown with GitHub's CommonMark 🚀

* 💄 Adds Prism code syntax highlighting

*  Adds support for redirect in Netlify

* ↩️ 🔥 Let Netlify handle all developer doc redirects

* ✏️ Fixes typo in redirects file: Netify -> Netlify

* 🔥 Removes unused .themes folder

* 🔥 Removes unused aside.html template

* 🔥 Removes Disqus config leftover

* 🔥 Removes rouge highlighter config

* 🔥 Removes Octopress 🎉

* 💄 Adjust code block font size and adds soft wraps

* 💄 Adds styling for inline code blocks

* 💄 Improve styling of note/warning/info boxes + div support

* 🔨 Rewrites all note/warning/info boxes
2019-07-15 22:17:54 +02:00

77 lines
2.3 KiB
Markdown

---
title: "Basic MQTT Example"
description: ""
ha_category: Custom Python Component Examples
---
<div class='note'>
This example requires you to have the [MQTT integration](/components/mqtt/) up and running.
</div>
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.
```python
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.
```yaml
# configuration.yaml entry
hello_mqtt:
topic: some_mqtt/topic/here
```
You can call the service with example payload:
```json
{
"new_state": "some new state"
}
```