Skip to content

Commit

Permalink
Log also when accessing hass.components
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbede committed Feb 26, 2024
1 parent de9f80d commit 48eabf7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 28 deletions.
12 changes: 12 additions & 0 deletions homeassistant/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,18 @@ def __getattr__(self, comp_name: str) -> ModuleWrapper:
if component is None:
raise ImportError(f"Unable to load {comp_name}")

from .helpers.frame import report # pylint: disable=import-outside-toplevel

report(
(
f"accesses hass.components.{comp_name}."
" This is deprecated and will stop working in Home Assistant 2024.9, it"
f" should be updated to import functions used from {comp_name} directly"
),
error_if_core=False,
log_custom_component_only=True,
)

wrapped = ModuleWrapper(self._hass, component)
setattr(self, comp_name, wrapped)
return wrapped
Expand Down
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,33 @@ def mock_bleak_scanner_start() -> Generator[MagicMock, None, None]:
yield mock_bleak_scanner_start


@pytest.fixture
def mock_integration_frame() -> Generator[Mock, None, None]:
"""Mock as if we're calling code from inside an integration."""
correct_frame = Mock(
filename="/home/paulus/homeassistant/components/hue/light.py",
lineno="23",
line="self.light.is_on",
)
with patch(
"homeassistant.helpers.frame.extract_stack",
return_value=[
Mock(
filename="/home/paulus/homeassistant/core.py",
lineno="23",
line="do_something()",
),
correct_frame,
Mock(
filename="/home/paulus/aiohue/lights.py",
lineno="2",
line="something()",
),
],
):
yield correct_frame


@pytest.fixture
def mock_bluetooth(
mock_bleak_scanner_start: MagicMock, mock_bluetooth_adapters: None
Expand Down
28 changes: 0 additions & 28 deletions tests/helpers/test_frame.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test the frame helper."""

from collections.abc import Generator
from unittest.mock import ANY, Mock, patch

import pytest
Expand All @@ -9,33 +8,6 @@
from homeassistant.helpers import frame


@pytest.fixture
def mock_integration_frame() -> Generator[Mock, None, None]:
"""Mock as if we're calling code from inside an integration."""
correct_frame = Mock(
filename="/home/paulus/homeassistant/components/hue/light.py",
lineno="23",
line="self.light.is_on",
)
with patch(
"homeassistant.helpers.frame.extract_stack",
return_value=[
Mock(
filename="/home/paulus/homeassistant/core.py",
lineno="23",
line="do_something()",
),
correct_frame,
Mock(
filename="/home/paulus/aiohue/lights.py",
lineno="2",
line="something()",
),
],
):
yield correct_frame


async def test_extract_frame_integration(
caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock
) -> None:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,3 +1032,30 @@ def _test(hass: HomeAssistant):
"Detected that custom integration 'test_integration_frame'"
" uses @bind_hass decorator. This is deprecated"
) in caplog.text


async def test_hass_components_use_reported(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock
) -> None:
"""Test that use of hass.components is reported."""
integration_frame = frame.IntegrationFrame(
custom_integration=True,
frame=mock_integration_frame,
integration="test_integration_frame",
module="custom_components.test_integration_frame",
relative_filename="custom_components/test_integration_frame/__init__.py",
)

with patch(
"homeassistant.helpers.frame.get_integration_frame",
return_value=integration_frame,
), patch(
"homeassistant.components.http.start_http_server_and_save_config",
return_value=None,
):
await hass.components.http.start_http_server_and_save_config(hass, [], None)

assert (
"Detected that custom integration 'test_integration_frame'"
" accesses hass.components.http. This is deprecated"
) in caplog.text

0 comments on commit 48eabf7

Please sign in to comment.