Don't allow partial update of input_button settings (#78374)
This commit is contained in:
parent
33fa4ec8b2
commit
abf8b59831
2 changed files with 18 additions and 13 deletions
|
@ -30,18 +30,23 @@ DOMAIN = "input_button"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CREATE_FIELDS = {
|
STORAGE_FIELDS = {
|
||||||
vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)),
|
vol.Required(CONF_NAME): vol.All(str, vol.Length(min=1)),
|
||||||
vol.Optional(CONF_ICON): cv.icon,
|
vol.Optional(CONF_ICON): cv.icon,
|
||||||
}
|
}
|
||||||
|
|
||||||
UPDATE_FIELDS = {
|
|
||||||
vol.Optional(CONF_NAME): cv.string,
|
|
||||||
vol.Optional(CONF_ICON): cv.icon,
|
|
||||||
}
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
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,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -53,12 +58,11 @@ STORAGE_VERSION = 1
|
||||||
class InputButtonStorageCollection(collection.StorageCollection):
|
class InputButtonStorageCollection(collection.StorageCollection):
|
||||||
"""Input button collection stored in storage."""
|
"""Input button collection stored in storage."""
|
||||||
|
|
||||||
CREATE_SCHEMA = vol.Schema(CREATE_FIELDS)
|
CREATE_UPDATE_SCHEMA = vol.Schema(STORAGE_FIELDS)
|
||||||
UPDATE_SCHEMA = vol.Schema(UPDATE_FIELDS)
|
|
||||||
|
|
||||||
async def _process_create_data(self, data: dict) -> vol.Schema:
|
async def _process_create_data(self, data: dict) -> vol.Schema:
|
||||||
"""Validate the config is valid."""
|
"""Validate the config is valid."""
|
||||||
return self.CREATE_SCHEMA(data)
|
return self.CREATE_UPDATE_SCHEMA(data)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _get_suggested_id(self, info: dict) -> str:
|
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:
|
async def _update_data(self, data: dict, update_data: dict) -> dict:
|
||||||
"""Return a new updated data object."""
|
"""Return a new updated data object."""
|
||||||
update_data = self.UPDATE_SCHEMA(update_data)
|
update_data = self.CREATE_UPDATE_SCHEMA(update_data)
|
||||||
return {**data, **update_data}
|
return {CONF_ID: data[CONF_ID]} | update_data
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
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()
|
await storage_collection.async_load()
|
||||||
|
|
||||||
collection.StorageCollectionWebsocket(
|
collection.StorageCollectionWebsocket(
|
||||||
storage_collection, DOMAIN, DOMAIN, CREATE_FIELDS, UPDATE_FIELDS
|
storage_collection, DOMAIN, DOMAIN, STORAGE_FIELDS, STORAGE_FIELDS
|
||||||
).async_setup(hass)
|
).async_setup(hass)
|
||||||
|
|
||||||
async def reload_service_handler(service_call: ServiceCall) -> None:
|
async def reload_service_handler(service_call: ServiceCall) -> None:
|
||||||
|
|
|
@ -305,6 +305,7 @@ async def test_ws_create_update(hass, hass_ws_client, storage_setup):
|
||||||
)
|
)
|
||||||
resp = await client.receive_json()
|
resp = await client.receive_json()
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
|
assert resp["result"] == {"id": "new", "name": "newer"}
|
||||||
|
|
||||||
state = hass.states.get(f"{DOMAIN}.new")
|
state = hass.states.get(f"{DOMAIN}.new")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue