diagnostics.py 4.2 KB

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