Coverage for custom_components/appliance_autonotify/diagnostics.py: 100%
58 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-28 12:19 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-28 12:19 +0000
1"""What an appliance is, as its integration has it, and what it did through its first cycles, to be attached to an
2issue, as asked for of an appliance of an integration in beta, and for automatic discovery, the devices that look like
3appliances but weren't found to be, to be attached to an issue asking for their integration to be supported"""
5from __future__ import annotations
7from typing import TYPE_CHECKING, Any
9from homeassistant.components.diagnostics import async_redact_data
10from homeassistant.const import CONF_DEVICE_ID, CONF_TYPE
11from homeassistant.helpers import device_registry as dr
12from homeassistant.helpers import entity_registry as er
14from .compat import all_devices
15from .const import CONF_CUSTOM_TARGET, CONF_POWER_ENTITY, CONF_TARGET, CONF_TARGETS, DOMAIN, TYPE_DISCOVERY
16from .discovery import appliance_profile, companions, cycle_entities, integration_name, role_key
17from .trace import CycleTrace
18from .words import name_type
20if TYPE_CHECKING:
21 from homeassistant.config_entries import ConfigEntry
22 from homeassistant.core import HomeAssistant
24 from .watcher import ApplianceWatcher
26# who is notified is nobody's business but the household's
27TO_REDACT = {CONF_TARGET, CONF_CUSTOM_TARGET, CONF_TARGETS}
28# the sensors an appliance's integration tells of its cycle with, its state, when it will finish, or how long it takes
29APPLIANCE_DEVICE_CLASSES = ("enum", "timestamp", "duration")
30# a state of a cycle under way, as an integration yet to be supported might name it, for a device whose name doesn't
31# say it's an appliance
32RUNNING_OPTIONS = ("run", "running", "in_progress", "active", "busy", "washing", "drying", "cleaning")
35async def async_get_config_entry_diagnostics(hass: HomeAssistant, entry: ConfigEntry) -> dict[str, Any]:
36 watcher: ApplianceWatcher | None = getattr(entry, "runtime_data", None)
37 device_ids: list[str] = []
38 if device_id := entry.data.get(CONF_DEVICE_ID):
39 device_ids = [device_id, *companions(hass, device_id)]
40 elif (power := er.async_get(hass).async_get(entry.options.get(CONF_POWER_ENTITY) or "")) and power.device_id:
41 device_ids = [power.device_id]
42 profile = appliance_profile(hass, device_id) if device_id else None
43 trace = watcher.trace if watcher is not None else None
44 if trace is None:
45 # kept from when its integration was in beta, or from before it was disabled
46 trace = CycleTrace(hass, entry.entry_id)
47 await trace.async_load()
48 if entry.data.get(CONF_TYPE) == TYPE_DISCOVERY:
49 return {
50 "entry": {"title": entry.title, "version": f"{entry.version}.{entry.minor_version}"},
51 "unmatched": _unmatched(hass),
52 }
53 return {
54 "entry": {
55 "title": entry.title,
56 "version": f"{entry.version}.{entry.minor_version}",
57 "data": async_redact_data(dict(entry.data), TO_REDACT),
58 "options": async_redact_data(dict(entry.options), TO_REDACT),
59 },
60 "platform": None
61 if profile is None
62 else {
63 "platform": profile.platform,
64 "name": integration_name(hass, profile.platform),
65 "status": profile.status,
66 },
67 "watcher": None if watcher is None else _watcher(watcher),
68 "devices": [_device(hass, device_id) for device_id in device_ids],
69 "trace": trace.as_dict(),
70 }
73def _watcher(watcher: ApplianceWatcher) -> dict[str, Any]:
74 return {
75 "class": type(watcher).__name__,
76 "name": watcher.name,
77 "appliance_type": watcher.appliance_type,
78 "running": watcher.running,
79 "cycle_entity_id": watcher.cycle_entity_id,
80 "fallback_cycle_entity_id": watcher.fallback_cycle_entity_id,
81 "entity_ids": dict(watcher.entity_ids),
82 "fallback_ids": dict(watcher.fallback_ids),
83 }
86def _device(hass: HomeAssistant, device_id: str) -> dict[str, Any]:
87 """The device, and every entity it has as its integration registers it, with what it's taken to do"""
88 device = dr.async_get(hass).async_get(device_id)
89 entities = er.async_entries_for_device(er.async_get(hass), device_id, include_disabled_entities=True)
90 # a device linked to another has none of the details of its own
91 details = device if isinstance(device, dr.DeviceEntry) else None
92 return {
93 "manufacturer": details.manufacturer if details else None,
94 "model": details.model if details else None,
95 "model_id": details.model_id if details else None,
96 "sw_version": details.sw_version if details else None,
97 "identifier_domains": sorted({domain for domain, _ in device.identifiers}) if device else [],
98 "entities": [_entity(hass, entity) for entity in sorted(entities, key=lambda e: e.entity_id)],
99 }
102def _unmatched(hass: HomeAssistant) -> list[dict[str, Any]]:
103 """The devices that look like appliances, by their name or model, or a state of a cycle under way, with a sensor
104 such as an appliance's integration tells of its cycle with, but that weren't found to be one, with no names, areas
105 or entity ids, since they're for a public issue"""
106 matched = set(cycle_entities(hass))
107 configured = {entry.data.get(CONF_DEVICE_ID) for entry in hass.config_entries.async_entries(DOMAIN)}
108 entity_registry = er.async_get(hass)
109 found: list[dict[str, Any]] = []
110 for device in all_devices(dr.async_get(hass)):
111 if device.id in matched or device.id in configured or any(domain == DOMAIN for domain, _ in device.identifiers):
112 continue
113 entities = sorted(er.async_entries_for_device(entity_registry, device.id), key=lambda e: e.entity_id)
114 sensors = [
115 e
116 for e in entities
117 if e.domain == "sensor" and (e.device_class or e.original_device_class) in APPLIANCE_DEVICE_CLASSES
118 ]
119 if not sensors:
120 continue
121 names = " ".join(name for name in (device.name_by_user, device.name, device.model) if name)
122 kind = name_type(hass, names.replace("_", " "))
123 options = {str(option).lower() for e in sensors for option in (e.capabilities or {}).get("options") or []}
124 if kind is None and options.isdisjoint(RUNNING_OPTIONS):
125 continue
126 found.append({
127 "manufacturer": device.manufacturer,
128 "model": device.model,
129 "model_id": device.model_id,
130 "type": kind,
131 "identifier_domains": sorted({domain for domain, _ in device.identifiers}),
132 "entities": [_entity(hass, entity, with_id=False) for entity in entities],
133 })
134 return found
137def _entity(hass: HomeAssistant, entity: er.RegistryEntry, with_id: bool = True) -> dict[str, Any]:
138 state = hass.states.get(entity.entity_id)
139 return {
140 **({"entity_id": entity.entity_id} if with_id else {}),
141 "platform": entity.platform,
142 "domain": entity.domain,
143 "translation_key": entity.translation_key,
144 "role": role_key(entity),
145 "device_class": entity.device_class or entity.original_device_class,
146 "unit": entity.unit_of_measurement,
147 "options": (entity.capabilities or {}).get("options"),
148 "disabled": entity.disabled,
149 "state": state.state if state is not None else None,
150 }