Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions homeassistant/components/mqtt_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
MQTT publisher for all Home Assistant states.

For more details about this component, please refer to the documentation at
https://home-assistant.io/components/mqtt_export/
"""
import json

import homeassistant.loader as loader
from homeassistant.const import (STATE_UNKNOWN, EVENT_STATE_CHANGED)
from homeassistant.remote import JSONEncoder

DOMAIN = "mqtt_export"
DEPENDENCIES = ['mqtt']

DEFAULT_TOPIC = 'home-assistant/states'
PAYLOAD = None


def setup(hass, config):
"""Setup the MQTT export component."""
mqtt = loader.get_component('mqtt')
pub_topic = config[DOMAIN].get('publish_topic', DEFAULT_TOPIC)

global PAYLOAD
PAYLOAD = dict(states=None, details=None)

# Add the configuration
PAYLOAD['details'] = hass.config.as_dict()

def mqtt_event_listener(event):
"""Listen for new messages on the bus and send data to MQTT."""
state = event.data.get('new_state')
if state is None or state.state in (STATE_UNKNOWN, ''):
return None

PAYLOAD['states'] = hass.states.all()

payload = json.dumps(PAYLOAD, cls=JSONEncoder)
mqtt.publish(hass, pub_topic, payload)

hass.bus.listen(EVENT_STATE_CHANGED, mqtt_event_listener)

return True
47 changes: 47 additions & 0 deletions tests/components/test_mqtt_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""The tests for the MQTT export component."""
import unittest
from unittest.mock import patch

import homeassistant.components.mqtt_export as export
import homeassistant.util.dt as dt_util

from tests.common import (
get_test_home_assistant,
mock_mqtt_component,
fire_time_changed
)


class TestMqttExport(unittest.TestCase):
"""Test the MQTT export module."""

def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
super(TestMqttExport, self).setUp()
self.hass = get_test_home_assistant()
self.mock_publish = mock_mqtt_component(self.hass)

def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()

def add_export(self, pub_topic=None):
"""Add a mqtt_export component to hass."""
config = {}
if pub_topic:
config['publish_topic'] = pub_topic
return export.setup(self.hass, {export.DOMAIN: config})

def test_setup_succeeds(self):
"""Test if setup is successful."""
self.assertTrue(self.add_export())

@patch('homeassistant.components.mqtt.publish')
def test_time_event_does_not_send_message(self, mock_pub):
""""Test of not sending a new message if time event."""
self.assertTrue(self.add_export(pub_topic='bar'))
self.hass.pool.block_till_done()
mock_pub.reset_mock()

fire_time_changed(self.hass, dt_util.utcnow())
self.assertFalse(mock_pub.called)