2017-02-23 10:54:35 +00:00
|
|
|
"""The tests for the discovery component."""
|
2017-03-01 15:38:49 +00:00
|
|
|
import asyncio
|
2019-12-09 16:42:00 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2017-05-13 03:14:17 +00:00
|
|
|
import pytest
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2018-08-09 11:24:14 +00:00
|
|
|
from homeassistant import config_entries
|
2017-03-01 15:38:49 +00:00
|
|
|
from homeassistant.bootstrap import async_setup_component
|
2017-02-23 10:54:35 +00:00
|
|
|
from homeassistant.components import discovery
|
2017-04-09 08:05:34 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2019-12-09 16:42:00 +00:00
|
|
|
from tests.common import async_fire_time_changed, mock_coro
|
2017-02-23 10:54:35 +00:00
|
|
|
|
|
|
|
# One might consider to "mock" services, but it's easy enough to just use
|
|
|
|
# what is already available.
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE = "yamaha"
|
|
|
|
SERVICE_COMPONENT = "media_player"
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_NO_PLATFORM = "hass_ios"
|
|
|
|
SERVICE_NO_PLATFORM_COMPONENT = "ios"
|
|
|
|
SERVICE_INFO = {"key": "value"} # Can be anything
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
UNKNOWN_SERVICE = "this_service_will_never_be_supported"
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
BASE_CONFIG = {discovery.DOMAIN: {"ignore": [], "enable": []}}
|
2017-02-23 10:54:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
IGNORE_CONFIG = {discovery.DOMAIN: {"ignore": [SERVICE_NO_PLATFORM]}}
|
2017-02-23 10:54:35 +00:00
|
|
|
|
|
|
|
|
2017-05-13 03:14:17 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def netdisco_mock():
|
|
|
|
"""Mock netdisco."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.dict("sys.modules", {"netdisco.discovery": MagicMock()}):
|
2017-05-13 03:14:17 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def mock_discovery(hass, discoveries, config=BASE_CONFIG):
|
2018-08-24 08:28:43 +00:00
|
|
|
"""Mock discoveries."""
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(hass, "discovery", config)
|
2017-03-01 15:38:49 +00:00
|
|
|
assert result
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
await hass.async_start()
|
2017-03-01 15:38:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(discovery, "_discover", discoveries), patch(
|
|
|
|
"homeassistant.components.discovery.async_discover", return_value=mock_coro()
|
|
|
|
) as mock_discover, patch(
|
|
|
|
"homeassistant.components.discovery.async_load_platform",
|
|
|
|
return_value=mock_coro(),
|
|
|
|
) as mock_platform:
|
2017-05-02 06:29:01 +00:00
|
|
|
async_fire_time_changed(hass, utcnow())
|
2017-04-09 08:05:34 +00:00
|
|
|
# Work around an issue where our loop.call_soon not get caught
|
2018-03-30 03:15:40 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_block_till_done()
|
2017-03-01 15:38:49 +00:00
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
return mock_discover, mock_platform
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_unknown_service(hass):
|
|
|
|
"""Test that unknown service is ignored."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
def discover(netdisco):
|
|
|
|
"""Fake discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [("this_service_will_never_be_supported", {"info": "some"})]
|
2017-04-09 08:05:34 +00:00
|
|
|
|
|
|
|
mock_discover, mock_platform = yield from mock_discovery(hass, discover)
|
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
assert not mock_discover.called
|
|
|
|
assert not mock_platform.called
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_load_platform(hass):
|
|
|
|
"""Test load a platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
def discover(netdisco):
|
|
|
|
"""Fake discovery."""
|
|
|
|
return [(SERVICE, SERVICE_INFO)]
|
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
mock_discover, mock_platform = yield from mock_discovery(hass, discover)
|
2017-03-01 15:38:49 +00:00
|
|
|
|
|
|
|
assert not mock_discover.called
|
|
|
|
assert mock_platform.called
|
|
|
|
mock_platform.assert_called_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, SERVICE_COMPONENT, SERVICE, SERVICE_INFO, BASE_CONFIG
|
|
|
|
)
|
2017-03-01 15:38:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_load_component(hass):
|
|
|
|
"""Test load a component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
def discover(netdisco):
|
|
|
|
"""Fake discovery."""
|
|
|
|
return [(SERVICE_NO_PLATFORM, SERVICE_INFO)]
|
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
mock_discover, mock_platform = yield from mock_discovery(hass, discover)
|
2017-03-01 15:38:49 +00:00
|
|
|
|
|
|
|
assert mock_discover.called
|
|
|
|
assert not mock_platform.called
|
|
|
|
mock_discover.assert_called_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
SERVICE_NO_PLATFORM,
|
|
|
|
SERVICE_INFO,
|
|
|
|
SERVICE_NO_PLATFORM_COMPONENT,
|
|
|
|
BASE_CONFIG,
|
|
|
|
)
|
2017-03-01 15:38:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_ignore_service(hass):
|
|
|
|
"""Test ignore service."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
def discover(netdisco):
|
|
|
|
"""Fake discovery."""
|
|
|
|
return [(SERVICE_NO_PLATFORM, SERVICE_INFO)]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_discover, mock_platform = yield from mock_discovery(
|
|
|
|
hass, discover, IGNORE_CONFIG
|
|
|
|
)
|
2017-03-01 15:38:49 +00:00
|
|
|
|
|
|
|
assert not mock_discover.called
|
|
|
|
assert not mock_platform.called
|
2017-03-03 21:11:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_discover_duplicates(hass):
|
|
|
|
"""Test load a component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-03-03 21:11:40 +00:00
|
|
|
def discover(netdisco):
|
|
|
|
"""Fake discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [
|
|
|
|
(SERVICE_NO_PLATFORM, SERVICE_INFO),
|
|
|
|
(SERVICE_NO_PLATFORM, SERVICE_INFO),
|
|
|
|
]
|
2017-03-03 21:11:40 +00:00
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
mock_discover, mock_platform = yield from mock_discovery(hass, discover)
|
2017-03-03 21:11:40 +00:00
|
|
|
|
|
|
|
assert mock_discover.called
|
|
|
|
assert mock_discover.call_count == 1
|
|
|
|
assert not mock_platform.called
|
|
|
|
mock_discover.assert_called_with(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
SERVICE_NO_PLATFORM,
|
|
|
|
SERVICE_INFO,
|
|
|
|
SERVICE_NO_PLATFORM_COMPONENT,
|
|
|
|
BASE_CONFIG,
|
|
|
|
)
|
2017-04-21 01:45:15 +00:00
|
|
|
|
|
|
|
|
2018-03-30 03:15:40 +00:00
|
|
|
async def test_discover_config_flow(hass):
|
|
|
|
"""Test discovery triggering a config flow."""
|
2019-07-31 19:25:30 +00:00
|
|
|
discovery_info = {"hello": "world"}
|
2018-03-30 03:15:40 +00:00
|
|
|
|
|
|
|
def discover(netdisco):
|
|
|
|
"""Fake discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return [("mock-service", discovery_info)]
|
2018-03-30 03:15:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.dict(
|
|
|
|
discovery.CONFIG_ENTRY_HANDLERS, {"mock-service": "mock-component"}
|
|
|
|
), patch("homeassistant.data_entry_flow.FlowManager.async_init") as m_init:
|
2018-03-30 03:15:40 +00:00
|
|
|
await mock_discovery(hass, discover)
|
|
|
|
|
|
|
|
assert len(m_init.mock_calls) == 1
|
|
|
|
args, kwargs = m_init.mock_calls[0][1:]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert args == ("mock-component",)
|
|
|
|
assert kwargs["context"]["source"] == config_entries.SOURCE_DISCOVERY
|
|
|
|
assert kwargs["data"] == discovery_info
|