diff --git a/CHANGELOG.md b/CHANGELOG.md index e63faff..cd483a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add sensor of total printed pages per consumable, available for toner only [#72](https://github.com/elad-bar/ha-hpprinter/issues/72) - Fix HTTPS requests [#160](https://github.com/elad-bar/ha-hpprinter/issues/160) - Remove startup blocking call +- Improved diagnostics file output (general and device level) with more details ## 2.0.2 diff --git a/custom_components/hpprinter/diagnostics.py b/custom_components/hpprinter/diagnostics.py index dc9e0e2..95e3fcc 100644 --- a/custom_components/hpprinter/diagnostics.py +++ b/custom_components/hpprinter/diagnostics.py @@ -10,7 +10,6 @@ from homeassistant.helpers.device_registry import DeviceEntry from .common.consts import DOMAIN -from .managers.ha_coordinator import HACoordinator _LOGGER = logging.getLogger(__name__) @@ -21,32 +20,67 @@ async def async_get_config_entry_diagnostics( """Return diagnostics for a config entry.""" _LOGGER.debug("Starting diagnostic tool") - return await _async_get_diagnostics(hass, 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 await _async_get_diagnostics(hass, entry, device) + return _async_get_diagnostics(hass, entry, device) -async def _async_get_diagnostics( +@callback +def _async_get_diagnostics( hass: HomeAssistant, entry: ConfigEntry, - _device: DeviceEntry | None = None, + device: DeviceEntry | None = None, ) -> dict[str, Any]: """Return diagnostics for a config entry.""" _LOGGER.debug("Getting diagnostic information") - coordinator: HACoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = hass.data[DOMAIN][entry.entry_id] + + debug_data = coordinator.get_debug_data() data = { "disabled_by": entry.disabled_by, "disabled_polling": entry.pref_disable_polling, - "debug": await coordinator.get_debug_data(), + "debug_data": debug_data, } + devices = coordinator.get_devices() + + if device: + current_device_key = [ + device_key + for device_key in devices + if coordinator.get_device(device_key).get("identifiers") + == device.identifiers + ][0] + + device_data = coordinator.get_device_data(current_device_key) + + data |= _async_device_as_dict( + hass, + device.identifiers, + device_data, + ) + + else: + _LOGGER.debug("Getting diagnostic information for all devices") + + data.update( + devices=[ + _async_device_as_dict( + hass, + coordinator.get_device(device_key).get("identifiers"), + coordinator.get_device_data(device_key), + ) + for device_key in devices + ] + ) + return data @@ -54,7 +88,7 @@ async def _async_get_diagnostics( def _async_device_as_dict( hass: HomeAssistant, identifiers, additional_data: dict ) -> dict[str, Any]: - """Represent a Shinobi monitor as a dictionary.""" + """Represent an EdgeOS based device as a dictionary.""" device_registry = dr.async_get(hass) entity_registry = er.async_get(hass) @@ -67,7 +101,7 @@ def _async_device_as_dict( "name_by_user": ha_device.name_by_user, "disabled": ha_device.disabled, "disabled_by": ha_device.disabled_by, - "parameters": additional_data, + "data": additional_data, "entities": [], } diff --git a/custom_components/hpprinter/managers/ha_coordinator.py b/custom_components/hpprinter/managers/ha_coordinator.py index 9b8fec1..17db09a 100644 --- a/custom_components/hpprinter/managers/ha_coordinator.py +++ b/custom_components/hpprinter/managers/ha_coordinator.py @@ -300,9 +300,7 @@ def get_device_value(self, device_key: str, key: str | None): return data - async def get_debug_data(self) -> dict: - await self._api.update_full() - + def get_debug_data(self) -> dict: data = { "rawData": self._api.raw_data, "devicesData": self._api.data, @@ -311,6 +309,9 @@ async def get_debug_data(self) -> dict: return data + def get_devices(self) -> dict[str, DeviceInfo]: + return self._devices + async def _async_update_data(self): try: await self._api.update()