mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add exception translations to input_select (#184414)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Support to select an option from a list."""
|
||||
|
||||
from collections import Counter
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any, Self, cast, override
|
||||
@@ -26,7 +27,7 @@ from homeassistant.const import ( # noqa: F401
|
||||
SERVICE_RELOAD,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
|
||||
from homeassistant.helpers import collection, config_validation as cv
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
@@ -269,18 +270,31 @@ class InputSelect(collection.CollectionEntity, SelectEntity, RestoreEntity):
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Select new option."""
|
||||
if option not in self.options:
|
||||
raise HomeAssistantError(
|
||||
f"Invalid option: {option} (possible options:"
|
||||
f" {', '.join(self.options)})"
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="invalid_option",
|
||||
translation_placeholders={
|
||||
"entity_id": self.entity_id,
|
||||
"option": option,
|
||||
"options": ", ".join(self.options),
|
||||
},
|
||||
)
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_set_options(self, options: list[str]) -> None:
|
||||
"""Set options."""
|
||||
unique_options = list(dict.fromkeys(options))
|
||||
if len(unique_options) != len(options):
|
||||
raise HomeAssistantError(f"Duplicated options: {options}")
|
||||
if duplicates := [
|
||||
option for option, count in Counter(options).items() if count > 1
|
||||
]:
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="duplicate_options",
|
||||
translation_placeholders={
|
||||
"entity_id": self.entity_id,
|
||||
"duplicates": ", ".join(duplicates),
|
||||
},
|
||||
)
|
||||
|
||||
self._attr_options = options
|
||||
|
||||
|
||||
@@ -16,6 +16,14 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"duplicate_options": {
|
||||
"message": "Options for {entity_id} contain duplicates: {duplicates}."
|
||||
},
|
||||
"invalid_option": {
|
||||
"message": "Option {option} is not valid for {entity_id}. Valid options are: {options}."
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
"reload": {
|
||||
"description": "Reloads helpers from the YAML-configuration.",
|
||||
|
||||
@@ -28,7 +28,7 @@ from homeassistant.const import (
|
||||
SERVICE_RELOAD,
|
||||
)
|
||||
from homeassistant.core import Context, HomeAssistant, State
|
||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
||||
from homeassistant.exceptions import ServiceValidationError, Unauthorized
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -105,7 +105,10 @@ async def test_select_option(hass: HomeAssistant) -> None:
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "another option"
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match="Option non existing option is not valid for input_select.test_1",
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
@@ -309,7 +312,10 @@ async def test_set_options_service(hass: HomeAssistant) -> None:
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "test1"
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match="Option first option is not valid for input_select.test_1",
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
@@ -329,7 +335,16 @@ async def test_set_options_service(hass: HomeAssistant) -> None:
|
||||
assert state.state == "test2"
|
||||
|
||||
|
||||
async def test_set_options_service_duplicate(hass: HomeAssistant) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
("options", "duplicates"),
|
||||
[
|
||||
(["option1", "option1"], "option1"),
|
||||
(["option1", "option2", "option1", "option3", "option2"], "option1, option2"),
|
||||
],
|
||||
)
|
||||
async def test_set_options_service_duplicate(
|
||||
hass: HomeAssistant, options: list[str], duplicates: str
|
||||
) -> None:
|
||||
"""Test set_options service with duplicates."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
@@ -353,11 +368,14 @@ async def test_set_options_service_duplicate(hass: HomeAssistant) -> None:
|
||||
"last option",
|
||||
]
|
||||
|
||||
with pytest.raises(HomeAssistantError):
|
||||
with pytest.raises(
|
||||
ServiceValidationError,
|
||||
match=f"Options for input_select.test_1 contain duplicates: {duplicates}$",
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_SET_OPTIONS,
|
||||
{ATTR_OPTIONS: ["option1", "option1"], ATTR_ENTITY_ID: entity_id},
|
||||
{ATTR_OPTIONS: options, ATTR_ENTITY_ID: entity_id},
|
||||
blocking=True,
|
||||
)
|
||||
state = hass.states.get(entity_id)
|
||||
|
||||
Reference in New Issue
Block a user