Add zeroconf/homekit/ssdp discovery support for custom components (#38466)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
1ebc420c75
commit
d0d0403664
5 files changed
+193
-37
No files matched your search
@@ -6,18 +6,17 @@ import aiohttp
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import ssdp
|
||||
from homeassistant.generated import ssdp as gn_ssdp
|
||||
|
||||
from tests.common import mock_coro
|
||||
|
||||
|
||||
async def test_scan_match_st(hass):
|
||||
"""Test matching based on ST."""
|
||||
scanner = ssdp.Scanner(hass)
|
||||
scanner = ssdp.Scanner(hass, {"mock-domain": [{"st": "mock-st"}]})
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan", return_value=[Mock(st="mock-st", location=None)]
|
||||
), patch.dict(gn_ssdp.SSDP, {"mock-domain": [{"st": "mock-st"}]}), patch.object(
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init", return_value=mock_coro()
|
||||
) as mock_init:
|
||||
await scanner.async_scan(None)
|
||||
@@ -42,12 +41,12 @@ async def test_scan_match_upnp_devicedesc(hass, aioclient_mock, key):
|
||||
</root>
|
||||
""",
|
||||
)
|
||||
scanner = ssdp.Scanner(hass)
|
||||
scanner = ssdp.Scanner(hass, {"mock-domain": [{key: "Paulus"}]})
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
return_value=[Mock(st="mock-st", location="http://1.1.1.1")],
|
||||
), patch.dict(gn_ssdp.SSDP, {"mock-domain": [{key: "Paulus"}]}), patch.object(
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init", return_value=mock_coro()
|
||||
) as mock_init:
|
||||
await scanner.async_scan(None)
|
||||
@@ -69,13 +68,8 @@ async def test_scan_not_all_present(hass, aioclient_mock):
|
||||
</root>
|
||||
""",
|
||||
)
|
||||
scanner = ssdp.Scanner(hass)
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
return_value=[Mock(st="mock-st", location="http://1.1.1.1")],
|
||||
), patch.dict(
|
||||
gn_ssdp.SSDP,
|
||||
scanner = ssdp.Scanner(
|
||||
hass,
|
||||
{
|
||||
"mock-domain": [
|
||||
{
|
||||
@@ -84,6 +78,11 @@ async def test_scan_not_all_present(hass, aioclient_mock):
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
return_value=[Mock(st="mock-st", location="http://1.1.1.1")],
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init", return_value=mock_coro()
|
||||
) as mock_init:
|
||||
@@ -105,13 +104,8 @@ async def test_scan_not_all_match(hass, aioclient_mock):
|
||||
</root>
|
||||
""",
|
||||
)
|
||||
scanner = ssdp.Scanner(hass)
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
return_value=[Mock(st="mock-st", location="http://1.1.1.1")],
|
||||
), patch.dict(
|
||||
gn_ssdp.SSDP,
|
||||
scanner = ssdp.Scanner(
|
||||
hass,
|
||||
{
|
||||
"mock-domain": [
|
||||
{
|
||||
@@ -120,6 +114,11 @@ async def test_scan_not_all_match(hass, aioclient_mock):
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
return_value=[Mock(st="mock-st", location="http://1.1.1.1")],
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init", return_value=mock_coro()
|
||||
) as mock_init:
|
||||
@@ -132,7 +131,7 @@ async def test_scan_not_all_match(hass, aioclient_mock):
|
||||
async def test_scan_description_fetch_fail(hass, aioclient_mock, exc):
|
||||
"""Test failing to fetch description."""
|
||||
aioclient_mock.get("http://1.1.1.1", exc=exc)
|
||||
scanner = ssdp.Scanner(hass)
|
||||
scanner = ssdp.Scanner(hass, {})
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
@@ -149,7 +148,7 @@ async def test_scan_description_parse_fail(hass, aioclient_mock):
|
||||
<root>INVALIDXML
|
||||
""",
|
||||
)
|
||||
scanner = ssdp.Scanner(hass)
|
||||
scanner = ssdp.Scanner(hass, {})
|
||||
|
||||
with patch(
|
||||
"netdisco.ssdp.scan",
|
||||
|
||||
@@ -168,10 +168,36 @@ def test_integration_properties(hass):
|
||||
"domain": "hue",
|
||||
"dependencies": ["test-dep"],
|
||||
"requirements": ["test-req==1.0.0"],
|
||||
"zeroconf": ["_hue._tcp.local."],
|
||||
"homekit": {"models": ["BSB002"]},
|
||||
"ssdp": [
|
||||
{
|
||||
"manufacturer": "Royal Philips Electronics",
|
||||
"modelName": "Philips hue bridge 2012",
|
||||
},
|
||||
{
|
||||
"manufacturer": "Royal Philips Electronics",
|
||||
"modelName": "Philips hue bridge 2015",
|
||||
},
|
||||
{"manufacturer": "Signify", "modelName": "Philips hue bridge 2015"},
|
||||
],
|
||||
},
|
||||
)
|
||||
assert integration.name == "Philips Hue"
|
||||
assert integration.domain == "hue"
|
||||
assert integration.homekit == {"models": ["BSB002"]}
|
||||
assert integration.zeroconf == ["_hue._tcp.local."]
|
||||
assert integration.ssdp == [
|
||||
{
|
||||
"manufacturer": "Royal Philips Electronics",
|
||||
"modelName": "Philips hue bridge 2012",
|
||||
},
|
||||
{
|
||||
"manufacturer": "Royal Philips Electronics",
|
||||
"modelName": "Philips hue bridge 2015",
|
||||
},
|
||||
{"manufacturer": "Signify", "modelName": "Philips hue bridge 2015"},
|
||||
]
|
||||
assert integration.dependencies == ["test-dep"]
|
||||
assert integration.requirements == ["test-req==1.0.0"]
|
||||
assert integration.is_built_in is True
|
||||
@@ -188,6 +214,9 @@ def test_integration_properties(hass):
|
||||
},
|
||||
)
|
||||
assert integration.is_built_in is False
|
||||
assert integration.homekit is None
|
||||
assert integration.zeroconf is None
|
||||
assert integration.ssdp is None
|
||||
|
||||
|
||||
async def test_integrations_only_once(hass):
|
||||
@@ -217,6 +246,9 @@ def _get_test_integration(hass, name, config_flow):
|
||||
"config_flow": config_flow,
|
||||
"dependencies": [],
|
||||
"requirements": [],
|
||||
"zeroconf": [f"_{name}._tcp.local."],
|
||||
"homekit": {"models": [name]},
|
||||
"ssdp": [{"manufacturer": name, "modelName": name}],
|
||||
},
|
||||
)
|
||||
|
||||
@@ -254,6 +286,51 @@ async def test_get_config_flows(hass):
|
||||
assert "test_1" not in flows
|
||||
|
||||
|
||||
async def test_get_zeroconf(hass):
|
||||
"""Verify that custom components with zeroconf are found."""
|
||||
test_1_integration = _get_test_integration(hass, "test_1", True)
|
||||
test_2_integration = _get_test_integration(hass, "test_2", True)
|
||||
|
||||
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
||||
mock_get.return_value = {
|
||||
"test_1": test_1_integration,
|
||||
"test_2": test_2_integration,
|
||||
}
|
||||
zeroconf = await loader.async_get_zeroconf(hass)
|
||||
assert zeroconf["_test_1._tcp.local."] == ["test_1"]
|
||||
assert zeroconf["_test_2._tcp.local."] == ["test_2"]
|
||||
|
||||
|
||||
async def test_get_homekit(hass):
|
||||
"""Verify that custom components with homekit are found."""
|
||||
test_1_integration = _get_test_integration(hass, "test_1", True)
|
||||
test_2_integration = _get_test_integration(hass, "test_2", True)
|
||||
|
||||
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
||||
mock_get.return_value = {
|
||||
"test_1": test_1_integration,
|
||||
"test_2": test_2_integration,
|
||||
}
|
||||
homekit = await loader.async_get_homekit(hass)
|
||||
assert homekit["test_1"] == "test_1"
|
||||
assert homekit["test_2"] == "test_2"
|
||||
|
||||
|
||||
async def test_get_ssdp(hass):
|
||||
"""Verify that custom components with ssdp are found."""
|
||||
test_1_integration = _get_test_integration(hass, "test_1", True)
|
||||
test_2_integration = _get_test_integration(hass, "test_2", True)
|
||||
|
||||
with patch("homeassistant.loader.async_get_custom_components") as mock_get:
|
||||
mock_get.return_value = {
|
||||
"test_1": test_1_integration,
|
||||
"test_2": test_2_integration,
|
||||
}
|
||||
ssdp = await loader.async_get_ssdp(hass)
|
||||
assert ssdp["test_1"] == [{"manufacturer": "test_1", "modelName": "test_1"}]
|
||||
assert ssdp["test_2"] == [{"manufacturer": "test_2", "modelName": "test_2"}]
|
||||
|
||||
|
||||
async def test_get_custom_components_safe_mode(hass):
|
||||
"""Test that we get empty custom components in safe mode."""
|
||||
hass.config.safe_mode = True
|
||||
|
||||
Reference in new issue
Block a user