core/tests/components/sensor/test_mqtt.py

61 lines
2.1 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the MQTT sensor platform."""
2015-10-03 06:57:26 +00:00
import unittest
from homeassistant.bootstrap import _setup_component
2015-10-03 06:57:26 +00:00
import homeassistant.components.sensor as sensor
from tests.common import mock_mqtt_component, fire_mqtt_message
2016-02-14 23:08:23 +00:00
from tests.common import get_test_home_assistant
2015-10-03 06:57:26 +00:00
class TestSensorMQTT(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the MQTT sensor."""
2015-10-03 06:57:26 +00:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
2015-10-03 06:57:26 +00:00
mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Stop down everything that was started."""
2015-10-03 06:57:26 +00:00
self.hass.stop()
def test_setting_sensor_value_via_mqtt_message(self):
2016-03-09 09:25:50 +00:00
"""Test the setting of the value via MQTT."""
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, sensor.DOMAIN, {
sensor.DOMAIN: {
2015-10-03 06:57:26 +00:00
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit'
}
})
2015-10-03 06:57:26 +00:00
fire_mqtt_message(self.hass, 'test-topic', '100')
self.hass.block_till_done()
2015-10-03 06:57:26 +00:00
state = self.hass.states.get('sensor.test')
self.assertEqual('100', state.state)
self.assertEqual('fav unit',
state.attributes.get('unit_of_measurement'))
def test_setting_sensor_value_via_mqtt_json_message(self):
2016-03-09 09:25:50 +00:00
"""Test the setting of the value via MQTT with JSON playload."""
self.hass.config.components = ['mqtt']
assert _setup_component(self.hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'value_template': '{{ value_json.val }}'
}
})
fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test')
self.assertEqual('100', state.state)