1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-22 02:47:14 +00:00
Files
core/homeassistant/components/traccar_server/diagnostics.py
2025-12-29 22:16:01 +01:00

104 lines
3.1 KiB
Python

"""Diagnostics platform for Traccar Server."""
from __future__ import annotations
from typing import Any
from homeassistant.components.diagnostics import REDACTED, async_redact_data
from homeassistant.const import CONF_ADDRESS, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from .coordinator import TraccarServerConfigEntry, TraccarServerCoordinator
KEYS_TO_REDACT = {
"area", # This is the polygon area of a geofence
CONF_ADDRESS,
CONF_LATITUDE,
CONF_LONGITUDE,
}
def _entity_state(
hass: HomeAssistant,
entity: er.RegistryEntry,
coordinator: TraccarServerCoordinator,
) -> dict[str, Any] | None:
states_to_redact = {x["position"]["address"] for x in coordinator.data.values()}
return (
{
"state": state.state if state.state not in states_to_redact else REDACTED,
"attributes": state.attributes,
}
if (state := hass.states.get(entity.entity_id))
else None
)
async def async_get_config_entry_diagnostics(
hass: HomeAssistant,
config_entry: TraccarServerConfigEntry,
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator = config_entry.runtime_data
entity_registry = er.async_get(hass)
entities = er.async_entries_for_config_entry(
entity_registry,
config_entry_id=config_entry.entry_id,
)
return async_redact_data(
{
"subscription_status": coordinator.client.subscription_status,
"config_entry_options": dict(config_entry.options),
"coordinator_data": coordinator.data,
"entities": [
{
"entity_id": entity.entity_id,
"disabled": entity.disabled,
"unit_of_measurement": entity.unit_of_measurement,
"state": _entity_state(hass, entity, coordinator),
}
for entity in entities
],
},
KEYS_TO_REDACT,
)
async def async_get_device_diagnostics(
hass: HomeAssistant,
entry: TraccarServerConfigEntry,
device: dr.DeviceEntry,
) -> dict[str, Any]:
"""Return device diagnostics."""
coordinator = entry.runtime_data
entity_registry = er.async_get(hass)
entities = er.async_entries_for_device(
entity_registry,
device_id=device.id,
include_disabled_entities=True,
)
await hass.config_entries.async_reload(entry.entry_id)
return async_redact_data(
{
"subscription_status": coordinator.client.subscription_status,
"config_entry_options": dict(entry.options),
"coordinator_data": coordinator.data,
"entities": [
{
"entity_id": entity.entity_id,
"disabled": entity.disabled,
"unit_of_measurement": entity.unit_of_measurement,
"state": _entity_state(hass, entity, coordinator),
}
for entity in entities
],
},
KEYS_TO_REDACT,
)