2022-01-20 22:02:47 +00:00
|
|
|
"""Diagnostic utilities."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Iterable, Mapping
|
2022-02-04 22:45:25 +00:00
|
|
|
from typing import Any, TypeVar, cast, overload
|
2022-01-20 22:02:47 +00:00
|
|
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
|
|
|
|
from .const import REDACTED
|
|
|
|
|
2022-01-29 20:30:15 +00:00
|
|
|
T = TypeVar("T")
|
|
|
|
|
2022-01-20 22:02:47 +00:00
|
|
|
|
2022-02-04 22:45:25 +00:00
|
|
|
@overload
|
|
|
|
def async_redact_data(data: Mapping, to_redact: Iterable[Any]) -> dict: # type: ignore
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def async_redact_data(data: T, to_redact: Iterable[Any]) -> T:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2022-01-20 22:02:47 +00:00
|
|
|
@callback
|
2022-01-29 20:30:15 +00:00
|
|
|
def async_redact_data(data: T, to_redact: Iterable[Any]) -> T:
|
2022-01-20 22:02:47 +00:00
|
|
|
"""Redact sensitive data in a dict."""
|
|
|
|
if not isinstance(data, (Mapping, list)):
|
|
|
|
return data
|
|
|
|
|
2022-01-29 20:30:15 +00:00
|
|
|
if isinstance(data, list):
|
|
|
|
return cast(T, [async_redact_data(val, to_redact) for val in data])
|
|
|
|
|
2022-01-20 22:02:47 +00:00
|
|
|
redacted = {**data}
|
|
|
|
|
|
|
|
for key, value in redacted.items():
|
|
|
|
if key in to_redact:
|
|
|
|
redacted[key] = REDACTED
|
2022-02-04 22:45:25 +00:00
|
|
|
elif isinstance(value, Mapping):
|
2022-01-20 22:02:47 +00:00
|
|
|
redacted[key] = async_redact_data(value, to_redact)
|
|
|
|
elif isinstance(value, list):
|
|
|
|
redacted[key] = [async_redact_data(item, to_redact) for item in value]
|
|
|
|
|
2022-01-29 20:30:15 +00:00
|
|
|
return cast(T, redacted)
|