[core] Add a DOMAIN constant to every component (#20395)

This commit is contained in:
Jesse Hills
2026-10-08 14:04:26 -10:00
committed by GitHub
parent b4bd95b2ac
commit e03db60a52
729 changed files with 863 additions and 3 deletions
+35
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import argparse
import ast
import codecs
import collections
from collections.abc import Iterator
@@ -811,6 +812,40 @@ def lint_const_py_frozen(fname, content):
return None
@lint_content_check(include=["esphome/components/*/__init__.py"])
def lint_component_domain(fname: Path, content: str) -> str | None:
"""Require every component's __init__.py to define DOMAIN as its own name."""
if len(fname.parts) != 4:
# Platform packages such as esphome/components/<name>/sensor/__init__.py
return None
domain = fname.parts[2]
expected = f'DOMAIN = "{domain}"'
try:
tree = ast.parse(content, filename=str(fname))
except SyntaxError:
# Reported by the Python linters; nothing useful to add here.
return None
for node in tree.body:
if isinstance(node, ast.Assign):
targets = node.targets
elif isinstance(node, ast.AnnAssign):
targets = [node.target]
else:
continue
if (
any(isinstance(t, ast.Name) and t.id == "DOMAIN" for t in targets)
and isinstance(node.value, ast.Constant)
and node.value.value == domain
):
return None
return (
f"Component is missing the {highlight(expected)} constant. "
"Add it in alphabetical order with the other component metadata such as "
"CODEOWNERS and DEPENDENCIES, so other code can refer to the component by "
"name, e.g. CORE.data[DOMAIN]."
)
def relative_cpp_search_text(fname: Path, content) -> str:
parts = fname.parts
integration = parts[2]