allow wildcards in subscription (#12247)

* allow wildcards in subscription

* remove whitespaces

* make function public

* also implement for mqtt_json

* avoid mqtt-outside topic matching

* add wildcard tests

* add not matching wildcard tests

* fix not-matching tests
This commit is contained in:
escoand authored and Paulus Schoutsen committed 2018-02-09 15:22:50 -08:00
1 parent 1db4df6d3a
commit cad9e9a4cb
4 files changed
+175 -34

No files matched your search

@@ -70,3 +70,79 @@ class TestComponentsDeviceTrackerMQTT(unittest.TestCase):
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertEqual(location, self.hass.states.get(entity_id).state)
def test_single_level_wildcard_topic(self):
"""Test single level wildcard topic."""
dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
subscription = '/location/+/paulus'
topic = '/location/room/paulus'
location = 'work'
self.hass.config.components = set(['mqtt', 'zone'])
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertEqual(location, self.hass.states.get(entity_id).state)
def test_multi_level_wildcard_topic(self):
"""Test multi level wildcard topic."""
dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
subscription = '/location/#'
topic = '/location/room/paulus'
location = 'work'
self.hass.config.components = set(['mqtt', 'zone'])
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertEqual(location, self.hass.states.get(entity_id).state)
def test_single_level_wildcard_topic_not_matching(self):
"""Test not matching single level wildcard topic."""
dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
subscription = '/location/+/paulus'
topic = '/location/paulus'
location = 'work'
self.hass.config.components = set(['mqtt', 'zone'])
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertIsNone(self.hass.states.get(entity_id))
def test_multi_level_wildcard_topic_not_matching(self):
"""Test not matching multi level wildcard topic."""
dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
subscription = '/location/#'
topic = '/somewhere/room/paulus'
location = 'work'
self.hass.config.components = set(['mqtt', 'zone'])
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertIsNone(self.hass.states.get(entity_id))
@@ -123,3 +123,77 @@ class TestComponentsDeviceTrackerJSONMQTT(unittest.TestCase):
"Skipping update for following data because of missing "
"or malformatted data: {\"longitude\": 2.0}",
test_handle.output[0])
def test_single_level_wildcard_topic(self):
"""Test single level wildcard topic."""
dev_id = 'zanzito'
subscription = 'location/+/zanzito'
topic = 'location/room/zanzito'
location = json.dumps(LOCATION_MESSAGE)
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt_json',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
state = self.hass.states.get('device_tracker.zanzito')
self.assertEqual(state.attributes.get('latitude'), 2.0)
self.assertEqual(state.attributes.get('longitude'), 1.0)
def test_multi_level_wildcard_topic(self):
"""Test multi level wildcard topic."""
dev_id = 'zanzito'
subscription = 'location/#'
topic = 'location/zanzito'
location = json.dumps(LOCATION_MESSAGE)
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt_json',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
state = self.hass.states.get('device_tracker.zanzito')
self.assertEqual(state.attributes.get('latitude'), 2.0)
self.assertEqual(state.attributes.get('longitude'), 1.0)
def test_single_level_wildcard_topic_not_matching(self):
"""Test not matching single level wildcard topic."""
dev_id = 'zanzito'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
subscription = 'location/+/zanzito'
topic = 'location/zanzito'
location = json.dumps(LOCATION_MESSAGE)
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt_json',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertIsNone(self.hass.states.get(entity_id))
def test_multi_level_wildcard_topic_not_matching(self):
"""Test not matching multi level wildcard topic."""
dev_id = 'zanzito'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
subscription = 'location/#'
topic = 'somewhere/zanzito'
location = json.dumps(LOCATION_MESSAGE)
assert setup_component(self.hass, device_tracker.DOMAIN, {
device_tracker.DOMAIN: {
CONF_PLATFORM: 'mqtt_json',
'devices': {dev_id: subscription}
}
})
fire_mqtt_message(self.hass, topic, location)
self.hass.block_till_done()
self.assertIsNone(self.hass.states.get(entity_id))