1 """Diagnostics support for Tuya."""
3 from __future__
import annotations
5 from contextlib
import suppress
7 from typing
import Any, cast
9 from tuya_sharing
import CustomerDevice
17 from .
import TuyaConfigEntry
18 from .const
import DOMAIN, DPCode
22 hass: HomeAssistant, entry: TuyaConfigEntry
24 """Return diagnostics for a config entry."""
29 hass: HomeAssistant, entry: TuyaConfigEntry, device: DeviceEntry
31 """Return diagnostics for a device entry."""
38 entry: TuyaConfigEntry,
39 device: DeviceEntry |
None =
None,
41 """Return diagnostics for a config entry."""
42 hass_data = entry.runtime_data
45 if hass_data.manager.mq.client:
46 mqtt_connected = hass_data.manager.mq.client.is_connected()
49 "endpoint": hass_data.manager.customer_api.endpoint,
50 "terminal_id": hass_data.manager.terminal_id,
51 "mqtt_connected": mqtt_connected,
52 "disabled_by": entry.disabled_by,
53 "disabled_polling": entry.pref_disable_polling,
57 tuya_device_id = next(iter(device.identifiers))[1]
59 hass, hass_data.manager.device_map[tuya_device_id]
65 for device
in hass_data.manager.device_map.values()
74 hass: HomeAssistant, device: CustomerDevice
76 """Represent a Tuya device as a dictionary."""
82 "category": device.category,
83 "product_id": device.product_id,
84 "product_name": device.product_name,
85 "online": device.online,
87 "time_zone": device.time_zone,
88 "active_time": dt_util.utc_from_timestamp(device.active_time).isoformat(),
89 "create_time": dt_util.utc_from_timestamp(device.create_time).isoformat(),
90 "update_time": dt_util.utc_from_timestamp(device.update_time).isoformat(),
95 "set_up": device.set_up,
96 "support_local": device.support_local,
100 for dpcode, value
in device.status.items():
102 if dpcode
in {DPCode.ALARM_MESSAGE, DPCode.MOVEMENT_DETECT_PIC}:
103 data[
"status"][dpcode] = REDACTED
106 with suppress(ValueError, TypeError):
107 value = json.loads(value)
108 data[
"status"][dpcode] = value
111 for function
in device.function.values():
112 value = function.values
113 with suppress(ValueError, TypeError, AttributeError):
114 value = json.loads(cast(str, function.values))
116 data[
"function"][function.code] = {
117 "type": function.type,
122 for status_range
in device.status_range.values():
123 value = status_range.values
124 with suppress(ValueError, TypeError, AttributeError):
125 value = json.loads(status_range.values)
127 data[
"status_range"][status_range.code] = {
128 "type": status_range.type,
133 device_registry = dr.async_get(hass)
134 entity_registry = er.async_get(hass)
135 hass_device = device_registry.async_get_device(identifiers={(DOMAIN, device.id)})
137 data[
"home_assistant"] = {
138 "name": hass_device.name,
139 "name_by_user": hass_device.name_by_user,
140 "disabled": hass_device.disabled,
141 "disabled_by": hass_device.disabled_by,
145 hass_entities = er.async_entries_for_device(
147 device_id=hass_device.id,
148 include_disabled_entities=
True,
151 for entity_entry
in hass_entities:
152 state = hass.states.get(entity_entry.entity_id)
153 state_dict: dict[str, Any] |
None =
None
155 state_dict =
dict(state.as_dict())
158 if "entity_picture" in state_dict[
"attributes"]:
159 state_dict[
"attributes"] = {
160 **state_dict[
"attributes"],
161 "entity_picture": REDACTED,
165 state_dict.pop(
"context",
None)
167 data[
"home_assistant"][
"entities"].append(
169 "disabled": entity_entry.disabled,
170 "disabled_by": entity_entry.disabled_by,
171 "entity_category": entity_entry.entity_category,
172 "device_class": entity_entry.device_class,
173 "original_device_class": entity_entry.original_device_class,
174 "icon": entity_entry.icon,
175 "original_icon": entity_entry.original_icon,
176 "unit_of_measurement": entity_entry.unit_of_measurement,
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, TuyaConfigEntry entry)
dict[str, Any] _async_get_diagnostics(HomeAssistant hass, TuyaConfigEntry entry, DeviceEntry|None device=None)
dict[str, Any] async_get_device_diagnostics(HomeAssistant hass, TuyaConfigEntry entry, DeviceEntry device)
dict[str, Any] _async_device_as_dict(HomeAssistant hass, CustomerDevice device)