From 301d4b065774d352f0369aec78e0c57d8fbc3cbb Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Mon, 3 May 2021 07:11:57 +0200 Subject: [PATCH] Fix saving a scene (#49980) Co-authored-by: Paulus Schoutsen --- homeassistant/components/config/scene.py | 34 ++++++++-------- tests/components/config/test_scene.py | 49 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/config/scene.py b/homeassistant/components/config/scene.py index 8507fbbe47d..41b8dce0957 100644 --- a/homeassistant/components/config/scene.py +++ b/homeassistant/components/config/scene.py @@ -1,5 +1,4 @@ """Provide configuration end points for Scenes.""" -from collections import OrderedDict import uuid 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): """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 - updated_value = OrderedDict() - for key in ("id", "name", "entities"): + updated_value = {CONF_ID: config_key} + for key in ("name", "entities"): if key in new_value: updated_value[key] = new_value[key] @@ -73,4 +57,16 @@ class EditSceneConfigView(EditIdBasedConfigView): # supporting more fields in the future. 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) diff --git a/tests/components/config/test_scene.py b/tests/components/config/test_scene.py index 8e4276cc9fd..429fb807883 100644 --- a/tests/components/config/test_scene.py +++ b/tests/components/config/test_scene.py @@ -8,6 +8,55 @@ from homeassistant.helpers import entity_registry as er 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): """Test updating a scene.""" with patch.object(config, "SECTIONS", ["scene"]):