mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix saving a scene (#49980)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
42c48e8cf9
commit
301d4b0657
@ -1,5 +1,4 @@
|
|||||||
"""Provide configuration end points for Scenes."""
|
"""Provide configuration end points for Scenes."""
|
||||||
from collections import OrderedDict
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from homeassistant.components.scene import DOMAIN, PLATFORM_SCHEMA
|
from homeassistant.components.scene import DOMAIN, PLATFORM_SCHEMA
|
||||||
@ -48,24 +47,9 @@ class EditSceneConfigView(EditIdBasedConfigView):
|
|||||||
|
|
||||||
def _write_value(self, hass, data, config_key, new_value):
|
def _write_value(self, hass, data, config_key, new_value):
|
||||||
"""Set value."""
|
"""Set value."""
|
||||||
index = None
|
|
||||||
for index, cur_value in enumerate(data):
|
|
||||||
# When people copy paste their scenes to the config file,
|
|
||||||
# they sometimes forget to add IDs. Fix it here.
|
|
||||||
if CONF_ID not in cur_value:
|
|
||||||
cur_value[CONF_ID] = uuid.uuid4().hex
|
|
||||||
|
|
||||||
elif cur_value[CONF_ID] == config_key:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
cur_value = {}
|
|
||||||
cur_value[CONF_ID] = config_key
|
|
||||||
index = len(data)
|
|
||||||
data.append(cur_value)
|
|
||||||
|
|
||||||
# Iterate through some keys that we want to have ordered in the output
|
# Iterate through some keys that we want to have ordered in the output
|
||||||
updated_value = OrderedDict()
|
updated_value = {CONF_ID: config_key}
|
||||||
for key in ("id", "name", "entities"):
|
for key in ("name", "entities"):
|
||||||
if key in new_value:
|
if key in new_value:
|
||||||
updated_value[key] = new_value[key]
|
updated_value[key] = new_value[key]
|
||||||
|
|
||||||
@ -73,4 +57,16 @@ class EditSceneConfigView(EditIdBasedConfigView):
|
|||||||
# supporting more fields in the future.
|
# supporting more fields in the future.
|
||||||
updated_value.update(new_value)
|
updated_value.update(new_value)
|
||||||
|
|
||||||
data[index] = updated_value
|
updated = False
|
||||||
|
for index, cur_value in enumerate(data):
|
||||||
|
# When people copy paste their scenes to the config file,
|
||||||
|
# they sometimes forget to add IDs. Fix it here.
|
||||||
|
if CONF_ID not in cur_value:
|
||||||
|
cur_value[CONF_ID] = uuid.uuid4().hex
|
||||||
|
|
||||||
|
elif cur_value[CONF_ID] == config_key:
|
||||||
|
data[index] = updated_value
|
||||||
|
updated = True
|
||||||
|
|
||||||
|
if not updated:
|
||||||
|
data.append(updated_value)
|
||||||
|
@ -8,6 +8,55 @@ from homeassistant.helpers import entity_registry as er
|
|||||||
from homeassistant.util.yaml import dump
|
from homeassistant.util.yaml import dump
|
||||||
|
|
||||||
|
|
||||||
|
async def test_create_scene(hass, hass_client):
|
||||||
|
"""Test creating a scene."""
|
||||||
|
with patch.object(config, "SECTIONS", ["scene"]):
|
||||||
|
await async_setup_component(hass, "config", {})
|
||||||
|
|
||||||
|
client = await hass_client()
|
||||||
|
|
||||||
|
def mock_read(path):
|
||||||
|
"""Mock reading data."""
|
||||||
|
return None
|
||||||
|
|
||||||
|
written = []
|
||||||
|
|
||||||
|
def mock_write(path, data):
|
||||||
|
"""Mock writing data."""
|
||||||
|
data = dump(data)
|
||||||
|
written.append(data)
|
||||||
|
|
||||||
|
with patch("homeassistant.components.config._read", mock_read), patch(
|
||||||
|
"homeassistant.components.config._write", mock_write
|
||||||
|
), patch("homeassistant.config.async_hass_config_yaml", return_value={}):
|
||||||
|
resp = await client.post(
|
||||||
|
"/api/config/scene/config/light_off",
|
||||||
|
data=json.dumps(
|
||||||
|
{
|
||||||
|
# "id": "light_off",
|
||||||
|
"name": "Lights off",
|
||||||
|
"entities": {"light.bedroom": {"state": "off"}},
|
||||||
|
}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert resp.status == 200
|
||||||
|
result = await resp.json()
|
||||||
|
assert result == {"result": "ok"}
|
||||||
|
|
||||||
|
assert len(written) == 1
|
||||||
|
written_yaml = written[0]
|
||||||
|
assert (
|
||||||
|
written_yaml
|
||||||
|
== """- id: light_off
|
||||||
|
name: Lights off
|
||||||
|
entities:
|
||||||
|
light.bedroom:
|
||||||
|
state: 'off'
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_update_scene(hass, hass_client):
|
async def test_update_scene(hass, hass_client):
|
||||||
"""Test updating a scene."""
|
"""Test updating a scene."""
|
||||||
with patch.object(config, "SECTIONS", ["scene"]):
|
with patch.object(config, "SECTIONS", ["scene"]):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user