Deprecate hass.components and log warning if used inside custom component (#111508)

* Deprecate @bind_hass and log error if used inside custom component

* Log also when accessing `hass.components`

* Log warning only when `hass.components` is used

* Change version

* Process code review
This commit is contained in:
Jan-Philipp Benecke
2024-02-29 12:25:46 +01:00
committed by GitHub
parent af4771a198
commit bc6b4d01c8
5 changed files with 83 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
"""Test to verify that we can load components."""
import asyncio
from unittest.mock import patch
from unittest.mock import Mock, patch
import pytest
@@ -8,6 +8,7 @@ from homeassistant import loader
from homeassistant.components import http, hue
from homeassistant.components.hue import light as hue_light
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import frame
from .common import MockModule, async_get_persistent_notifications, mock_integration
@@ -287,6 +288,7 @@ async def test_get_integration_custom_component(
) -> None:
"""Test resolving integration."""
integration = await loader.async_get_integration(hass, "test_package")
assert integration.get_component().DOMAIN == "test_package"
assert integration.name == "Test Package"
@@ -1001,3 +1003,33 @@ async def test_config_folder_not_in_path(hass):
# Verify that we are able to load the file with absolute path
import tests.testing_config.check_config_not_in_path # noqa: F401
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."""
mock_integration_frame.filename = (
"/home/paulus/homeassistant/custom_components/demo/light.py"
)
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,
):
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