Clean up mqtt device tracker tests (#23700)

* Move mqtt device tracker test under mqtt dir

* Patch config file load and save

* Clean up

* Sort imports

* Put expected value last in assertions
This commit is contained in:
Martin Hjelmare 2019-05-05 20:50:38 +02:00 committed by Rohan Kapoor
parent c0f9ccfdbb
commit 4fe0cd76f8

View File

@ -1,27 +1,18 @@
"""The tests for the MQTT device tracker platform.""" """The tests for the MQTT device tracker platform."""
import logging
import os
from asynctest import patch from asynctest import patch
import pytest import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components import device_tracker from homeassistant.components import device_tracker
from homeassistant.const import CONF_PLATFORM from homeassistant.const import CONF_PLATFORM
from homeassistant.setup import async_setup_component
from tests.common import ( from tests.common import async_fire_mqtt_message
async_mock_mqtt_component, async_fire_mqtt_message)
_LOGGER = logging.getLogger(__name__)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def setup_comp(hass): def setup_comp(hass, mqtt_mock):
"""Initialize components.""" """Set up mqtt component."""
hass.loop.run_until_complete(async_mock_mqtt_component(hass)) pass
yaml_devices = hass.config.path(device_tracker.YAML_DEVICES)
yield
if os.path.isfile(yaml_devices):
os.remove(yaml_devices)
async def test_ensure_device_tracker_platform_validation(hass): async def test_ensure_device_tracker_platform_validation(hass):
@ -45,7 +36,7 @@ async def test_ensure_device_tracker_platform_validation(hass):
assert mock_sp.call_count == 1 assert mock_sp.call_count == 1
async def test_new_message(hass): async def test_new_message(hass, mock_device_tracker_conf):
"""Test new message.""" """Test new message."""
dev_id = 'paulus' dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
@ -61,10 +52,10 @@ async def test_new_message(hass):
}) })
async_fire_mqtt_message(hass, topic, location) async_fire_mqtt_message(hass, topic, location)
await hass.async_block_till_done() await hass.async_block_till_done()
assert location == hass.states.get(entity_id).state assert hass.states.get(entity_id).state == location
async def test_single_level_wildcard_topic(hass): async def test_single_level_wildcard_topic(hass, mock_device_tracker_conf):
"""Test single level wildcard topic.""" """Test single level wildcard topic."""
dev_id = 'paulus' dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
@ -81,10 +72,10 @@ async def test_single_level_wildcard_topic(hass):
}) })
async_fire_mqtt_message(hass, topic, location) async_fire_mqtt_message(hass, topic, location)
await hass.async_block_till_done() await hass.async_block_till_done()
assert location == hass.states.get(entity_id).state assert hass.states.get(entity_id).state == location
async def test_multi_level_wildcard_topic(hass): async def test_multi_level_wildcard_topic(hass, mock_device_tracker_conf):
"""Test multi level wildcard topic.""" """Test multi level wildcard topic."""
dev_id = 'paulus' dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
@ -101,10 +92,11 @@ async def test_multi_level_wildcard_topic(hass):
}) })
async_fire_mqtt_message(hass, topic, location) async_fire_mqtt_message(hass, topic, location)
await hass.async_block_till_done() await hass.async_block_till_done()
assert location == hass.states.get(entity_id).state assert hass.states.get(entity_id).state == location
async def test_single_level_wildcard_topic_not_matching(hass): async def test_single_level_wildcard_topic_not_matching(
hass, mock_device_tracker_conf):
"""Test not matching single level wildcard topic.""" """Test not matching single level wildcard topic."""
dev_id = 'paulus' dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
@ -124,7 +116,8 @@ async def test_single_level_wildcard_topic_not_matching(hass):
assert hass.states.get(entity_id) is None assert hass.states.get(entity_id) is None
async def test_multi_level_wildcard_topic_not_matching(hass): async def test_multi_level_wildcard_topic_not_matching(
hass, mock_device_tracker_conf):
"""Test not matching multi level wildcard topic.""" """Test not matching multi level wildcard topic."""
dev_id = 'paulus' dev_id = 'paulus'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id) entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)