diagnostics.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Diagnostics support for tuya-local."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from homeassistant.components.diagnostics import REDACTED
  5. from homeassistant.config_entries import ConfigEntry
  6. from homeassistant.core import HomeAssistant, callback
  7. from homeassistant.helpers import device_registry as dr, entity_registry as er
  8. from homeassistant.helpers.device_registry import DeviceEntry
  9. from .const import (
  10. API_PROTOCOL_VERSIONS,
  11. CONF_PROTOCOL_VERSION,
  12. CONF_TYPE,
  13. DOMAIN,
  14. CONF_DEVICE_CID,
  15. )
  16. from .device import TuyaLocalDevice
  17. from .helpers.config import get_device_id
  18. async def async_get_config_entry_diagnostics(
  19. hass: HomeAssistant, entry: ConfigEntry
  20. ) -> dict[str, Any]:
  21. """Return diagnostics for a config entry."""
  22. return _async_get_diagnostics(hass, entry)
  23. async def async_get_device_diagnostics(
  24. hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
  25. ) -> dict[str, Any]:
  26. """Return diagnostics for a device entry."""
  27. return _async_get_diagnostics(hass, entry, device)
  28. @callback
  29. def _async_get_diagnostics(
  30. hass: HomeAssistant,
  31. entry: ConfigEntry,
  32. device: DeviceEntry | None = None,
  33. ) -> dict[str, Any]:
  34. """Return diagnostics for a tuya-local config entry."""
  35. hass_data = hass.data[DOMAIN][get_device_id(entry.data)]
  36. data = {
  37. "name": entry.title,
  38. "type": entry.data[CONF_TYPE],
  39. "device_id": REDACTED,
  40. "device_cid": REDACTED if entry.data.get(CONF_DEVICE_CID, "") != "" else "",
  41. "local_key": REDACTED,
  42. "host": REDACTED,
  43. "protocol_version": entry.data[CONF_PROTOCOL_VERSION],
  44. }
  45. # The DeviceEntry also has interesting looking data, but this
  46. # integration does not publish anything to it other than some hardcoded
  47. # values that don't change between devices. Instead get the live data
  48. # from the running hass.
  49. data |= _async_device_as_dict(hass, hass_data["device"])
  50. return data
  51. @callback
  52. def _async_device_as_dict(
  53. hass: HomeAssistant, device: TuyaLocalDevice
  54. ) -> dict[str, Any]:
  55. """Represent a Tuya Local device as a dictionary."""
  56. # Base device information, without sensitive information
  57. data = {
  58. "name": device.name,
  59. "api_version_set": device._api.version,
  60. "api_version_used": API_PROTOCOL_VERSIONS[device._api_protocol_version_index],
  61. "api_working": device._api_protocol_working,
  62. "status": device._api.dps_cache,
  63. "cached_state": device._cached_state,
  64. "pending_state": device._pending_updates,
  65. "connected": device._running,
  66. "force_dps": device._force_dps,
  67. }
  68. device_registry = dr.async_get(hass)
  69. entity_registry = er.async_get(hass)
  70. hass_device = device_registry.async_get_device(
  71. identifiers={(DOMAIN, device.unique_id)}
  72. )
  73. if hass_device:
  74. data["home_assistant"] = {
  75. "name": hass_device.name,
  76. "name_by_user": hass_device.name_by_user,
  77. "disabled": hass_device.disabled,
  78. "disabled_by": hass_device.disabled_by,
  79. "entities": [],
  80. }
  81. hass_entities = er.async_entries_for_device(
  82. entity_registry,
  83. device_id=hass_device.id,
  84. include_disabled_entities=True,
  85. )
  86. for entity_entry in hass_entities:
  87. state = hass.states.get(entity_entry.entity_id)
  88. state_dict = None
  89. if state:
  90. state_dict = dict(state.as_dict())
  91. # Redact entity_picture in case it is sensitive
  92. if "entity_picture" in state_dict["attributes"]:
  93. state_dict["attributes"] = {
  94. **state_dict["attributes"],
  95. "entity_picture": REDACTED,
  96. }
  97. # Context is not useful information
  98. state_dict.pop("context", None)
  99. data["home_assistant"]["entities"].append(
  100. {
  101. "disabled": entity_entry.disabled,
  102. "disabled_by": entity_entry.disabled_by,
  103. "entity_category": entity_entry.entity_category,
  104. "device_class": entity_entry.device_class,
  105. "original_device_class": entity_entry.original_device_class,
  106. "icon": entity_entry.icon,
  107. "unit_of_measurement": entity_entry.unit_of_measurement,
  108. "state": state_dict,
  109. }
  110. )
  111. return data