mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add error handling to LaMetric switch platform (#80161)
This commit is contained in:
parent
9396169060
commit
1191f4b61d
@ -16,6 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import LaMetricDataUpdateCoordinator
|
from .coordinator import LaMetricDataUpdateCoordinator
|
||||||
from .entity import LaMetricEntity
|
from .entity import LaMetricEntity
|
||||||
|
from .helpers import lametric_exception_handler
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -91,11 +92,13 @@ class LaMetricSwitchEntity(LaMetricEntity, SwitchEntity):
|
|||||||
"""Return state of the switch."""
|
"""Return state of the switch."""
|
||||||
return self.entity_description.is_on_fn(self.coordinator.data)
|
return self.entity_description.is_on_fn(self.coordinator.data)
|
||||||
|
|
||||||
|
@lametric_exception_handler
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
await self.entity_description.set_fn(self.coordinator.lametric, True)
|
await self.entity_description.set_fn(self.coordinator.lametric, True)
|
||||||
await self.coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
|
@lametric_exception_handler
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
await self.entity_description.set_fn(self.coordinator.lametric, False)
|
await self.entity_description.set_fn(self.coordinator.lametric, False)
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
"""Tests for the LaMetric switch platform."""
|
"""Tests for the LaMetric switch platform."""
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from demetriek import LaMetricConnectionError, LaMetricError
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.lametric.const import DOMAIN, SCAN_INTERVAL
|
from homeassistant.components.lametric.const import DOMAIN, SCAN_INTERVAL
|
||||||
from homeassistant.components.switch import (
|
from homeassistant.components.switch import (
|
||||||
DOMAIN as SWITCH_DOMAIN,
|
DOMAIN as SWITCH_DOMAIN,
|
||||||
@ -16,6 +19,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
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 import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.entity import EntityCategory
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
@ -87,3 +91,63 @@ async def test_bluetooth(
|
|||||||
state = hass.states.get("switch.frenck_s_lametric_bluetooth")
|
state = hass.states.get("switch.frenck_s_lametric_bluetooth")
|
||||||
assert state
|
assert state
|
||||||
assert state.state == STATE_UNAVAILABLE
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user