Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the shared zeroconf instance when attempting to create another Zeroconf instance #38744

Merged
merged 9 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions homeassistant/components/zeroconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from homeassistant.helpers.singleton import singleton
from homeassistant.loader import async_get_homekit, async_get_zeroconf

from .usage import install_multiple_zeroconf_catcher

_LOGGER = logging.getLogger(__name__)

DOMAIN = "zeroconf"
Expand Down Expand Up @@ -135,6 +137,8 @@ def setup(hass, config):
ipv6=zc_config.get(CONF_IPV6, DEFAULT_IPV6),
)

install_multiple_zeroconf_catcher(zeroconf)

# Get instance UUID
uuid = asyncio.run_coroutine_threadsafe(
hass.helpers.instance_id.async_get(), hass.loop
Expand Down
50 changes: 50 additions & 0 deletions homeassistant/components/zeroconf/usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Zeroconf usage utility to warn about multiple instances."""

import logging

import zeroconf

from homeassistant.helpers.frame import (
MissingIntegrationFrame,
get_integration_frame,
report_integration,
)

_LOGGER = logging.getLogger(__name__)


def install_multiple_zeroconf_catcher(hass_zc) -> None:
"""Wrap the Zeroconf class to return the shared instance if multiple instances are detected."""

def new_zeroconf_new(self, *k, **kw):
_report(
"attempted to create another Zeroconf instance. Please use the shared Zeroconf via await homeassistant.components.zeroconf.async_get_instance(hass)",
)
return hass_zc

def new_zeroconf_init(self, *k, **kw):
return

zeroconf.Zeroconf.__new__ = new_zeroconf_new
zeroconf.Zeroconf.__init__ = new_zeroconf_init


def _report(what: str) -> None:
"""Report incorrect usage.

Async friendly.
"""
integration_frame = None

try:
integration_frame = get_integration_frame(exclude_integrations={"zeroconf"})
except MissingIntegrationFrame:
pass

if not integration_frame:
_LOGGER.warning(
"Detected code that %s. Please report this issue.", what, stack_info=True
)
return

report_integration(what, integration_frame)
35 changes: 26 additions & 9 deletions homeassistant/helpers/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
import logging
from traceback import FrameSummary, extract_stack
from typing import Any, Callable, Tuple, TypeVar, cast
from typing import Any, Callable, Optional, Tuple, TypeVar, cast

from homeassistant.exceptions import HomeAssistantError

Expand All @@ -12,15 +12,24 @@
CALLABLE_T = TypeVar("CALLABLE_T", bound=Callable) # pylint: disable=invalid-name


def get_integration_frame() -> Tuple[FrameSummary, str, str]:
def get_integration_frame(
exclude_integrations: Optional[set] = None,
) -> Tuple[FrameSummary, str, str]:
"""Return the frame, integration and integration path of the current stack frame."""
found_frame = None
if not exclude_integrations:
exclude_integrations = set()

for frame in reversed(extract_stack()):
for path in ("custom_components/", "homeassistant/components/"):
try:
index = frame.filename.index(path)
found_frame = frame
start = index + len(path)
end = frame.filename.index("/", start)
integration = frame.filename[start:end]
if integration not in exclude_integrations:
found_frame = frame

break
except ValueError:
continue
Expand All @@ -31,11 +40,6 @@ def get_integration_frame() -> Tuple[FrameSummary, str, str]:
if found_frame is None:
raise MissingIntegrationFrame

start = index + len(path)
end = found_frame.filename.index("/", start)

integration = found_frame.filename[start:end]

return found_frame, integration, path


Expand All @@ -49,11 +53,24 @@ def report(what: str) -> None:
Async friendly.
"""
try:
found_frame, integration, path = get_integration_frame()
integration_frame = get_integration_frame()
except MissingIntegrationFrame:
# Did not source from an integration? Hard error.
raise RuntimeError(f"Detected code that {what}. Please report this issue.")

report_integration(what, integration_frame)


def report_integration(
what: str, integration_frame: Tuple[FrameSummary, str, str]
) -> None:
"""Report incorrect usage in an integration.

Async friendly.
"""

found_frame, integration, path = integration_frame

index = found_frame.filename.index(path)
if path == "custom_components/":
extra = " to the custom component author"
Expand Down
7 changes: 7 additions & 0 deletions tests/components/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""Fixtures for component testing."""
import pytest

from homeassistant.components import zeroconf

from tests.async_mock import patch

zeroconf.orig_install_multiple_zeroconf_catcher = (
zeroconf.install_multiple_zeroconf_catcher
)
zeroconf.install_multiple_zeroconf_catcher = lambda zc: None


@pytest.fixture(autouse=True)
def prevent_io():
Expand Down
11 changes: 11 additions & 0 deletions tests/components/zeroconf/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""conftest for zeroconf."""
bdraco marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 0 additions & 8 deletions tests/components/zeroconf/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test Zeroconf component setup process."""
import pytest
from zeroconf import InterfaceChoice, IPVersion, ServiceInfo, ServiceStateChange

from homeassistant.components import zeroconf
Expand All @@ -22,13 +21,6 @@
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:
Expand Down
56 changes: 56 additions & 0 deletions tests/components/zeroconf/test_usage.py
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions tests/helpers/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ async def test_extract_frame_integration(caplog):
assert found_frame == correct_frame


async def test_extract_frame_integration_with_excluded_intergration(caplog):
"""Test extracting the current frame from integration context."""
correct_frame = Mock(
filename="/home/dev/homeassistant/components/mdns/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()",),
],
):
found_frame, integration, path = frame.get_integration_frame(
exclude_integrations={"zeroconf"}
)

assert integration == "mdns"
assert path == "homeassistant/components/"
assert found_frame == correct_frame


async def test_extract_frame_no_integration(caplog):
"""Test extracting the current frame without integration context."""
with patch(
Expand Down