Skip to content

Commit

Permalink
Fix diagnostic file creation process & redact sensitive details
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Aug 31, 2023
1 parent 1e71f06 commit e863f4e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## v3.0.26

- Change API update from 30 seconds to 5 minutes, will update also when action is being performed
- Redact sensitive details from diagnostic file
- Fix diagnostic file creation process

## v3.0.25

Expand Down
9 changes: 9 additions & 0 deletions custom_components/aqua_temp/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
DEVICE_CONTROL_VALUE = "value"
DEVICE_CONTROL_PARAM = "param"

TO_REDACT = [
"userId",
"user_id",
"userName",
"user_name",
"accessKey",
HTTP_HEADER_X_TOKEN,
]


class ProductParameter(StrEnum):
MAPPING = "mapping"
Expand Down
29 changes: 18 additions & 11 deletions custom_components/aqua_temp/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
import logging
from typing import Any

from homeassistant.components.diagnostics import async_redact_data
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 .common.api_types import APIParam
from .common.consts import (
DATA_ITEM_CONFIG,
DATA_ITEM_DEVICES,
DATA_ITEM_LOGIN_DETAILS,
DOMAIN,
TO_REDACT,
)
from .managers.aqua_temp_coordinator import AquaTempCoordinator

Expand Down Expand Up @@ -50,33 +53,37 @@ def _async_get_diagnostics(
"""Return diagnostics for a config entry."""
_LOGGER.debug("Getting diagnostic information")

debug_data = coordinator.get_debug_data()
debug_data = async_redact_data(coordinator.get_debug_data(), TO_REDACT)

devices = debug_data.get(DATA_ITEM_DEVICES)
config_data = debug_data.get(DATA_ITEM_CONFIG)
login_details = debug_data.get(DATA_ITEM_LOGIN_DETAILS)

device_id_param = coordinator.config_manager.get_api_param(APIParam.DeviceId)

data = {
DATA_ITEM_LOGIN_DETAILS: login_details,
DATA_ITEM_CONFIG: config_data,
"disabled_by": entry.disabled_by,
"disabled_polling": entry.pref_disable_polling,
}

if device:
if device is not None:
device_id = next(iter(device.identifiers))[1]

for device_code in devices:
device_details = devices[device_code]
if device_details.get("device_id") == device_id:
if device_details.get(device_id_param) == device_id:
data |= _async_device_as_dict(hass, device_details)

else:
_LOGGER.debug("Getting diagnostic information for all devices")

data.update(
devices=[
_async_device_as_dict(hass, devices[device_code])
_async_device_as_dict(
hass, devices[device_code][device_id_param], devices[device_code]
)
for device_code in devices
]
)
Expand All @@ -85,23 +92,23 @@ def _async_get_diagnostics(


@callback
def _async_device_as_dict(hass: HomeAssistant, device_data: dict) -> dict[str, Any]:
def _async_device_as_dict(
hass: HomeAssistant, device_id: str, device_data: dict
) -> dict[str, Any]:
"""Represent a Shinobi monitor as a dictionary."""
device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)

unique_id = device_data.get("device_id")

ha_device = device_registry.async_get_device(identifiers={(DOMAIN, unique_id)})
ha_device = device_registry.async_get_device(identifiers={(DOMAIN, device_id)})
data = {}

if ha_device:
data["home_assistant"] = {
data["device"] = {
"name": ha_device.name,
"name_by_user": ha_device.name_by_user,
"disabled": ha_device.disabled,
"disabled_by": ha_device.disabled_by,
"parameters": device_data,
"data": device_data,
"entities": [],
}

Expand All @@ -120,7 +127,7 @@ def _async_device_as_dict(hass: HomeAssistant, device_data: dict) -> dict[str, A
# The context doesn't provide useful information in this case.
state_dict.pop("context", None)

data["home_assistant"]["entities"].append(
data["device"]["entities"].append(
{
"disabled": entity_entry.disabled,
"disabled_by": entity_entry.disabled_by,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/aqua_temp/managers/aqua_temp_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_debug_data(self) -> dict:
config_data = self._config_manager.get_debug_data()

data = {
DATA_ITEM_DEVICES: self._api.devices,
DATA_ITEM_DEVICES: self.devices,
DATA_ITEM_CONFIG: config_data,
DATA_ITEM_LOGIN_DETAILS: self._api.login_details,
}
Expand All @@ -115,7 +115,7 @@ async def _async_update_data(self):
config_data = self._config_manager.get_debug_data()

return {
DATA_ITEM_DEVICES: self._api.devices,
DATA_ITEM_DEVICES: self.devices,
DATA_ITEM_LOGIN_DETAILS: self._api.login_details,
DATA_ITEM_CONFIG: config_data,
}
Expand Down

0 comments on commit e863f4e

Please sign in to comment.