Add diagnostics rule to quality_scale hassfest validation (#131859)

This commit is contained in:
epenet
2024-11-28 19:42:31 +01:00
committed by GitHub
parent 1a9ab07742
commit 837716b69e
5 changed files with 56 additions and 12 deletions

View File

@@ -0,0 +1,42 @@
"""Enforce that the integration implements diagnostics.
https://developers.home-assistant.io/docs/core/integration-quality-scale/rules/diagnostics/
"""
import ast
from script.hassfest.model import Integration
DIAGNOSTICS_FUNCTIONS = {
"async_get_config_entry_diagnostics",
"async_get_device_diagnostics",
}
def _has_diagnostics_function(module: ast.Module) -> bool:
"""Test if the module defines at least one of diagnostic functions."""
return any(
type(item) is ast.AsyncFunctionDef and item.name in DIAGNOSTICS_FUNCTIONS
for item in ast.walk(module)
)
def validate(integration: Integration) -> list[str] | None:
"""Validate that the integration implements diagnostics."""
diagnostics_file = integration.path / "diagnostics.py"
if not diagnostics_file.exists():
return [
"Integration does implement diagnostics platform "
"(is missing diagnostics.py)",
]
diagnostics = ast.parse(diagnostics_file.read_text())
if not _has_diagnostics_function(diagnostics):
return [
f"Integration is missing one of {DIAGNOSTICS_FUNCTIONS} "
f"in {diagnostics_file}"
]
return None