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
|
|
|
|
|
2016-04-07 00:32:35 +00:00
|
|
|
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."""
|
2016-04-07 00:32:35 +00:00
|
|
|
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'
|
|
|
|
}
|
2016-04-07 00:32:35 +00:00
|
|
|
})
|
2015-10-03 06:57:26 +00:00
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', '100')
|
2016-09-13 02:16:14 +00:00
|
|
|
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'))
|
2015-11-20 22:44:46 +00:00
|
|
|
|
|
|
|
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."""
|
2016-04-07 00:32:35 +00:00
|
|
|
self.hass.config.components = ['mqtt']
|
|
|
|
assert _setup_component(self.hass, sensor.DOMAIN, {
|
|
|
|
sensor.DOMAIN: {
|
2015-11-20 22:44:46 +00:00
|
|
|
'platform': 'mqtt',
|
|
|
|
'name': 'test',
|
|
|
|
'state_topic': 'test-topic',
|
|
|
|
'unit_of_measurement': 'fav unit',
|
2015-12-11 05:39:01 +00:00
|
|
|
'value_template': '{{ value_json.val }}'
|
2015-11-20 22:44:46 +00:00
|
|
|
}
|
2016-04-07 00:32:35 +00:00
|
|
|
})
|
2015-11-20 22:44:46 +00:00
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-11-20 22:44:46 +00:00
|
|
|
state = self.hass.states.get('sensor.test')
|
|
|
|
|
|
|
|
self.assertEqual('100', state.state)
|