Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions homeassistant/components/duco/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

from __future__ import annotations

import asyncio
from dataclasses import asdict
from typing import Any

from duco.exceptions import DucoConnectionError

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN
from .coordinator import DucoConfigEntry

TO_REDACT = {
Expand All @@ -32,11 +35,15 @@ async def async_get_config_entry_diagnostics(
board = asdict(coordinator.board_info)
board.pop("time")

lan_info, duco_diags, write_remaining = await asyncio.gather(
coordinator.client.async_get_lan_info(),
coordinator.client.async_get_diagnostics(),
coordinator.client.async_get_write_req_remaining(),
)
try:
lan_info = await coordinator.client.async_get_lan_info()
duco_diags = await coordinator.client.async_get_diagnostics()
write_remaining = await coordinator.client.async_get_write_req_remaining()
Comment thread
ronaldvdmeer marked this conversation as resolved.
except DucoConnectionError as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_error",
) from err
Comment thread
ronaldvdmeer marked this conversation as resolved.
Comment thread
ronaldvdmeer marked this conversation as resolved.

return async_redact_data(
{
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/duco/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
"cannot_connect": {
"message": "An error occurred while trying to connect to the Duco instance: {error}"
},
"connection_error": {
"message": "Could not connect to the Duco device."
},
"failed_to_set_state": {
"message": "Failed to set ventilation state: {error}"
},
Expand Down
29 changes: 29 additions & 0 deletions tests/components/duco/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

from __future__ import annotations

from http import HTTPStatus
from unittest.mock import AsyncMock

from duco.exceptions import DucoConnectionError
import pytest
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.diagnostics import DOMAIN as DIAGNOSTICS_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
Expand All @@ -27,3 +31,28 @@ async def test_diagnostics(
await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
== snapshot
)


@pytest.mark.usefixtures("init_integration")
@pytest.mark.parametrize(
"failing_method",
["async_get_lan_info", "async_get_diagnostics", "async_get_write_req_remaining"],
)
async def test_diagnostics_connection_error(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
mock_duco_client: AsyncMock,
failing_method: str,
) -> None:
"""Test that a connection error during diagnostics returns a 500 response."""
getattr(mock_duco_client, failing_method).side_effect = DucoConnectionError(
Comment thread
ronaldvdmeer marked this conversation as resolved.
"Server disconnected"
)
assert await async_setup_component(hass, DIAGNOSTICS_DOMAIN, {})
Comment thread
ronaldvdmeer marked this conversation as resolved.
await hass.async_block_till_done()
client = await hass_client()
response = await client.get(
f"/api/diagnostics/config_entry/{mock_config_entry.entry_id}"
)
assert response.status == HTTPStatus.INTERNAL_SERVER_ERROR
Comment thread
ronaldvdmeer marked this conversation as resolved.