Add error handling to input_select integration (#93940)

pull/94288/head
j4n-e4t 2023-06-05 19:53:24 +02:00 committed by Paulus Schoutsen
parent 421fa5b035
commit 93baf24394
3 changed files with 19 additions and 18 deletions

View File

@ -302,12 +302,9 @@ class InputSelect(collection.CollectionEntity, SelectEntity, RestoreEntity):
async def async_select_option(self, option: str) -> None:
"""Select new option."""
if option not in self.options:
_LOGGER.warning(
"Invalid option: %s (possible options: %s)",
option,
", ".join(self.options),
raise HomeAssistantError(
f"Invalid option: {option} (possible options: {', '.join(self.options)})"
)
return
self._attr_current_option = option
self.async_write_ha_state()

View File

@ -102,6 +102,7 @@ async def test_select_option(hass: HomeAssistant) -> None:
state = hass.states.get(entity_id)
assert state.state == "another option"
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,
@ -305,6 +306,7 @@ async def test_set_options_service(hass: HomeAssistant) -> None:
state = hass.states.get(entity_id)
assert state.state == "test1"
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_SELECT_OPTION,

View File

@ -2,6 +2,7 @@
import pytest
from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.state import async_reproduce_state
from homeassistant.setup import async_setup_component
@ -60,6 +61,7 @@ async def test_reproducing_states(
assert hass.states.get(ENTITY).state == VALID_OPTION3
# Test setting state to invalid state
with pytest.raises(HomeAssistantError):
await async_reproduce_state(hass, [State(ENTITY, INVALID_OPTION)])
# The entity state should be unchanged