Add error handling to input_select integration (#93940)

This commit is contained in:
j4n-e4t 2023-06-05 19:53:24 +02:00 committed by GitHub
parent e2c2262719
commit 28f6062bab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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: async def async_select_option(self, option: str) -> None:
"""Select new option.""" """Select new option."""
if option not in self.options: if option not in self.options:
_LOGGER.warning( raise HomeAssistantError(
"Invalid option: %s (possible options: %s)", f"Invalid option: {option} (possible options: {', '.join(self.options)})"
option,
", ".join(self.options),
) )
return
self._attr_current_option = option self._attr_current_option = option
self.async_write_ha_state() self.async_write_ha_state()

View file

@ -102,12 +102,13 @@ async def test_select_option(hass: HomeAssistant) -> None:
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "another option" assert state.state == "another option"
await hass.services.async_call( with pytest.raises(HomeAssistantError):
DOMAIN, await hass.services.async_call(
SERVICE_SELECT_OPTION, DOMAIN,
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "non existing option"}, SERVICE_SELECT_OPTION,
blocking=True, {ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "non existing option"},
) blocking=True,
)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "another option" assert state.state == "another option"
@ -305,12 +306,13 @@ async def test_set_options_service(hass: HomeAssistant) -> None:
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "test1" assert state.state == "test1"
await hass.services.async_call( with pytest.raises(HomeAssistantError):
DOMAIN, await hass.services.async_call(
SERVICE_SELECT_OPTION, DOMAIN,
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "first option"}, SERVICE_SELECT_OPTION,
blocking=True, {ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "first option"},
) blocking=True,
)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "test1" assert state.state == "test1"

View file

@ -2,6 +2,7 @@
import pytest import pytest
from homeassistant.core import HomeAssistant, State from homeassistant.core import HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.state import async_reproduce_state from homeassistant.helpers.state import async_reproduce_state
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -60,7 +61,8 @@ async def test_reproducing_states(
assert hass.states.get(ENTITY).state == VALID_OPTION3 assert hass.states.get(ENTITY).state == VALID_OPTION3
# Test setting state to invalid state # 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 # The entity state should be unchanged
assert hass.states.get(ENTITY).state == VALID_OPTION3 assert hass.states.get(ENTITY).state == VALID_OPTION3