diff --git a/homeassistant/components/tuya/diagnostics.py b/homeassistant/components/tuya/diagnostics.py index b9d5036f71c..9a37478cfbd 100644 --- a/homeassistant/components/tuya/diagnostics.py +++ b/homeassistant/components/tuya/diagnostics.py @@ -11,6 +11,7 @@ from homeassistant.components.diagnostics import REDACTED from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.util import dt as dt_util from . import HomeAssistantTuyaData @@ -26,6 +27,23 @@ from .const import ( async def async_get_config_entry_diagnostics( hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + return _async_get_diagnostics(hass, entry) + + +async def async_get_device_diagnostics( + hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry +) -> dict[str, Any]: + """Return diagnostics for a device entry.""" + return _async_get_diagnostics(hass, entry, device) + + +@callback +def _async_get_diagnostics( + hass: HomeAssistant, + entry: ConfigEntry, + device: DeviceEntry | None = None, ) -> dict[str, Any]: """Return diagnostics for a config entry.""" hass_data: HomeAssistantTuyaData = hass.data[DOMAIN][entry.entry_id] @@ -34,7 +52,7 @@ async def async_get_config_entry_diagnostics( if hass_data.home_manager.mq.client: mqtt_connected = hass_data.home_manager.mq.client.is_connected() - return { + data = { "endpoint": entry.data[CONF_ENDPOINT], "auth_type": entry.data[CONF_AUTH_TYPE], "country_code": entry.data[CONF_COUNTRY_CODE], @@ -42,12 +60,23 @@ async def async_get_config_entry_diagnostics( "mqtt_connected": mqtt_connected, "disabled_by": entry.disabled_by, "disabled_polling": entry.pref_disable_polling, - "devices": [ - _async_device_as_dict(hass, device) - for device in hass_data.device_manager.device_map.values() - ], } + if device: + tuya_device_id = next(iter(device.identifiers))[1] + data |= _async_device_as_dict( + hass, hass_data.device_manager.device_map[tuya_device_id] + ) + else: + data.update( + devices=[ + _async_device_as_dict(hass, device) + for device in hass_data.device_manager.device_map.values() + ] + ) + + return data + @callback def _async_device_as_dict(hass: HomeAssistant, device: TuyaDevice) -> dict[str, Any]: