2020-05-13 07:58:33 +00:00
|
|
|
"""Provide frame helper for finding the current frame context."""
|
2021-03-17 17:34:19 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-07-06 22:58:53 +00:00
|
|
|
import asyncio
|
2021-09-29 14:32:11 +00:00
|
|
|
from collections.abc import Callable
|
2023-10-05 17:52:26 +00:00
|
|
|
from contextlib import suppress
|
2023-10-03 17:21:27 +00:00
|
|
|
from dataclasses import dataclass
|
2020-07-06 22:58:53 +00:00
|
|
|
import functools
|
|
|
|
import logging
|
2023-10-04 19:43:00 +00:00
|
|
|
import sys
|
2020-05-13 07:58:33 +00:00
|
|
|
from traceback import FrameSummary, extract_stack
|
2021-09-29 14:32:11 +00:00
|
|
|
from typing import Any, TypeVar, cast
|
2020-05-13 07:58:33 +00:00
|
|
|
|
2023-10-05 17:52:26 +00:00
|
|
|
from homeassistant.core import HomeAssistant, async_get_hass
|
2020-05-13 07:58:33 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2023-10-05 17:52:26 +00:00
|
|
|
from homeassistant.loader import async_suggest_report_issue
|
2020-05-13 07:58:33 +00:00
|
|
|
|
2020-07-06 22:58:53 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-12-06 23:26:31 +00:00
|
|
|
# Keep track of integrations already reported to prevent flooding
|
|
|
|
_REPORTED_INTEGRATIONS: set[str] = set()
|
|
|
|
|
2022-03-17 18:09:55 +00:00
|
|
|
_CallableT = TypeVar("_CallableT", bound=Callable)
|
2020-07-06 22:58:53 +00:00
|
|
|
|
2020-05-13 07:58:33 +00:00
|
|
|
|
2023-10-04 19:43:00 +00:00
|
|
|
@dataclass(kw_only=True)
|
2023-10-03 17:21:27 +00:00
|
|
|
class IntegrationFrame:
|
|
|
|
"""Integration frame container."""
|
|
|
|
|
|
|
|
custom_integration: bool
|
|
|
|
frame: FrameSummary
|
|
|
|
integration: str
|
2023-10-04 19:43:00 +00:00
|
|
|
module: str | None
|
|
|
|
relative_filename: str
|
2023-10-03 17:21:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_integration_frame(exclude_integrations: set | None = None) -> IntegrationFrame:
|
2020-05-13 07:58:33 +00:00
|
|
|
"""Return the frame, integration and integration path of the current stack frame."""
|
|
|
|
found_frame = None
|
2020-08-12 14:08:33 +00:00
|
|
|
if not exclude_integrations:
|
|
|
|
exclude_integrations = set()
|
2020-05-13 07:58:33 +00:00
|
|
|
|
|
|
|
for frame in reversed(extract_stack()):
|
|
|
|
for path in ("custom_components/", "homeassistant/components/"):
|
|
|
|
try:
|
|
|
|
index = frame.filename.index(path)
|
2020-08-12 14:08:33 +00:00
|
|
|
start = index + len(path)
|
|
|
|
end = frame.filename.index("/", start)
|
|
|
|
integration = frame.filename[start:end]
|
|
|
|
if integration not in exclude_integrations:
|
|
|
|
found_frame = frame
|
|
|
|
|
2020-05-13 07:58:33 +00:00
|
|
|
break
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if found_frame is not None:
|
|
|
|
break
|
|
|
|
|
|
|
|
if found_frame is None:
|
|
|
|
raise MissingIntegrationFrame
|
|
|
|
|
2023-10-04 19:43:00 +00:00
|
|
|
found_module: str | None = None
|
|
|
|
for module, module_obj in dict(sys.modules).items():
|
|
|
|
if not hasattr(module_obj, "__file__"):
|
|
|
|
continue
|
|
|
|
if module_obj.__file__ == found_frame.filename:
|
|
|
|
found_module = module
|
|
|
|
break
|
|
|
|
|
2023-10-03 17:21:27 +00:00
|
|
|
return IntegrationFrame(
|
2023-10-04 19:43:00 +00:00
|
|
|
custom_integration=path == "custom_components/",
|
|
|
|
frame=found_frame,
|
|
|
|
integration=integration,
|
|
|
|
module=found_module,
|
|
|
|
relative_filename=found_frame.filename[index:],
|
2023-10-03 17:21:27 +00:00
|
|
|
)
|
2020-05-13 07:58:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MissingIntegrationFrame(HomeAssistantError):
|
|
|
|
"""Raised when no integration is found in the frame."""
|
2020-07-06 22:58:53 +00:00
|
|
|
|
|
|
|
|
2021-11-20 10:53:04 +00:00
|
|
|
def report(
|
2021-11-23 12:35:53 +00:00
|
|
|
what: str,
|
|
|
|
exclude_integrations: set | None = None,
|
|
|
|
error_if_core: bool = True,
|
|
|
|
level: int = logging.WARNING,
|
2021-11-20 10:53:04 +00:00
|
|
|
) -> None:
|
2020-07-06 22:58:53 +00:00
|
|
|
"""Report incorrect usage.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
|
|
|
try:
|
2021-11-20 10:53:04 +00:00
|
|
|
integration_frame = get_integration_frame(
|
|
|
|
exclude_integrations=exclude_integrations
|
|
|
|
)
|
2020-08-28 11:50:32 +00:00
|
|
|
except MissingIntegrationFrame as err:
|
2021-11-20 10:53:04 +00:00
|
|
|
msg = f"Detected code that {what}. Please report this issue."
|
|
|
|
if error_if_core:
|
|
|
|
raise RuntimeError(msg) from err
|
|
|
|
_LOGGER.warning(msg, stack_info=True)
|
|
|
|
return
|
2020-07-06 22:58:53 +00:00
|
|
|
|
2023-10-03 17:21:27 +00:00
|
|
|
_report_integration(what, integration_frame, level)
|
2020-08-12 14:08:33 +00:00
|
|
|
|
|
|
|
|
2023-10-03 17:21:27 +00:00
|
|
|
def _report_integration(
|
2021-11-23 12:35:53 +00:00
|
|
|
what: str,
|
2023-10-03 17:21:27 +00:00
|
|
|
integration_frame: IntegrationFrame,
|
2021-11-23 12:35:53 +00:00
|
|
|
level: int = logging.WARNING,
|
2020-08-12 14:08:33 +00:00
|
|
|
) -> None:
|
|
|
|
"""Report incorrect usage in an integration.
|
|
|
|
|
|
|
|
Async friendly.
|
|
|
|
"""
|
2023-10-03 17:21:27 +00:00
|
|
|
found_frame = integration_frame.frame
|
2021-12-06 23:26:31 +00:00
|
|
|
# Keep track of integrations already reported to prevent flooding
|
|
|
|
key = f"{found_frame.filename}:{found_frame.lineno}"
|
|
|
|
if key in _REPORTED_INTEGRATIONS:
|
|
|
|
return
|
|
|
|
_REPORTED_INTEGRATIONS.add(key)
|
|
|
|
|
2023-10-05 17:52:26 +00:00
|
|
|
hass: HomeAssistant | None = None
|
|
|
|
with suppress(HomeAssistantError):
|
|
|
|
hass = async_get_hass()
|
|
|
|
report_issue = async_suggest_report_issue(
|
|
|
|
hass,
|
|
|
|
integration_domain=integration_frame.integration,
|
|
|
|
module=integration_frame.module,
|
|
|
|
)
|
2020-07-06 22:58:53 +00:00
|
|
|
|
2021-11-23 12:35:53 +00:00
|
|
|
_LOGGER.log(
|
|
|
|
level,
|
2023-10-05 17:52:26 +00:00
|
|
|
"Detected that %sintegration '%s' %s at %s, line %s: %s, please %s",
|
|
|
|
"custom " if integration_frame.custom_integration else "",
|
2023-10-03 17:21:27 +00:00
|
|
|
integration_frame.integration,
|
2023-10-05 17:52:26 +00:00
|
|
|
what,
|
2023-10-04 19:43:00 +00:00
|
|
|
integration_frame.relative_filename,
|
2020-07-06 22:58:53 +00:00
|
|
|
found_frame.lineno,
|
2022-04-26 14:41:52 +00:00
|
|
|
(found_frame.line or "?").strip(),
|
2023-10-05 17:52:26 +00:00
|
|
|
report_issue,
|
2020-07-06 22:58:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-03-17 18:09:55 +00:00
|
|
|
def warn_use(func: _CallableT, what: str) -> _CallableT:
|
2020-07-06 22:58:53 +00:00
|
|
|
"""Mock a function to warn when it was about to be used."""
|
|
|
|
if asyncio.iscoroutinefunction(func):
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
async def report_use(*args: Any, **kwargs: Any) -> None:
|
|
|
|
report(what)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def report_use(*args: Any, **kwargs: Any) -> None:
|
|
|
|
report(what)
|
|
|
|
|
2022-03-17 18:09:55 +00:00
|
|
|
return cast(_CallableT, report_use)
|