Skip to content

Commit 0ff9649

Browse files
committed
Add the ability to redact webhook in diagnostics
1 parent 8327364 commit 0ff9649

File tree

3 files changed

+1293
-3
lines changed

3 files changed

+1293
-3
lines changed

custom_components/smartcar/diagnostics.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Diagnostics support for Smartcar."""
22

3+
from http import HTTPStatus
4+
import json
35
from typing import Any, cast
46

57
from homeassistant.components.diagnostics import async_redact_data
@@ -36,11 +38,28 @@ async def async_get_config_entry_diagnostics(
3638
entry: ConfigEntry,
3739
) -> dict[str, Any]:
3840
"""Return diagnostics for a config entry."""
39-
meta_coordinator = entry.runtime_data.meta_coordinator
4041
coordinators: dict[str, SmartcarVehicleCoordinator] = (
4142
entry.runtime_data.coordinators
4243
)
4344

45+
meta_coordinator = entry.runtime_data.meta_coordinator
46+
metadata = {**meta_coordinator.data}
47+
48+
if "last_webhook_request" in metadata:
49+
include_raw = False
50+
response = metadata.get("last_webhook_response", {})
51+
response_status = response.get("status")
52+
request = metadata.pop("last_webhook_request")
53+
include_raw = response_status == HTTPStatus.UNAUTHORIZED
54+
55+
try:
56+
metadata["last_webhook_request"] = json.loads(request)
57+
except json.JSONDecodeError:
58+
include_raw = True
59+
60+
if include_raw:
61+
metadata["last_webhook_request_raw"] = request
62+
4463
return cast(
4564
"dict[str, Any]",
4665
async_redact_data(
@@ -55,7 +74,7 @@ async def async_get_config_entry_diagnostics(
5574
coordinator_name: coordinator.data
5675
for coordinator_name, coordinator in coordinators.items()
5776
},
58-
"metadata": meta_coordinator.data,
77+
"metadata": metadata,
5978
},
6079
TO_REDACT,
6180
),

0 commit comments

Comments
 (0)