From 52ad6b58dbad5893710b830793e3af3474f520da Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Thu, 10 Mar 2016 18:54:43 +0100 Subject: [PATCH] Add mqtt_export component --- homeassistant/components/mqtt_export.py | 44 +++++++++++++++++++++++ tests/components/test_mqtt_export.py | 47 +++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 homeassistant/components/mqtt_export.py create mode 100644 tests/components/test_mqtt_export.py diff --git a/homeassistant/components/mqtt_export.py b/homeassistant/components/mqtt_export.py new file mode 100644 index 00000000000000..eeb141bf200385 --- /dev/null +++ b/homeassistant/components/mqtt_export.py @@ -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 diff --git a/tests/components/test_mqtt_export.py b/tests/components/test_mqtt_export.py new file mode 100644 index 00000000000000..9fad09cb087ef6 --- /dev/null +++ b/tests/components/test_mqtt_export.py @@ -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)