Home Assistant Unofficial Reference 2024.12.1
diagnostics.py
Go to the documentation of this file.
1 """Diagnostics support for generic (IP camera)."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import yarl
8 
9 from homeassistant.components.diagnostics import async_redact_data
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
12 from homeassistant.core import HomeAssistant
13 
14 from .const import CONF_STILL_IMAGE_URL, CONF_STREAM_SOURCE
15 
16 TO_REDACT = {
17  CONF_PASSWORD,
18  CONF_USERNAME,
19 }
20 
21 
22 # A very similar redact function is in components.sql. Possible to be made common.
23 def redact_url(data: str) -> str:
24  """Redact credentials from string url."""
25  url = url_in = yarl.URL(data)
26  # https://github.com/pylint-dev/pylint/issues/3484
27  # pylint: disable-next=using-constant-test
28  if url_in.user:
29  url = url.with_user("****")
30  # pylint: disable-next=using-constant-test
31  if url_in.password:
32  url = url.with_password("****")
33  if url_in.path != "/":
34  url = url.with_path("****")
35  # pylint: disable-next=using-constant-test
36  if url_in.query_string:
37  url = url.with_query("****=****")
38  return str(url)
39 
40 
42  hass: HomeAssistant, entry: ConfigEntry
43 ) -> dict[str, Any]:
44  """Return diagnostics for a config entry."""
45  options = async_redact_data(entry.options, TO_REDACT)
46  for key in (CONF_STREAM_SOURCE, CONF_STILL_IMAGE_URL):
47  if (value := options.get(key)) is not None:
48  options[key] = redact_url(value)
49 
50  return {
51  "title": entry.title,
52  "data": async_redact_data(entry.data, TO_REDACT),
53  "options": options,
54  }
dict async_redact_data(Mapping data, Iterable[Any] to_redact)
Definition: util.py:14
dict[str, Any] async_get_config_entry_diagnostics(HomeAssistant hass, ConfigEntry entry)
Definition: diagnostics.py:43