mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add support for device diagnostics (#64344)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
"""Test the Diagnostics integration."""
|
||||
from http import HTTPStatus
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||
from homeassistant.helpers.device_registry import async_get
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import get_diagnostics_for_config_entry
|
||||
from . import get_diagnostics_for_config_entry, get_diagnostics_for_device
|
||||
|
||||
from tests.common import mock_platform
|
||||
from tests.common import MockConfigEntry, mock_platform
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -21,16 +23,26 @@ async def mock_diagnostics_integration(hass):
|
||||
Mock(
|
||||
async_get_config_entry_diagnostics=AsyncMock(
|
||||
return_value={
|
||||
"hello": "info",
|
||||
"config_entry": "info",
|
||||
}
|
||||
),
|
||||
async_get_device_diagnostics=AsyncMock(
|
||||
return_value={
|
||||
"device": "info",
|
||||
}
|
||||
),
|
||||
),
|
||||
)
|
||||
mock_platform(
|
||||
hass,
|
||||
"integration_without_diagnostics.diagnostics",
|
||||
Mock(),
|
||||
)
|
||||
assert await async_setup_component(hass, "diagnostics", {})
|
||||
|
||||
|
||||
async def test_websocket_info(hass, hass_ws_client):
|
||||
"""Test camera_thumbnail websocket command."""
|
||||
async def test_websocket(hass, hass_ws_client):
|
||||
"""Test websocket command."""
|
||||
client = await hass_ws_client(hass)
|
||||
await client.send_json({"id": 5, "type": "diagnostics/list"})
|
||||
|
||||
@@ -40,12 +52,78 @@ async def test_websocket_info(hass, hass_ws_client):
|
||||
assert msg["type"] == TYPE_RESULT
|
||||
assert msg["success"]
|
||||
assert msg["result"] == [
|
||||
{"domain": "fake_integration", "handlers": {"config_entry": True}}
|
||||
{
|
||||
"domain": "fake_integration",
|
||||
"handlers": {"config_entry": True, "device": True},
|
||||
}
|
||||
]
|
||||
|
||||
await client.send_json(
|
||||
{"id": 6, "type": "diagnostics/get", "domain": "fake_integration"}
|
||||
)
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg["id"] == 6
|
||||
assert msg["type"] == TYPE_RESULT
|
||||
assert msg["success"]
|
||||
assert msg["result"] == {
|
||||
"domain": "fake_integration",
|
||||
"handlers": {"config_entry": True, "device": True},
|
||||
}
|
||||
|
||||
|
||||
async def test_download_diagnostics(hass, hass_client):
|
||||
"""Test record service."""
|
||||
assert await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, "fake_integration"
|
||||
) == {"hello": "info"}
|
||||
"""Test download diagnostics."""
|
||||
config_entry = MockConfigEntry(domain="fake_integration")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
||||
"config_entry": "info"
|
||||
}
|
||||
|
||||
dev_reg = async_get(hass)
|
||||
device = dev_reg.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id, identifiers={("test", "test")}
|
||||
)
|
||||
|
||||
assert await get_diagnostics_for_device(
|
||||
hass, hass_client, config_entry, device
|
||||
) == {"device": "info"}
|
||||
|
||||
|
||||
async def test_failure_scenarios(hass, hass_client):
|
||||
"""Test failure scenarios."""
|
||||
client = await hass_client()
|
||||
|
||||
# test wrong d_type
|
||||
response = await client.get("/api/diagnostics/wrong_type/fake_id")
|
||||
assert response.status == HTTPStatus.BAD_REQUEST
|
||||
|
||||
# test wrong d_id
|
||||
response = await client.get("/api/diagnostics/config_entry/fake_id")
|
||||
assert response.status == HTTPStatus.NOT_FOUND
|
||||
|
||||
config_entry = MockConfigEntry(domain="integration_without_diagnostics")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
# test valid d_type and d_id but no config entry diagnostics
|
||||
response = await client.get(
|
||||
f"/api/diagnostics/config_entry/{config_entry.entry_id}"
|
||||
)
|
||||
assert response.status == HTTPStatus.NOT_FOUND
|
||||
|
||||
config_entry = MockConfigEntry(domain="fake_integration")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
# test invalid sub_type
|
||||
response = await client.get(
|
||||
f"/api/diagnostics/config_entry/{config_entry.entry_id}/wrong_type/id"
|
||||
)
|
||||
assert response.status == HTTPStatus.BAD_REQUEST
|
||||
|
||||
# test invalid sub_id
|
||||
response = await client.get(
|
||||
f"/api/diagnostics/config_entry/{config_entry.entry_id}/device/fake_id"
|
||||
)
|
||||
assert response.status == HTTPStatus.NOT_FOUND
|
||||
|
||||
Reference in New Issue
Block a user