From abf8b59831607bc886a7cc81a39292e838fdb043 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 13 Sep 2022 20:56:46 +0200 Subject: [PATCH] Don't allow partial update of input_button settings (#78374) --- .../components/input_button/__init__.py | 30 +++++++++++-------- tests/components/input_button/test_init.py | 1 + 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/input_button/__init__.py b/homeassistant/components/input_button/__init__.py index d59142fb915..14ff940ff64 100644 --- a/homeassistant/components/input_button/__init__.py +++ b/homeassistant/components/input_button/__init__.py @@ -30,18 +30,23 @@ DOMAIN = "input_button" _LOGGER = logging.getLogger(__name__) -CREATE_FIELDS = { +STORAGE_FIELDS = { vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)), vol.Optional(CONF_ICON): cv.icon, } -UPDATE_FIELDS = { - vol.Optional(CONF_NAME): cv.string, - vol.Optional(CONF_ICON): cv.icon, -} - CONFIG_SCHEMA = vol.Schema( - {DOMAIN: cv.schema_with_slug_keys(vol.Any(UPDATE_FIELDS, None))}, + { + DOMAIN: cv.schema_with_slug_keys( + vol.Any( + { + vol.Optional(CONF_NAME): cv.string, + vol.Optional(CONF_ICON): cv.icon, + }, + None, + ) + ) + }, extra=vol.ALLOW_EXTRA, ) @@ -53,12 +58,11 @@ STORAGE_VERSION = 1 class InputButtonStorageCollection(collection.StorageCollection): """Input button collection stored in storage.""" - CREATE_SCHEMA = vol.Schema(CREATE_FIELDS) - UPDATE_SCHEMA = vol.Schema(UPDATE_FIELDS) + CREATE_UPDATE_SCHEMA = vol.Schema(STORAGE_FIELDS) async def _process_create_data(self, data: dict) -> vol.Schema: """Validate the config is valid.""" - return self.CREATE_SCHEMA(data) + return self.CREATE_UPDATE_SCHEMA(data) @callback def _get_suggested_id(self, info: dict) -> str: @@ -67,8 +71,8 @@ class InputButtonStorageCollection(collection.StorageCollection): async def _update_data(self, data: dict, update_data: dict) -> dict: """Return a new updated data object.""" - update_data = self.UPDATE_SCHEMA(update_data) - return {**data, **update_data} + update_data = self.CREATE_UPDATE_SCHEMA(update_data) + return {CONF_ID: data[CONF_ID]} | update_data async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -103,7 +107,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await storage_collection.async_load() collection.StorageCollectionWebsocket( - storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS + storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS ).async_setup(hass) async def reload_service_handler(service_call: ServiceCall) -> None: diff --git a/tests/components/input_button/test_init.py b/tests/components/input_button/test_init.py index 33342455147..eb27f277884 100644 --- a/tests/components/input_button/test_init.py +++ b/tests/components/input_button/test_init.py @@ -305,6 +305,7 @@ async def test_ws_create_update(hass, hass_ws_client, storage_setup): ) resp = await client.receive_json() assert resp["success"] + assert resp["result"] == {"id": "new", "name": "newer"} state = hass.states.get(f"{DOMAIN}.new") assert state is not None