Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ Set the Access Point's [**Shorten latency during config**](https://github.com/Op

## Contributing

- Feature requests and bug reports are welcome. Please [open an issue](https://github.com/OpenEPaperLink/Home_Assistant_Integration/issues).
- Feature requests and bug reports are welcome. Please [open an issue](https://github.com/OpenEPaperLink/Home_Assistant_Integration/issues). For a bug, attach the diagnostics from the Access Point or tag's **⋮** menu in **Settings → Devices & services → OpenEPaperLink**. Addresses, MACs and Wi-Fi names are redacted.
- Pull requests are encouraged.
- Join the [OpenEPaperLink Discord](https://discord.com/invite/eRUHt4u5CZ) (`#home_assistant` and `#atc_ble_oepl`) to discuss ideas and get help.
118 changes: 118 additions & 0 deletions custom_components/open_epaper_link/diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""Diagnostics for OpenEPaperLink Access Points and ATC tags."""

from __future__ import annotations

from dataclasses import asdict, is_dataclass
from typing import Any

from homeassistant.components.diagnostics import REDACTED, async_redact_data
from homeassistant.core import HomeAssistant

from . import OpenEPaperLinkConfigEntry, _is_ap_entry
from .atc_coordinator import ATCConfigEntry
from .const import CONF_AP_MAC, CONF_HOST, CONF_MAC_ADDRESS, OPT_BLACKLISTED_TAGS
from .coordinator import APConfigEntry

# Fields that locate the network or the Access Point on it. MACs are handled
# separately, so tags stay distinguishable from one another.
TO_REDACT = {
CONF_HOST,
"ap_ip",
"wifi_ssid",
# A tag's content configuration can carry URLs and tokens for the
# Access Point's own content modes.
"modecfgjson",
}


def _redact_mac(mac: str) -> str:
"""Hide a MAC but keep its last four digits, which tell tags apart."""
clean = mac.replace(":", "").replace("-", "").upper()
return f"{REDACTED}{clean[-4:]}" if clean else REDACTED


def _as_dict(obj: object) -> dict[str, Any] | None:
"""Turn a library model into a dict, without the raw payload it came from.

``raw`` holds every key the device sent, including ones not modelled, so
nothing in it could be redacted by name.
"""
if obj is None or not is_dataclass(obj) or isinstance(obj, type):
return None
data = asdict(obj)
data.pop("raw", None)
return async_redact_data(data, TO_REDACT)


def _entry_info(entry: OpenEPaperLinkConfigEntry) -> dict[str, Any]:
"""Describe the config entry itself."""
data = dict(entry.data)
for key in (CONF_AP_MAC, CONF_MAC_ADDRESS):
if key in data:
data[key] = _redact_mac(str(data[key]))
options = dict(entry.options)
if OPT_BLACKLISTED_TAGS in options:
options[OPT_BLACKLISTED_TAGS] = [
_redact_mac(str(mac)) for mac in options[OPT_BLACKLISTED_TAGS]
]
return {
"title": entry.title,
"version": entry.version,
"minor_version": entry.minor_version,
"data": async_redact_data(data, TO_REDACT),
"options": async_redact_data(options, TO_REDACT),
}


def _ap_diagnostics(entry: APConfigEntry) -> dict[str, Any]:
"""Describe an Access Point and the tags it shows."""
coordinator = entry.runtime_data
tags: dict[str, Any] = {}
for mac, tag in coordinator.data.items():
info = _as_dict(tag) or {}
info["mac"] = _redact_mac(mac)
if info.get("alias", "").replace(":", "").upper() == mac.upper():
info["alias"] = info["mac"]
info["tag_type"] = coordinator.tag_type_name(mac)
info["is_ap_display"] = coordinator.is_ap_display(mac)
tags[info["mac"]] = info
return {
"type": "access_point",
"online": coordinator.online,
"last_update_success": coordinator.last_update_success,
"info": _as_dict(coordinator.ap_info),
"config": _as_dict(coordinator.ap_config),
"status": _as_dict(coordinator.ap_status),
# The Access Point lists every tag in the mesh; this entry shows only
# the ones it owns and the user has not hidden.
"tags_reported": len(coordinator.client.tags),
"tags_shown": len(tags),
"tags": tags,
}


def _atc_diagnostics(entry: ATCConfigEntry) -> dict[str, Any]:
"""Describe an ATC tag from what it reported at setup and advertises."""
runtime = entry.runtime_data
coordinator = runtime.coordinator
update = coordinator.data
return {
"type": "atc",
"available": coordinator.available,
"capabilities": _as_dict(runtime.capabilities),
"device_config": _as_dict(coordinator.device_config),
"advertisement": _as_dict(update.advertisement) if update else None,
"has_rendered_image": coordinator.rendered is not None,
"upload_in_progress": (
runtime.upload_task is not None and not runtime.upload_task.done()
),
}


async def async_get_config_entry_diagnostics(
hass: HomeAssistant, # noqa: ARG001
entry: OpenEPaperLinkConfigEntry,
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
device = _ap_diagnostics(entry) if _is_ap_entry(entry) else _atc_diagnostics(entry)
return {"entry": _entry_info(entry), "device": device}
177 changes: 177 additions & 0 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""Diagnostics downloads for Access Points and ATC tags."""

from __future__ import annotations

import json
from unittest.mock import AsyncMock, MagicMock

from homeassistant.components.bluetooth import BluetoothChange
from homeassistant.core import HomeAssistant
from oepl import APStatus
from py_atc_ble_oepl import DeviceConfig
from pytest_homeassistant.common import MockConfigEntry
from pytest_homeassistant.components.diagnostics import (
get_diagnostics_for_config_entry,
)
from pytest_homeassistant.typing import ClientSessionGenerator

from custom_components.open_epaper_link.const import OPT_BLACKLISTED_TAGS

from .conftest import (
AP_HOST,
AP_MAC,
ATC_ADDRESS,
TAG_MAC,
atc_service_info,
make_tag,
)

OTHER_TAG = "00000335042F3E11"


async def _setup(hass: HomeAssistant, entry: MockConfigEntry) -> None:
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()


async def test_access_point(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
ap_config_entry: MockConfigEntry,
mock_client: AsyncMock,
) -> None:
"""An Access Point's diagnostics describe it and each tag it shows."""
mock_client.set_tags(
make_tag(TAG_MAC, alias="Fridge", modecfgjson='{"url": "http://secret"}'),
make_tag(OTHER_TAG, ap_ip="192.0.2.99"),
)
ap_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
ap_config_entry, options={OPT_BLACKLISTED_TAGS: [OTHER_TAG]}
)
await hass.config_entries.async_setup(ap_config_entry.entry_id)
await hass.async_block_till_done()
coordinator = ap_config_entry.runtime_data
coordinator._handle_ap_status(
APStatus.from_dict({"heap": 1234, "wifissid": "HomeNet", "rssi": -50})
)

diag = await get_diagnostics_for_config_entry(hass, hass_client, ap_config_entry)

entry = diag["entry"]
assert entry["title"] == "Kitchen AP"
assert entry["data"]["host"] == "**REDACTED**"
assert entry["data"]["ap_mac"] == "**REDACTED**EEFF"
assert entry["options"][OPT_BLACKLISTED_TAGS] == ["**REDACTED**3E11"]

device = diag["device"]
assert device["type"] == "access_point"
assert device["online"] is True
assert device["info"]["env"] == "ESP32_S3"
assert device["config"]["alias"] == "Kitchen AP"
assert device["status"]["heap"] == 1234
assert device["status"]["wifi_ssid"] == "**REDACTED**"
assert device["tags_reported"] == 2
# The hidden tag is reported by the Access Point but not shown.
assert device["tags_shown"] == 1
tag = device["tags"]["**REDACTED**3E10"]
assert tag["alias"] == "Fridge"
assert tag["modecfgjson"] == "**REDACTED**"
assert tag["tag_type"] == 'M2 2.9"'
assert tag["is_ap_display"] is False
assert "raw" not in tag

dumped = json.dumps(diag)
for secret in (AP_HOST, AP_MAC, TAG_MAC, OTHER_TAG, "HomeNet", "secret"):
assert secret.lower() not in dumped.lower()


async def test_tag_named_after_its_mac(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
ap_config_entry: MockConfigEntry,
mock_client: AsyncMock,
) -> None:
"""A tag whose alias is its MAC does not give the MAC away through it."""
mock_client.set_tags(make_tag(TAG_MAC, alias=TAG_MAC))
await _setup(hass, ap_config_entry)

diag = await get_diagnostics_for_config_entry(hass, hass_client, ap_config_entry)

assert diag["device"]["tags"]["**REDACTED**3E10"]["alias"] == "**REDACTED**3E10"
assert TAG_MAC not in json.dumps(diag)


async def test_access_point_before_any_status(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
ap_config_entry: MockConfigEntry,
mock_client: AsyncMock,
) -> None:
"""An Access Point that has not sent a status yet still downloads."""
await _setup(hass, ap_config_entry)

diag = await get_diagnostics_for_config_entry(hass, hass_client, ap_config_entry)

assert diag["device"]["status"] is None


async def test_atc_tag(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
atc_config_entry: MockConfigEntry,
mock_atc_tag: MagicMock,
) -> None:
"""An ATC tag's diagnostics carry its geometry, config and advertisement."""
mock_atc_tag.device_config = DeviceConfig(
screen_type=5,
hw_type=0x67,
screen_functions=0,
wh_inverted_ble=False,
wh_inverted=False,
screen_h=200,
screen_w=152,
screen_h_offset=0,
screen_w_offset=0,
screen_colors=3,
black_invert=False,
second_color_invert=False,
epd_enabled=True,
led_enabled=False,
nfc_enabled=False,
flash_enabled=False,
adc_pinout=0,
uart_pinout=0,
)
await _setup(hass, atc_config_entry)
atc_config_entry.runtime_data.coordinator._async_handle_bluetooth_event(
atc_service_info(), BluetoothChange.ADVERTISEMENT
)

diag = await get_diagnostics_for_config_entry(hass, hass_client, atc_config_entry)

assert diag["entry"]["data"]["mac_address"] == "**REDACTED**EE01"
device = diag["device"]
assert device["type"] == "atc"
assert device["capabilities"] == {"width": 152, "height": 200, "color_scheme": 2}
assert device["device_config"]["screen_type"] == 5
assert device["advertisement"]["battery_mv"] == 2828
assert device["has_rendered_image"] is False
assert device["upload_in_progress"] is False
assert ATC_ADDRESS.lower() not in json.dumps(diag).lower()


async def test_atc_tag_before_any_advertisement(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
atc_config_entry: MockConfigEntry,
mock_atc_tag: MagicMock,
) -> None:
"""A tag that has not advertised since setup still downloads."""
await _setup(hass, atc_config_entry)

diag = await get_diagnostics_for_config_entry(hass, hass_client, atc_config_entry)

assert diag["device"]["advertisement"] is None
assert diag["device"]["device_config"] is None
Loading