2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the MQTT device tracker platform."""
|
2018-11-24 18:24:06 +00:00
|
|
|
from asynctest import patch
|
|
|
|
import pytest
|
2015-09-12 16:15:28 +00:00
|
|
|
|
|
|
|
from homeassistant.components import device_tracker
|
2019-05-15 21:43:45 +00:00
|
|
|
from homeassistant.components.device_tracker.const import ENTITY_ID_FORMAT
|
2015-09-12 16:15:28 +00:00
|
|
|
from homeassistant.const import CONF_PLATFORM
|
2019-05-05 18:50:38 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2015-09-12 16:15:28 +00:00
|
|
|
|
2019-05-05 18:50:38 +00:00
|
|
|
from tests.common import async_fire_mqtt_message
|
2016-08-30 16:22:52 +00:00
|
|
|
|
2015-09-12 16:15:28 +00:00
|
|
|
|
2018-11-24 18:24:06 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2019-05-05 18:50:38 +00:00
|
|
|
def setup_comp(hass, mqtt_mock):
|
|
|
|
"""Set up mqtt component."""
|
|
|
|
pass
|
2018-02-09 23:22:50 +00:00
|
|
|
|
|
|
|
|
2018-11-24 18:24:06 +00:00
|
|
|
async def test_ensure_device_tracker_platform_validation(hass):
|
|
|
|
"""Test if platform validation was done."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-11-24 18:24:06 +00:00
|
|
|
async def mock_setup_scanner(hass, config, see, discovery_info=None):
|
|
|
|
"""Check that Qos was added by validation."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "qos" in config
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.mqtt.device_tracker." "async_setup_scanner",
|
|
|
|
autospec=True,
|
|
|
|
side_effect=mock_setup_scanner,
|
|
|
|
) as mock_sp:
|
|
|
|
|
|
|
|
dev_id = "paulus"
|
|
|
|
topic = "/location/paulus"
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
device_tracker.DOMAIN,
|
|
|
|
{
|
|
|
|
device_tracker.DOMAIN: {
|
|
|
|
CONF_PLATFORM: "mqtt",
|
|
|
|
"devices": {dev_id: topic},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-11-24 18:24:06 +00:00
|
|
|
assert mock_sp.call_count == 1
|
|
|
|
|
|
|
|
|
2019-05-05 18:50:38 +00:00
|
|
|
async def test_new_message(hass, mock_device_tracker_conf):
|
2018-11-24 18:24:06 +00:00
|
|
|
"""Test new message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_id = "paulus"
|
2019-05-15 21:43:45 +00:00
|
|
|
entity_id = ENTITY_ID_FORMAT.format(dev_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
topic = "/location/paulus"
|
|
|
|
location = "work"
|
|
|
|
|
|
|
|
hass.config.components = set(["mqtt", "zone"])
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
device_tracker.DOMAIN,
|
|
|
|
{device_tracker.DOMAIN: {CONF_PLATFORM: "mqtt", "devices": {dev_id: topic}}},
|
|
|
|
)
|
2018-11-24 18:24:06 +00:00
|
|
|
async_fire_mqtt_message(hass, topic, location)
|
|
|
|
await hass.async_block_till_done()
|
2019-05-05 18:50:38 +00:00
|
|
|
assert hass.states.get(entity_id).state == location
|
2018-11-24 18:24:06 +00:00
|
|
|
|
|
|
|
|
2019-05-05 18:50:38 +00:00
|
|
|
async def test_single_level_wildcard_topic(hass, mock_device_tracker_conf):
|
2018-11-24 18:24:06 +00:00
|
|
|
"""Test single level wildcard topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_id = "paulus"
|
2019-05-15 21:43:45 +00:00
|
|
|
entity_id = ENTITY_ID_FORMAT.format(dev_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
subscription = "/location/+/paulus"
|
|
|
|
topic = "/location/room/paulus"
|
|
|
|
location = "work"
|
|
|
|
|
|
|
|
hass.config.components = set(["mqtt", "zone"])
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
device_tracker.DOMAIN,
|
|
|
|
{
|
|
|
|
device_tracker.DOMAIN: {
|
|
|
|
CONF_PLATFORM: "mqtt",
|
|
|
|
"devices": {dev_id: subscription},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-11-24 18:24:06 +00:00
|
|
|
async_fire_mqtt_message(hass, topic, location)
|
|
|
|
await hass.async_block_till_done()
|
2019-05-05 18:50:38 +00:00
|
|
|
assert hass.states.get(entity_id).state == location
|
2018-11-24 18:24:06 +00:00
|
|
|
|
|
|
|
|
2019-05-05 18:50:38 +00:00
|
|
|
async def test_multi_level_wildcard_topic(hass, mock_device_tracker_conf):
|
2018-11-24 18:24:06 +00:00
|
|
|
"""Test multi level wildcard topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_id = "paulus"
|
2019-05-15 21:43:45 +00:00
|
|
|
entity_id = ENTITY_ID_FORMAT.format(dev_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
subscription = "/location/#"
|
|
|
|
topic = "/location/room/paulus"
|
|
|
|
location = "work"
|
|
|
|
|
|
|
|
hass.config.components = set(["mqtt", "zone"])
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
device_tracker.DOMAIN,
|
|
|
|
{
|
|
|
|
device_tracker.DOMAIN: {
|
|
|
|
CONF_PLATFORM: "mqtt",
|
|
|
|
"devices": {dev_id: subscription},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-11-24 18:24:06 +00:00
|
|
|
async_fire_mqtt_message(hass, topic, location)
|
|
|
|
await hass.async_block_till_done()
|
2019-05-05 18:50:38 +00:00
|
|
|
assert hass.states.get(entity_id).state == location
|
2018-11-24 18:24:06 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_single_level_wildcard_topic_not_matching(hass, mock_device_tracker_conf):
|
2018-11-24 18:24:06 +00:00
|
|
|
"""Test not matching single level wildcard topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_id = "paulus"
|
2019-05-15 21:43:45 +00:00
|
|
|
entity_id = ENTITY_ID_FORMAT.format(dev_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
subscription = "/location/+/paulus"
|
|
|
|
topic = "/location/paulus"
|
|
|
|
location = "work"
|
|
|
|
|
|
|
|
hass.config.components = set(["mqtt", "zone"])
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
device_tracker.DOMAIN,
|
|
|
|
{
|
|
|
|
device_tracker.DOMAIN: {
|
|
|
|
CONF_PLATFORM: "mqtt",
|
|
|
|
"devices": {dev_id: subscription},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-11-24 18:24:06 +00:00
|
|
|
async_fire_mqtt_message(hass, topic, location)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id) is None
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_multi_level_wildcard_topic_not_matching(hass, mock_device_tracker_conf):
|
2018-11-24 18:24:06 +00:00
|
|
|
"""Test not matching multi level wildcard topic."""
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_id = "paulus"
|
2019-05-15 21:43:45 +00:00
|
|
|
entity_id = ENTITY_ID_FORMAT.format(dev_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
subscription = "/location/#"
|
|
|
|
topic = "/somewhere/room/paulus"
|
|
|
|
location = "work"
|
|
|
|
|
|
|
|
hass.config.components = set(["mqtt", "zone"])
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
device_tracker.DOMAIN,
|
|
|
|
{
|
|
|
|
device_tracker.DOMAIN: {
|
|
|
|
CONF_PLATFORM: "mqtt",
|
|
|
|
"devices": {dev_id: subscription},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-11-24 18:24:06 +00:00
|
|
|
async_fire_mqtt_message(hass, topic, location)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert hass.states.get(entity_id) is None
|