Example fixed to be compatible with the latest ha version (#8066)

This commit is contained in:
rgruebel 2019-01-06 22:19:59 +01:00 committed by Fabian Affolter
parent d066b69451
commit beba4f20df

View File

@ -34,8 +34,8 @@ DEFAULT_TOPIC = 'home-assistant/hello_mqtt'
def setup(hass, config):
"""Set up the Hello MQTT component."""
mqtt = loader.get_component('mqtt')
topic = config[DOMAIN].get('topic', DEFAULT_TOPIC)
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.
@ -44,7 +44,7 @@ def setup(hass, config):
hass.states.set(entity_id, payload)
# Subscribe our listener to a topic.
mqtt.subscribe(hass, topic, message_received)
mqtt.subscribe(topic, message_received)
# Set the initial state.
hass.states.set(entity_id, 'No messages')
@ -52,7 +52,7 @@ def setup(hass, config):
# 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'))
mqtt.publish(topic, call.data.get('new_state'))
# Register our service with Home Assistant.
hass.services.register(DOMAIN, 'set_state', set_state_service)