From 93baf2439452839c3cef4769a0326499229ac028 Mon Sep 17 00:00:00 2001 From: j4n-e4t <130256240+j4n-e4t@users.noreply.github.com> Date: Mon, 5 Jun 2023 19:53:24 +0200 Subject: [PATCH] Add error handling to input_select integration (#93940) --- .../components/input_select/__init__.py | 7 ++--- tests/components/input_select/test_init.py | 26 ++++++++++--------- .../input_select/test_reproduce_state.py | 4 ++- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/input_select/__init__.py b/homeassistant/components/input_select/__init__.py index 186ab84fb81..2c5a1c87f29 100644 --- a/homeassistant/components/input_select/__init__.py +++ b/homeassistant/components/input_select/__init__.py @@ -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() diff --git a/tests/components/input_select/test_init.py b/tests/components/input_select/test_init.py index 315392702eb..6908a1c532e 100644 --- a/tests/components/input_select/test_init.py +++ b/tests/components/input_select/test_init.py @@ -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" diff --git a/tests/components/input_select/test_reproduce_state.py b/tests/components/input_select/test_reproduce_state.py index d6e9274fa8d..a00b6b02ade 100644 --- a/tests/components/input_select/test_reproduce_state.py +++ b/tests/components/input_select/test_reproduce_state.py @@ -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