Add diagnostics support to onewire (#65131)
* Add diagnostics support to onewire * Add tests Co-authored-by: epenet <epenet@users.noreply.github.com>pull/65442/head
parent
0f9e65e687
commit
1e60958fc4
|
@ -0,0 +1,33 @@
|
|||
"""Diagnostics support for 1-Wire."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .onewirehub import OneWireHub
|
||||
|
||||
TO_REDACT = {CONF_HOST}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
onewirehub: OneWireHub = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
return {
|
||||
"entry": {
|
||||
"title": entry.title,
|
||||
"data": async_redact_data(entry.data, TO_REDACT),
|
||||
"options": {**entry.options},
|
||||
},
|
||||
"devices": [asdict(device_details) for device_details in onewirehub.devices]
|
||||
if onewirehub.devices
|
||||
else [],
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
"""Test 1-Wire diagnostics."""
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.diagnostics import REDACTED
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_owproxy_mock_devices
|
||||
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def override_platforms():
|
||||
"""Override PLATFORMS."""
|
||||
with patch("homeassistant.components.onewire.PLATFORMS", [Platform.SWITCH]):
|
||||
yield
|
||||
|
||||
|
||||
DEVICE_DETAILS = {
|
||||
"device_info": {
|
||||
"identifiers": [["onewire", "EF.111111111113"]],
|
||||
"manufacturer": "Hobby Boards",
|
||||
"model": "HB_HUB",
|
||||
"name": "EF.111111111113",
|
||||
},
|
||||
"family": "EF",
|
||||
"id": "EF.111111111113",
|
||||
"path": "/EF.111111111113/",
|
||||
"type": "HB_HUB",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_id", ["EF.111111111113"], indirect=True)
|
||||
async def test_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
hass_client,
|
||||
owproxy: MagicMock,
|
||||
device_id: str,
|
||||
):
|
||||
"""Test config entry diagnostics."""
|
||||
setup_owproxy_mock_devices(owproxy, Platform.SENSOR, [device_id])
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
||||
"entry": {
|
||||
"data": {
|
||||
"host": REDACTED,
|
||||
"port": 1234,
|
||||
"type": "OWServer",
|
||||
},
|
||||
"options": {},
|
||||
"title": "Mock Title",
|
||||
},
|
||||
"devices": [DEVICE_DETAILS],
|
||||
}
|
Loading…
Reference in New Issue