Add connection-closed exception and test

This commit is contained in:
Taraman17 2025-02-08 14:35:28 +00:00
parent 45708c6f9b
commit 38cf88f46c
2 changed files with 37 additions and 1 deletions

View File

@ -57,7 +57,13 @@ class HomeeEntity(Entity):
async def async_set_value(self, value: float) -> None:
"""Set an attribute value on the homee node."""
homee = self._entry.runtime_data
await homee.set_value(self._attribute.node_id, self._attribute.id, value)
try:
await homee.set_value(self._attribute.node_id, self._attribute.id, value)
except ConnectionClosed as exception:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="connection_closed",
) from exception
async def async_update(self) -> None:
"""Update entity from homee."""

View File

@ -2,8 +2,12 @@
from unittest.mock import MagicMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from websockets import frames
from websockets.exceptions import ConnectionClosed
from homeassistant.components.homee.const import DOMAIN
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
@ -13,6 +17,7 @@ from homeassistant.components.switch import (
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from . import build_mock_node, setup_integration
@ -116,6 +121,31 @@ async def test_switch_device_class_no_outlet(
)
async def test_send_error(
hass: HomeAssistant,
mock_homee: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test failed set_value command."""
mock_homee.nodes = [build_mock_node("switches.json")]
mock_homee.get_node_by_id.return_value = mock_homee.nodes[0]
await setup_integration(hass, mock_config_entry)
mock_homee.set_value.side_effect = ConnectionClosed(
rcvd=frames.Close(1002, "Protocol Error"), sent=None
)
with pytest.raises(HomeAssistantError) as exc_info:
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.test_switch_switch_1"},
blocking=True,
)
assert exc_info.value.translation_domain == DOMAIN
assert exc_info.value.translation_key == "connection_closed"
async def test_switch_snapshot(
hass: HomeAssistant,
mock_homee: MagicMock,