mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
SMA add diagnostics (#135852)
This commit is contained in:
parent
a55bd593af
commit
15d57692d9
35
homeassistant/components/sma/diagnostics.py
Normal file
35
homeassistant/components/sma/diagnostics.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""Diagnostics support for SMA."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
TO_REDACT = {CONF_PASSWORD}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for the config entry."""
|
||||
ent_reg = er.async_get(hass)
|
||||
entities = [
|
||||
entity.entity_id
|
||||
for entity in er.async_entries_for_config_entry(ent_reg, entry.entry_id)
|
||||
]
|
||||
|
||||
entity_states = {entity: hass.states.get(entity) for entity in entities}
|
||||
|
||||
entry_dict = entry.as_dict()
|
||||
if "data" in entry_dict:
|
||||
entry_dict["data"] = async_redact_data(entry_dict["data"], TO_REDACT)
|
||||
|
||||
return {
|
||||
"entry": entry_dict,
|
||||
"entities": entity_states,
|
||||
}
|
29
tests/components/sma/snapshots/test_diagnostics.ambr
Normal file
29
tests/components/sma/snapshots/test_diagnostics.ambr
Normal file
@ -0,0 +1,29 @@
|
||||
# serializer version: 1
|
||||
# name: test_get_config_entry_diagnostics
|
||||
dict({
|
||||
'entities': dict({
|
||||
}),
|
||||
'entry': dict({
|
||||
'data': dict({
|
||||
'group': 'user',
|
||||
'host': '1.1.1.1',
|
||||
'password': '**REDACTED**',
|
||||
'ssl': True,
|
||||
'verify_ssl': False,
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'discovery_keys': dict({
|
||||
}),
|
||||
'domain': 'sma',
|
||||
'minor_version': 2,
|
||||
'options': dict({
|
||||
}),
|
||||
'pref_disable_new_entities': False,
|
||||
'pref_disable_polling': False,
|
||||
'source': 'import',
|
||||
'title': 'SMA Device Name',
|
||||
'unique_id': '123456789',
|
||||
'version': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
29
tests/components/sma/test_diagnostics.py
Normal file
29
tests/components/sma/test_diagnostics.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""Test the SMA diagnostics."""
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
from syrupy.filters import props
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test if get_config_entry_diagnostics returns the correct data."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
diagnostics = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, mock_config_entry
|
||||
)
|
||||
assert diagnostics == snapshot(
|
||||
exclude=props("created_at", "modified_at", "entry_id")
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user