diff --git a/tests/components/device_tracker/test_init.py b/tests/components/device_tracker/test_init.py index 84cca1bb843..ebf568309ad 100644 --- a/tests/components/device_tracker/test_init.py +++ b/tests/components/device_tracker/test_init.py @@ -10,7 +10,7 @@ import os from homeassistant.components import zone from homeassistant.core import callback, State -from homeassistant.setup import setup_component +from homeassistant.setup import setup_component, async_setup_component from homeassistant.helpers import discovery from homeassistant.loader import get_component from homeassistant.util.async import run_coroutine_threadsafe @@ -152,26 +152,6 @@ class TestComponentsDeviceTracker(unittest.TestCase): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) - # pylint: disable=invalid-name - def test_adding_unknown_device_to_config(self): - """Test the adding of unknown devices to configuration file.""" - scanner = get_component('device_tracker.test').SCANNER - scanner.reset() - scanner.come_home('DEV1') - - with assert_setup_component(1, device_tracker.DOMAIN): - assert setup_component(self.hass, device_tracker.DOMAIN, { - device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}) - - # wait for async calls (macvendor) to finish - self.hass.block_till_done() - - config = device_tracker.load_config(self.yaml_devices, self.hass, - timedelta(seconds=0)) - assert len(config) == 1 - assert config[0].dev_id == 'dev1' - assert config[0].track - def test_gravatar(self): """Test the Gravatar generation.""" dev_id = 'test' @@ -646,61 +626,6 @@ class TestComponentsDeviceTracker(unittest.TestCase): assert len(config) == 4 - def test_config_failure(self): - """Test that the device tracker see failures.""" - with assert_setup_component(0, device_tracker.DOMAIN): - setup_component(self.hass, device_tracker.DOMAIN, - {device_tracker.DOMAIN: { - device_tracker.CONF_CONSIDER_HOME: -1}}) - - def test_picture_and_icon_on_see_discovery(self): - """Test that picture and icon are set in initial see.""" - tracker = device_tracker.DeviceTracker( - self.hass, timedelta(seconds=60), False, {}, []) - tracker.see(dev_id=11, picture='pic_url', icon='mdi:icon') - self.hass.block_till_done() - config = device_tracker.load_config(self.yaml_devices, self.hass, - timedelta(seconds=0)) - assert len(config) == 1 - assert config[0].icon == 'mdi:icon' - assert config[0].entity_picture == 'pic_url' - - def test_default_hide_if_away_is_used(self): - """Test that default track_new is used.""" - tracker = device_tracker.DeviceTracker( - self.hass, timedelta(seconds=60), False, - {device_tracker.CONF_AWAY_HIDE: True}, []) - tracker.see(dev_id=12) - self.hass.block_till_done() - config = device_tracker.load_config(self.yaml_devices, self.hass, - timedelta(seconds=0)) - assert len(config) == 1 - self.assertTrue(config[0].hidden) - - def test_backward_compatibility_for_track_new(self): - """Test backward compatibility for track new.""" - tracker = device_tracker.DeviceTracker( - self.hass, timedelta(seconds=60), False, - {device_tracker.CONF_TRACK_NEW: True}, []) - tracker.see(dev_id=13) - self.hass.block_till_done() - config = device_tracker.load_config(self.yaml_devices, self.hass, - timedelta(seconds=0)) - assert len(config) == 1 - self.assertFalse(config[0].track) - - def test_old_style_track_new_is_skipped(self): - """Test old style config is skipped.""" - tracker = device_tracker.DeviceTracker( - self.hass, timedelta(seconds=60), None, - {device_tracker.CONF_TRACK_NEW: False}, []) - tracker.see(dev_id=14) - self.hass.block_till_done() - config = device_tracker.load_config(self.yaml_devices, self.hass, - timedelta(seconds=0)) - assert len(config) == 1 - self.assertFalse(config[0].track) - @asyncio.coroutine def test_async_added_to_hass(hass): @@ -742,3 +667,66 @@ def test_bad_platform(hass): } with assert_setup_component(0, device_tracker.DOMAIN): assert (yield from device_tracker.async_setup(hass, config)) + + +async def test_adding_unknown_device_to_config(mock_device_tracker_conf, hass): + """Test the adding of unknown devices to configuration file.""" + scanner = get_component('device_tracker.test').SCANNER + scanner.reset() + scanner.come_home('DEV1') + + await async_setup_component(hass, device_tracker.DOMAIN, { + device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}) + + await hass.async_block_till_done() + + assert len(mock_device_tracker_conf) == 1 + device = mock_device_tracker_conf[0] + assert device.dev_id == 'dev1' + assert device.track + + +async def test_picture_and_icon_on_see_discovery(mock_device_tracker_conf, + hass): + """Test that picture and icon are set in initial see.""" + tracker = device_tracker.DeviceTracker( + hass, timedelta(seconds=60), False, {}, []) + await tracker.async_see(dev_id=11, picture='pic_url', icon='mdi:icon') + await hass.async_block_till_done() + assert len(mock_device_tracker_conf) == 1 + assert mock_device_tracker_conf[0].icon == 'mdi:icon' + assert mock_device_tracker_conf[0].entity_picture == 'pic_url' + + +async def test_default_hide_if_away_is_used(mock_device_tracker_conf, hass): + """Test that default track_new is used.""" + tracker = device_tracker.DeviceTracker( + hass, timedelta(seconds=60), False, + {device_tracker.CONF_AWAY_HIDE: True}, []) + await tracker.async_see(dev_id=12) + await hass.async_block_till_done() + assert len(mock_device_tracker_conf) == 1 + assert mock_device_tracker_conf[0].away_hide + + +async def test_backward_compatibility_for_track_new(mock_device_tracker_conf, + hass): + """Test backward compatibility for track new.""" + tracker = device_tracker.DeviceTracker( + hass, timedelta(seconds=60), False, + {device_tracker.CONF_TRACK_NEW: True}, []) + await tracker.async_see(dev_id=13) + await hass.async_block_till_done() + assert len(mock_device_tracker_conf) == 1 + assert mock_device_tracker_conf[0].track is False + + +async def test_old_style_track_new_is_skipped(mock_device_tracker_conf, hass): + """Test old style config is skipped.""" + tracker = device_tracker.DeviceTracker( + hass, timedelta(seconds=60), None, + {device_tracker.CONF_TRACK_NEW: False}, []) + await tracker.async_see(dev_id=14) + await hass.async_block_till_done() + assert len(mock_device_tracker_conf) == 1 + assert mock_device_tracker_conf[0].track is False diff --git a/tests/components/device_tracker/test_meraki.py b/tests/components/device_tracker/test_meraki.py index a739df804fd..74fc577bca8 100644 --- a/tests/components/device_tracker/test_meraki.py +++ b/tests/components/device_tracker/test_meraki.py @@ -1,8 +1,9 @@ """The tests the for Meraki device tracker.""" import asyncio import json -from unittest.mock import patch + import pytest + from homeassistant.components.device_tracker.meraki import ( CONF_VALIDATOR, CONF_SECRET) from homeassistant.setup import async_setup_component @@ -24,12 +25,11 @@ def meraki_client(loop, hass, test_client): } })) - with patch('homeassistant.components.device_tracker.update_config'): - yield loop.run_until_complete(test_client(hass.http.app)) + yield loop.run_until_complete(test_client(hass.http.app)) @asyncio.coroutine -def test_invalid_or_missing_data(meraki_client): +def test_invalid_or_missing_data(mock_device_tracker_conf, meraki_client): """Test validator with invalid or missing data.""" req = yield from meraki_client.get(URL) text = yield from req.text() @@ -87,7 +87,7 @@ def test_invalid_or_missing_data(meraki_client): @asyncio.coroutine -def test_data_will_be_saved(hass, meraki_client): +def test_data_will_be_saved(mock_device_tracker_conf, hass, meraki_client): """Test with valid data.""" data = { "version": "2.0", @@ -130,6 +130,7 @@ def test_data_will_be_saved(hass, meraki_client): } req = yield from meraki_client.post(URL, data=json.dumps(data)) assert req.status == 200 + yield from hass.async_block_till_done() state_name = hass.states.get('{}.{}'.format('device_tracker', '0026abb8a9a4')).state assert 'home' == state_name diff --git a/tests/components/media_player/test_mediaroom.py b/tests/components/media_player/test_mediaroom.py deleted file mode 100644 index 7c7922b87be..00000000000 --- a/tests/components/media_player/test_mediaroom.py +++ /dev/null @@ -1,32 +0,0 @@ -"""The tests for the mediaroom media_player.""" - -import unittest - -from homeassistant.setup import setup_component -import homeassistant.components.media_player as media_player -from tests.common import ( - assert_setup_component, get_test_home_assistant) - - -class TestMediaroom(unittest.TestCase): - """Tests the Mediaroom Component.""" - - def setUp(self): - """Initialize values for this test case class.""" - self.hass = get_test_home_assistant() - - def tearDown(self): # pylint: disable=invalid-name - """Stop everything that we started.""" - self.hass.stop() - - def test_mediaroom_config(self): - """Test set up the platform with basic configuration.""" - config = { - media_player.DOMAIN: { - 'platform': 'mediaroom', - 'name': 'Living Room' - } - } - with assert_setup_component(1, media_player.DOMAIN) as result_config: - assert setup_component(self.hass, media_player.DOMAIN, config) - assert result_config[media_player.DOMAIN] diff --git a/tests/conftest.py b/tests/conftest.py index 989785e72d5..8f0ca787721 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,8 +11,8 @@ import requests_mock as _requests_mock from homeassistant import util from homeassistant.util import location -from tests.common import async_test_home_assistant, INSTANCES, \ - async_mock_mqtt_component +from tests.common import ( + async_test_home_assistant, INSTANCES, async_mock_mqtt_component, mock_coro) from tests.test_util.aiohttp import mock_aiohttp_client from tests.mock.zwave import MockNetwork, MockOption @@ -106,3 +106,24 @@ def mock_openzwave(): 'openzwave.group': base_mock.group, }): yield base_mock + + +@pytest.fixture +def mock_device_tracker_conf(): + """Prevent device tracker from reading/writing data.""" + devices = [] + + async def mock_update_config(path, id, entity): + devices.append(entity) + + with patch( + 'homeassistant.components.device_tracker' + '.DeviceTracker.async_update_config', + side_effect=mock_update_config + ), patch( + 'homeassistant.components.device_tracker.async_load_config', + side_effect=lambda *args: mock_coro(devices) + ), patch('homeassistant.components.device_tracker' + '.Device.set_vendor_for_mac'): + + yield devices