diff --git a/homeassistant/components/lametric/switch.py b/homeassistant/components/lametric/switch.py index 8c0acac65e6..c9f7ce047aa 100644 --- a/homeassistant/components/lametric/switch.py +++ b/homeassistant/components/lametric/switch.py @@ -16,6 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import DOMAIN from .coordinator import LaMetricDataUpdateCoordinator from .entity import LaMetricEntity +from .helpers import lametric_exception_handler @dataclass @@ -91,11 +92,13 @@ class LaMetricSwitchEntity(LaMetricEntity, SwitchEntity): """Return state of the switch.""" return self.entity_description.is_on_fn(self.coordinator.data) + @lametric_exception_handler async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" await self.entity_description.set_fn(self.coordinator.lametric, True) await self.coordinator.async_request_refresh() + @lametric_exception_handler async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" await self.entity_description.set_fn(self.coordinator.lametric, False) diff --git a/tests/components/lametric/test_switch.py b/tests/components/lametric/test_switch.py index 350fa1b24f8..7ed47fe463e 100644 --- a/tests/components/lametric/test_switch.py +++ b/tests/components/lametric/test_switch.py @@ -1,6 +1,9 @@ """Tests for the LaMetric switch platform.""" from unittest.mock import MagicMock +from demetriek import LaMetricConnectionError, LaMetricError +import pytest + from homeassistant.components.lametric.const import DOMAIN, SCAN_INTERVAL from homeassistant.components.switch import ( DOMAIN as SWITCH_DOMAIN, @@ -16,6 +19,7 @@ from homeassistant.const import ( STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.entity import EntityCategory import homeassistant.util.dt as dt_util @@ -87,3 +91,63 @@ async def test_bluetooth( state = hass.states.get("switch.frenck_s_lametric_bluetooth") assert state assert state.state == STATE_UNAVAILABLE + + +async def test_switch_error( + hass: HomeAssistant, + init_integration: MockConfigEntry, + mock_lametric: MagicMock, +) -> None: + """Test error handling of the LaMetric switches.""" + mock_lametric.bluetooth.side_effect = LaMetricError + + state = hass.states.get("switch.frenck_s_lametric_bluetooth") + assert state + assert state.state == STATE_OFF + + with pytest.raises( + HomeAssistantError, match="Invalid response from the LaMetric device" + ): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: "switch.frenck_s_lametric_bluetooth", + }, + blocking=True, + ) + await hass.async_block_till_done() + + state = hass.states.get("switch.frenck_s_lametric_bluetooth") + assert state + assert state.state == STATE_OFF + + +async def test_switch_connection_error( + hass: HomeAssistant, + init_integration: MockConfigEntry, + mock_lametric: MagicMock, +) -> None: + """Test connection error handling of the LaMetric switches.""" + mock_lametric.bluetooth.side_effect = LaMetricConnectionError + + state = hass.states.get("switch.frenck_s_lametric_bluetooth") + assert state + assert state.state == STATE_OFF + + with pytest.raises( + HomeAssistantError, match="Error communicating with the LaMetric device" + ): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: "switch.frenck_s_lametric_bluetooth", + }, + blocking=True, + ) + await hass.async_block_till_done() + + state = hass.states.get("switch.frenck_s_lametric_bluetooth") + assert state + assert state.state == STATE_UNAVAILABLE