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 2018-02-10 00:22:50 +01:00 committed by Paulus Schoutsen
parent 1db4df6d3a
commit cad9e9a4cb
4 changed files with 175 additions and 34 deletions

View file

@ -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))