Skip to content

Commit

Permalink
Merge pull request #167 from elad-bar/improve-diagnostics-file
Browse files Browse the repository at this point in the history
improved diagnostics file output
  • Loading branch information
elad-bar authored Jun 20, 2024
2 parents 7ed8833 + 0081d15 commit 0ede2bd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 43 additions & 9 deletions custom_components/hpprinter/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -21,40 +20,75 @@ 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


@callback
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)

Expand All @@ -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": [],
}

Expand Down
7 changes: 4 additions & 3 deletions custom_components/hpprinter/managers/ha_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 0ede2bd

Please sign in to comment.