Add error handling to input_select integration (#93940)
parent
421fa5b035
commit
93baf24394
|
@ -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()
|
||||
|
||||
|
|
|
@ -102,12 +102,13 @@ async def test_select_option(hass: HomeAssistant) -> None:
|
|||
state = hass.states.get(entity_id)
|
||||
assert state.state == "another option"
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "non existing option"},
|
||||
blocking=True,
|
||||
)
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "non existing option"},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "another option"
|
||||
|
||||
|
@ -305,12 +306,13 @@ async def test_set_options_service(hass: HomeAssistant) -> None:
|
|||
state = hass.states.get(entity_id)
|
||||
assert state.state == "test1"
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "first option"},
|
||||
blocking=True,
|
||||
)
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "first option"},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "test1"
|
||||
|
||||
|
|
|
@ -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,7 +61,8 @@ async def test_reproducing_states(
|
|||
assert hass.states.get(ENTITY).state == VALID_OPTION3
|
||||
|
||||
# Test setting state to invalid state
|
||||
await async_reproduce_state(hass, [State(ENTITY, INVALID_OPTION)])
|
||||
with pytest.raises(HomeAssistantError):
|
||||
await async_reproduce_state(hass, [State(ENTITY, INVALID_OPTION)])
|
||||
|
||||
# The entity state should be unchanged
|
||||
assert hass.states.get(ENTITY).state == VALID_OPTION3
|
||||
|
|
Loading…
Reference in New Issue