Use the shared zeroconf instance when attempting to create another Zeroconf instance (#38744)
This commit is contained in:
1 parent
34cb12d3c9
commit
444df4a7d2
8 files changed
+187
-17
No files matched your search
@@ -0,0 +1,11 @@
|
||||
"""conftest for zeroconf."""
|
||||
import pytest
|
||||
|
||||
from tests.async_mock import patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_zeroconf():
|
||||
"""Mock zeroconf."""
|
||||
with patch("homeassistant.components.zeroconf.HaZeroconf") as mock_zc:
|
||||
yield mock_zc.return_value
|
||||
@@ -1,5 +1,4 @@
|
||||
"""Test Zeroconf component setup process."""
|
||||
import pytest
|
||||
from zeroconf import InterfaceChoice, IPVersion, ServiceInfo, ServiceStateChange
|
||||
|
||||
from homeassistant.components import zeroconf
|
||||
@@ -22,13 +21,6 @@ HOMEKIT_STATUS_UNPAIRED = b"1"
|
||||
HOMEKIT_STATUS_PAIRED = b"0"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_zeroconf():
|
||||
"""Mock zeroconf."""
|
||||
with patch("homeassistant.components.zeroconf.HaZeroconf") as mock_zc:
|
||||
yield mock_zc.return_value
|
||||
|
||||
|
||||
def service_update_mock(zeroconf, services, handlers):
|
||||
"""Call service update handler."""
|
||||
for service in services:
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Test Zeroconf multiple instance protection."""
|
||||
import zeroconf
|
||||
|
||||
from homeassistant.components.zeroconf import async_get_instance
|
||||
from homeassistant.components.zeroconf.usage import install_multiple_zeroconf_catcher
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
|
||||
|
||||
async def test_multiple_zeroconf_instances(hass, mock_zeroconf, caplog):
|
||||
"""Test creating multiple zeroconf throws without an integration."""
|
||||
|
||||
zeroconf_instance = await async_get_instance(hass)
|
||||
|
||||
install_multiple_zeroconf_catcher(zeroconf_instance)
|
||||
|
||||
new_zeroconf_instance = zeroconf.Zeroconf()
|
||||
assert new_zeroconf_instance == zeroconf_instance
|
||||
|
||||
assert "Zeroconf" in caplog.text
|
||||
|
||||
|
||||
async def test_multiple_zeroconf_instances_gives_shared(hass, mock_zeroconf, caplog):
|
||||
"""Test creating multiple zeroconf gives the shared instance to an integration."""
|
||||
|
||||
zeroconf_instance = await async_get_instance(hass)
|
||||
|
||||
install_multiple_zeroconf_catcher(zeroconf_instance)
|
||||
|
||||
correct_frame = Mock(
|
||||
filename="/config/custom_components/burncpu/light.py",
|
||||
lineno="23",
|
||||
line="self.light.is_on",
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.helpers.frame.extract_stack",
|
||||
return_value=[
|
||||
Mock(
|
||||
filename="/home/dev/homeassistant/core.py",
|
||||
lineno="23",
|
||||
line="do_something()",
|
||||
),
|
||||
correct_frame,
|
||||
Mock(
|
||||
filename="/home/dev/homeassistant/components/zeroconf/usage.py",
|
||||
lineno="23",
|
||||
line="self.light.is_on",
|
||||
),
|
||||
Mock(filename="/home/dev/mdns/lights.py", lineno="2", line="something()",),
|
||||
],
|
||||
):
|
||||
assert zeroconf.Zeroconf() == zeroconf_instance
|
||||
|
||||
assert "custom_components/burncpu/light.py" in caplog.text
|
||||
assert "23" in caplog.text
|
||||
assert "self.light.is_on" in caplog.text
|
||||
Reference in new issue
Block a user