Implement diagnostics
Gold rule diagnostics: users can download diagnostics, with sensitive data redacted.
This is a Gold rule. In quality_scale.yaml, it is called diagnostics.
The integration implements diagnostics: a diagnostics.py with async_get_config_entry_diagnostics, async_get_device_diagnostics, or both. The diagnostics contain the information that helps to debug a problem, like the configuration and the data the integration received. They must never contain sensitive information, like passwords, tokens, or coordinates.
When something does not work, diagnostics let a user download a snapshot of the integration with one selection, and attach it to an issue. Without it, you end up asking for logs, versions, and payloads in several rounds. With it, the first report often contains everything you need.
Users share these files in public issues, so redaction is part of the rule, not an extra.
Redact sensitive keys with async_redact_data before you return the data:
TO_REDACT = {CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: MyConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator = entry.runtime_data
return {
"entry_data": async_redact_data(entry.data, TO_REDACT),
"data": async_redact_data(coordinator.data.to_dict(), TO_REDACT),
}async_redact_data comes from homeassistant.components.diagnostics. It replaces the values of matching keys, also in nested dictionaries and lists.
- Pylint, when the rule is
done:diagnostics.pymust implementasync_get_config_entry_diagnosticsorasync_get_device_diagnostics(home-assistant-missing-diagnostics). - Codecov requires 100% test coverage of the changed lines in
diagnostics.pyof every integration. Most integrations test their diagnostics with a snapshot. - Whether the right data is in, and the sensitive data is out, is checked in review.
If the integration has no data that helps with debugging, for example because it has no runtime data and its configuration contains only sensitive information, this rule does not apply.