mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Add error handling to LaMetric select platform (#80160)
This commit is contained in:
parent
77571c8a84
commit
9396169060
@ -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
|
||||
@ -83,6 +84,7 @@ class LaMetricSelectEntity(LaMetricEntity, SelectEntity):
|
||||
"""Return the selected entity option to represent the entity state."""
|
||||
return self.entity_description.current_fn(self.coordinator.data)
|
||||
|
||||
@lametric_exception_handler
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the selected option."""
|
||||
await self.entity_description.select_fn(self.coordinator.lametric, option)
|
||||
|
@ -1,7 +1,8 @@
|
||||
"""Tests for the LaMetric select platform."""
|
||||
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.select import (
|
||||
@ -14,8 +15,10 @@ from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_ICON,
|
||||
ATTR_OPTION,
|
||||
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
|
||||
|
||||
@ -69,3 +72,65 @@ async def test_brightness_mode(
|
||||
|
||||
assert len(mock_lametric.display.mock_calls) == 1
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user