Add error handling to LaMetric switch platform (#80161)

This commit is contained in:
Franck Nijhof 2022-10-12 11:35:09 +02:00 committed by GitHub
parent 9396169060
commit 1191f4b61d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 0 deletions

View File

@ -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)

View File

@ -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