Compare commits

...

4 Commits

Author SHA1 Message Date
Ludovic BOUÉ
08eee8d479 Update tests/components/matter/test_switch.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-29 11:40:54 +01:00
Ludovic BOUÉ
6bbaae7235 Update homeassistant/components/matter/switch.py
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-29 11:40:39 +01:00
Ludovic BOUÉ
86a5dff3f5 Add test for MatterError handling in numeric switch with Eve Thermo fixture 2025-10-29 10:15:35 +00:00
Ludovic BOUÉ
34e137005d Add error handling for Matter switch commands 2025-10-29 10:08:43 +00:00
2 changed files with 60 additions and 21 deletions

View File

@@ -9,6 +9,7 @@ from typing import Any
from chip.clusters import Objects as clusters
from chip.clusters.Objects import ClusterCommand, NullValue
from matter_server.client.models import device_types
from matter_server.common.errors import MatterError
from homeassistant.components.switch import (
SwitchDeviceClass,
@@ -18,6 +19,7 @@ from homeassistant.components.switch import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .entity import MatterEntity, MatterEntityDescription
@@ -54,15 +56,21 @@ class MatterSwitch(MatterEntity, SwitchEntity):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn switch on."""
await self.send_device_command(
clusters.OnOff.Commands.On(),
)
try:
await self.send_device_command(
clusters.OnOff.Commands.On(),
)
except MatterError as err:
raise HomeAssistantError(f"Failed to set value: {err}") from err
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn switch off."""
await self.send_device_command(
clusters.OnOff.Commands.Off(),
)
try:
await self.send_device_command(
clusters.OnOff.Commands.Off(),
)
except MatterError as err:
raise HomeAssistantError(f"Failed to set value: {err}") from err
@callback
def _update_from_device(self) -> None:
@@ -83,18 +91,24 @@ class MatterGenericCommandSwitch(MatterSwitch):
"""Turn switch on."""
if self.entity_description.on_command:
# custom command defined to set the new value
await self.send_device_command(
self.entity_description.on_command(),
self.entity_description.command_timeout,
)
try:
await self.send_device_command(
self.entity_description.on_command(),
self.entity_description.command_timeout,
)
except MatterError as err:
raise HomeAssistantError(f"Failed to set value: {err}") from err
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn switch off."""
if self.entity_description.off_command:
await self.send_device_command(
self.entity_description.off_command(),
self.entity_description.command_timeout,
)
try:
await self.send_device_command(
self.entity_description.off_command(),
self.entity_description.command_timeout,
)
except MatterError as err:
raise HomeAssistantError(f"Failed to set value: {err}") from err
@callback
def _update_from_device(self) -> None:
@@ -111,13 +125,16 @@ class MatterGenericCommandSwitch(MatterSwitch):
**kwargs: Any,
) -> None:
"""Send device command with timeout."""
await self.matter_client.send_device_command(
node_id=self._endpoint.node.node_id,
endpoint_id=self._endpoint.endpoint_id,
command=command,
timed_request_timeout_ms=command_timeout,
**kwargs,
)
try:
await self.matter_client.send_device_command(
node_id=self._endpoint.node.node_id,
endpoint_id=self._endpoint.endpoint_id,
command=command,
timed_request_timeout_ms=command_timeout,
**kwargs,
)
except MatterError as err:
raise HomeAssistantError(f"Failed to set value: {err}") from err
@dataclass(frozen=True, kw_only=True)

View File

@@ -170,6 +170,28 @@ async def test_numeric_switch(
)
@pytest.mark.parametrize("node_fixture", ["eve_thermo"])
async def test_numeric_switch_matter_exception_on_command(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test if a MatterError gets converted to HomeAssistantError using an Eve Thermo fixture."""
state = hass.states.get("switch.eve_thermo_child_lock")
assert state
matter_client.write_attribute.side_effect = MatterError("Unable to write attribute")
with pytest.raises(HomeAssistantError):
# MatterNumericSwitchEntity: child lock
await hass.services.async_call(
"switch",
"turn_on",
{
"entity_id": "switch.eve_thermo_child_lock",
},
blocking=True,
)
@pytest.mark.parametrize("node_fixture", ["on_off_plugin_unit"])
async def test_matter_exception_on_command(
hass: HomeAssistant,