mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +00:00
Don't allow partial update of input_number settings (#78356)
This commit is contained in:
parent
0e7c81288f
commit
15f104911a
@ -65,7 +65,7 @@ def _cv_input_number(cfg):
|
|||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
|
|
||||||
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.Required(CONF_MIN): vol.Coerce(float),
|
vol.Required(CONF_MIN): vol.Coerce(float),
|
||||||
vol.Required(CONF_MAX): vol.Coerce(float),
|
vol.Required(CONF_MAX): vol.Coerce(float),
|
||||||
@ -76,17 +76,6 @@ CREATE_FIELDS = {
|
|||||||
vol.Optional(CONF_MODE, default=MODE_SLIDER): vol.In([MODE_BOX, MODE_SLIDER]),
|
vol.Optional(CONF_MODE, default=MODE_SLIDER): vol.In([MODE_BOX, MODE_SLIDER]),
|
||||||
}
|
}
|
||||||
|
|
||||||
UPDATE_FIELDS = {
|
|
||||||
vol.Optional(CONF_NAME): cv.string,
|
|
||||||
vol.Optional(CONF_MIN): vol.Coerce(float),
|
|
||||||
vol.Optional(CONF_MAX): vol.Coerce(float),
|
|
||||||
vol.Optional(CONF_INITIAL): vol.Coerce(float),
|
|
||||||
vol.Optional(CONF_STEP): vol.All(vol.Coerce(float), vol.Range(min=1e-9)),
|
|
||||||
vol.Optional(CONF_ICON): cv.icon,
|
|
||||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
|
||||||
vol.Optional(CONF_MODE): vol.In([MODE_BOX, MODE_SLIDER]),
|
|
||||||
}
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema(
|
CONFIG_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
DOMAIN: cv.schema_with_slug_keys(
|
DOMAIN: cv.schema_with_slug_keys(
|
||||||
@ -148,7 +137,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:
|
||||||
@ -184,12 +173,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
class NumberStorageCollection(collection.StorageCollection):
|
class NumberStorageCollection(collection.StorageCollection):
|
||||||
"""Input storage based collection."""
|
"""Input storage based collection."""
|
||||||
|
|
||||||
CREATE_SCHEMA = vol.Schema(vol.All(CREATE_FIELDS, _cv_input_number))
|
SCHEMA = vol.Schema(vol.All(STORAGE_FIELDS, _cv_input_number))
|
||||||
UPDATE_SCHEMA = vol.Schema(UPDATE_FIELDS)
|
|
||||||
|
|
||||||
async def _process_create_data(self, data: dict) -> dict:
|
async def _process_create_data(self, data: dict) -> dict:
|
||||||
"""Validate the config is valid."""
|
"""Validate the config is valid."""
|
||||||
return self.CREATE_SCHEMA(data)
|
return self.SCHEMA(data)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _get_suggested_id(self, info: dict) -> str:
|
def _get_suggested_id(self, info: dict) -> str:
|
||||||
@ -214,8 +202,8 @@ class NumberStorageCollection(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.SCHEMA(update_data)
|
||||||
return _cv_input_number({**data, **update_data})
|
return {CONF_ID: data[CONF_ID]} | update_data
|
||||||
|
|
||||||
|
|
||||||
class InputNumber(collection.CollectionEntity, RestoreEntity):
|
class InputNumber(collection.CollectionEntity, RestoreEntity):
|
||||||
|
@ -507,16 +507,14 @@ async def test_ws_delete(hass, hass_ws_client, storage_setup):
|
|||||||
async def test_update_min_max(hass, hass_ws_client, storage_setup):
|
async def test_update_min_max(hass, hass_ws_client, storage_setup):
|
||||||
"""Test updating min/max updates the state."""
|
"""Test updating min/max updates the state."""
|
||||||
|
|
||||||
items = [
|
settings = {
|
||||||
{
|
"name": "from storage",
|
||||||
"id": "from_storage",
|
"max": 100,
|
||||||
"name": "from storage",
|
"min": 0,
|
||||||
"max": 100,
|
"step": 1,
|
||||||
"min": 0,
|
"mode": "slider",
|
||||||
"step": 1,
|
}
|
||||||
"mode": "slider",
|
items = [{"id": "from_storage"} | settings]
|
||||||
}
|
|
||||||
]
|
|
||||||
assert await storage_setup(items)
|
assert await storage_setup(items)
|
||||||
|
|
||||||
input_id = "from_storage"
|
input_id = "from_storage"
|
||||||
@ -530,26 +528,34 @@ async def test_update_min_max(hass, hass_ws_client, storage_setup):
|
|||||||
|
|
||||||
client = await hass_ws_client(hass)
|
client = await hass_ws_client(hass)
|
||||||
|
|
||||||
|
updated_settings = settings | {"min": 9}
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{"id": 6, "type": f"{DOMAIN}/update", f"{DOMAIN}_id": f"{input_id}", "min": 9}
|
{
|
||||||
|
"id": 6,
|
||||||
|
"type": f"{DOMAIN}/update",
|
||||||
|
f"{DOMAIN}_id": f"{input_id}",
|
||||||
|
**updated_settings,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
resp = await client.receive_json()
|
resp = await client.receive_json()
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
|
assert resp["result"] == {"id": "from_storage"} | updated_settings
|
||||||
|
|
||||||
state = hass.states.get(input_entity_id)
|
state = hass.states.get(input_entity_id)
|
||||||
assert float(state.state) == 9
|
assert float(state.state) == 9
|
||||||
|
|
||||||
|
updated_settings = settings | {"max": 5}
|
||||||
await client.send_json(
|
await client.send_json(
|
||||||
{
|
{
|
||||||
"id": 7,
|
"id": 7,
|
||||||
"type": f"{DOMAIN}/update",
|
"type": f"{DOMAIN}/update",
|
||||||
f"{DOMAIN}_id": f"{input_id}",
|
f"{DOMAIN}_id": f"{input_id}",
|
||||||
"max": 5,
|
**updated_settings,
|
||||||
"min": 0,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
resp = await client.receive_json()
|
resp = await client.receive_json()
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
|
assert resp["result"] == {"id": "from_storage"} | updated_settings
|
||||||
|
|
||||||
state = hass.states.get(input_entity_id)
|
state = hass.states.get(input_entity_id)
|
||||||
assert float(state.state) == 5
|
assert float(state.state) == 5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user