Add JSON attribute topic to MQTT binary sensor
Add MqttAttributes mixin
This commit is contained in:
1 parent
b7e2522083
commit
b9ad19acbf
3 files changed
+136
-6
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""The tests for the MQTT binary sensor platform."""
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
from unittest.mock import Mock, patch
|
||||
from datetime import timedelta
|
||||
|
||||
import homeassistant.core as ha
|
||||
@@ -256,6 +256,67 @@ class TestSensorMQTT(unittest.TestCase):
|
||||
assert STATE_OFF == state.state
|
||||
assert 3 == len(events)
|
||||
|
||||
def test_setting_sensor_attribute_via_mqtt_json_message(self):
|
||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||
binary_sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'json_attributes_topic': 'attr-topic'
|
||||
}
|
||||
})
|
||||
|
||||
fire_mqtt_message(self.hass, 'attr-topic', '{ "val": "100" }')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
|
||||
assert '100' == \
|
||||
state.attributes.get('val')
|
||||
|
||||
@patch('homeassistant.components.mqtt._LOGGER')
|
||||
def test_update_with_json_attrs_not_dict(self, mock_logger):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||
binary_sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'json_attributes_topic': 'attr-topic'
|
||||
}
|
||||
})
|
||||
|
||||
fire_mqtt_message(self.hass, 'attr-topic', '[ "list", "of", "things"]')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
|
||||
assert state.attributes.get('val') is None
|
||||
mock_logger.debug.assert_called_with(
|
||||
'JSON result was not a dictionary')
|
||||
|
||||
@patch('homeassistant.components.mqtt._LOGGER')
|
||||
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
|
||||
"""Test attributes get extracted from a JSON result."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||
binary_sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'json_attributes_topic': 'attr-topic'
|
||||
}
|
||||
})
|
||||
|
||||
fire_mqtt_message(self.hass, 'attr-topic', 'This is not JSON')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
assert state.attributes.get('val') is None
|
||||
mock_logger.debug.assert_called_with(
|
||||
'Erroneous JSON: %s', 'This is not JSON')
|
||||
|
||||
|
||||
async def test_unique_id(hass):
|
||||
"""Test unique id option only creates one sensor per unique_id."""
|
||||
|
||||
Reference in new issue
Block a user