Add error handling to LaMetric select platform (#80160)

This commit is contained in:
Franck Nijhof 2022-10-12 11:34:08 +02:00 committed by GitHub
parent 77571c8a84
commit 9396169060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 1 deletions

View File

@ -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
@ -83,6 +84,7 @@ class LaMetricSelectEntity(LaMetricEntity, SelectEntity):
"""Return the selected entity option to represent the entity state.""" """Return the selected entity option to represent the entity state."""
return self.entity_description.current_fn(self.coordinator.data) return self.entity_description.current_fn(self.coordinator.data)
@lametric_exception_handler
async def async_select_option(self, option: str) -> None: async def async_select_option(self, option: str) -> None:
"""Change the selected option.""" """Change the selected option."""
await self.entity_description.select_fn(self.coordinator.lametric, option) await self.entity_description.select_fn(self.coordinator.lametric, option)

View File

@ -1,7 +1,8 @@
"""Tests for the LaMetric select platform.""" """Tests for the LaMetric select platform."""
from unittest.mock import MagicMock from unittest.mock import MagicMock
from demetriek import BrightnessMode from demetriek import BrightnessMode, LaMetricConnectionError, LaMetricError
import pytest
from homeassistant.components.lametric.const import DOMAIN from homeassistant.components.lametric.const import DOMAIN
from homeassistant.components.select import ( from homeassistant.components.select import (
@ -14,8 +15,10 @@ from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_FRIENDLY_NAME,
ATTR_ICON, ATTR_ICON,
ATTR_OPTION, ATTR_OPTION,
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
@ -69,3 +72,65 @@ async def test_brightness_mode(
assert len(mock_lametric.display.mock_calls) == 1 assert len(mock_lametric.display.mock_calls) == 1
mock_lametric.display.assert_called_once_with(brightness_mode=BrightnessMode.MANUAL) mock_lametric.display.assert_called_once_with(brightness_mode=BrightnessMode.MANUAL)
async def test_select_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_lametric: MagicMock,
) -> None:
"""Test error handling of the LaMetric selects."""
mock_lametric.display.side_effect = LaMetricError
state = hass.states.get("select.frenck_s_lametric_brightness_mode")
assert state
assert state.state == BrightnessMode.AUTO
with pytest.raises(
HomeAssistantError, match="Invalid response from the LaMetric device"
):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.frenck_s_lametric_brightness_mode",
ATTR_OPTION: "manual",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.frenck_s_lametric_brightness_mode")
assert state
assert state.state == BrightnessMode.AUTO
async def test_select_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_lametric: MagicMock,
) -> None:
"""Test connection error handling of the LaMetric selects."""
mock_lametric.display.side_effect = LaMetricConnectionError
state = hass.states.get("select.frenck_s_lametric_brightness_mode")
assert state
assert state.state == BrightnessMode.AUTO
with pytest.raises(
HomeAssistantError, match="Error communicating with the LaMetric device"
):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.frenck_s_lametric_brightness_mode",
ATTR_OPTION: "manual",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.frenck_s_lametric_brightness_mode")
assert state
assert state.state == STATE_UNAVAILABLE