mirror of
https://github.com/home-assistant/core.git
synced 2026-04-21 00:49:54 +02:00
Add diagnostics platform to AWS S3 (#164118)
Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com> Co-authored-by: Erwin Douna <e.douna@gmail.com>
This commit is contained in:
55
homeassistant/components/aws_s3/diagnostics.py
Normal file
55
homeassistant/components/aws_s3/diagnostics.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""Diagnostics support for AWS S3."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.backup import (
|
||||
DATA_MANAGER as BACKUP_DATA_MANAGER,
|
||||
BackupManager,
|
||||
)
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import (
|
||||
CONF_ACCESS_KEY_ID,
|
||||
CONF_BUCKET,
|
||||
CONF_PREFIX,
|
||||
CONF_SECRET_ACCESS_KEY,
|
||||
DOMAIN,
|
||||
)
|
||||
from .coordinator import S3ConfigEntry
|
||||
from .helpers import async_list_backups_from_s3
|
||||
|
||||
TO_REDACT = (CONF_ACCESS_KEY_ID, CONF_SECRET_ACCESS_KEY)
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
entry: S3ConfigEntry,
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
backup_manager: BackupManager = hass.data[BACKUP_DATA_MANAGER]
|
||||
backups = await async_list_backups_from_s3(
|
||||
coordinator.client,
|
||||
bucket=entry.data[CONF_BUCKET],
|
||||
prefix=entry.data.get(CONF_PREFIX, ""),
|
||||
)
|
||||
|
||||
data = {
|
||||
"coordinator_data": dataclasses.asdict(coordinator.data),
|
||||
"config": {
|
||||
**entry.data,
|
||||
**entry.options,
|
||||
},
|
||||
"backup_agents": [
|
||||
{"name": agent.name}
|
||||
for agent in backup_manager.backup_agents.values()
|
||||
if agent.domain == DOMAIN
|
||||
],
|
||||
"backup": [backup.as_dict() for backup in backups],
|
||||
}
|
||||
|
||||
return async_redact_data(data, TO_REDACT)
|
||||
@@ -45,7 +45,7 @@ rules:
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
diagnostics: todo
|
||||
diagnostics: done
|
||||
discovery-update-info:
|
||||
status: exempt
|
||||
comment: S3 is a cloud service that is not discovered on the network.
|
||||
|
||||
73
tests/components/aws_s3/snapshots/test_diagnostics.ambr
Normal file
73
tests/components/aws_s3/snapshots/test_diagnostics.ambr
Normal file
@@ -0,0 +1,73 @@
|
||||
# serializer version: 1
|
||||
# name: test_entry_diagnostics[large]
|
||||
dict({
|
||||
'backup': list([
|
||||
dict({
|
||||
'addons': list([
|
||||
]),
|
||||
'backup_id': '23e64aec',
|
||||
'database_included': True,
|
||||
'date': '2024-11-22T11:48:48.727189+01:00',
|
||||
'extra_metadata': dict({
|
||||
}),
|
||||
'folders': list([
|
||||
]),
|
||||
'homeassistant_included': True,
|
||||
'homeassistant_version': '2024.12.0.dev0',
|
||||
'name': 'Core 2024.12.0.dev0',
|
||||
'protected': False,
|
||||
'size': 20971520,
|
||||
}),
|
||||
]),
|
||||
'backup_agents': list([
|
||||
dict({
|
||||
'name': 'test',
|
||||
}),
|
||||
]),
|
||||
'config': dict({
|
||||
'access_key_id': '**REDACTED**',
|
||||
'bucket': 'test',
|
||||
'endpoint_url': 'https://s3.eu-south-1.amazonaws.com',
|
||||
'secret_access_key': '**REDACTED**',
|
||||
}),
|
||||
'coordinator_data': dict({
|
||||
'all_backups_size': 20971520,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_entry_diagnostics[small]
|
||||
dict({
|
||||
'backup': list([
|
||||
dict({
|
||||
'addons': list([
|
||||
]),
|
||||
'backup_id': '23e64aec',
|
||||
'database_included': True,
|
||||
'date': '2024-11-22T11:48:48.727189+01:00',
|
||||
'extra_metadata': dict({
|
||||
}),
|
||||
'folders': list([
|
||||
]),
|
||||
'homeassistant_included': True,
|
||||
'homeassistant_version': '2024.12.0.dev0',
|
||||
'name': 'Core 2024.12.0.dev0',
|
||||
'protected': False,
|
||||
'size': 1048576,
|
||||
}),
|
||||
]),
|
||||
'backup_agents': list([
|
||||
dict({
|
||||
'name': 'test',
|
||||
}),
|
||||
]),
|
||||
'config': dict({
|
||||
'access_key_id': '**REDACTED**',
|
||||
'bucket': 'test',
|
||||
'endpoint_url': 'https://s3.eu-south-1.amazonaws.com',
|
||||
'secret_access_key': '**REDACTED**',
|
||||
}),
|
||||
'coordinator_data': dict({
|
||||
'all_backups_size': 1048576,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
29
tests/components/aws_s3/test_diagnostics.py
Normal file
29
tests/components/aws_s3/test_diagnostics.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""Tests for AWS S3 diagnostics."""
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.backup import DOMAIN as BACKUP_DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test config entry diagnostics."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
assert await async_setup_component(hass, BACKUP_DOMAIN, {BACKUP_DOMAIN: {}})
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
|
||||
== snapshot
|
||||
)
|
||||
Reference in New Issue
Block a user