diff --git a/homeassistant/components/config/__init__.py b/homeassistant/components/config/__init__.py index 3752d5d37bf..70c72e899c0 100644 --- a/homeassistant/components/config/__init__.py +++ b/homeassistant/components/config/__init__.py @@ -92,6 +92,10 @@ class BaseEditConfigView(HomeAssistantView): """Set value.""" raise NotImplementedError + def _delete_value(self, hass, data, config_key): + """Delete value.""" + raise NotImplementedError + async def get(self, request, config_key): """Fetch device specific config.""" hass = request.app['hass'] @@ -128,7 +132,27 @@ class BaseEditConfigView(HomeAssistantView): current = await self.read_config(hass) self._write_value(hass, current, config_key, data) - await hass.async_add_job(_write, path, current) + await hass.async_add_executor_job(_write, path, current) + + if self.post_write_hook is not None: + hass.async_create_task(self.post_write_hook(hass)) + + return self.json({ + 'result': 'ok', + }) + + async def delete(self, request, config_key): + """Remove an entry.""" + hass = request.app['hass'] + current = await self.read_config(hass) + value = self._get_value(hass, current, config_key) + path = hass.config.path(self.path) + + if value is None: + return self.json_message('Resource not found', 404) + + self._delete_value(hass, current, config_key) + await hass.async_add_executor_job(_write, path, current) if self.post_write_hook is not None: hass.async_create_task(self.post_write_hook(hass)) @@ -161,6 +185,10 @@ class EditKeyBasedConfigView(BaseEditConfigView): """Set value.""" data.setdefault(config_key, {}).update(new_value) + def _delete_value(self, hass, data, config_key): + """Delete value.""" + return data.pop(config_key) + class EditIdBasedConfigView(BaseEditConfigView): """Configure key based config entries.""" @@ -184,6 +212,13 @@ class EditIdBasedConfigView(BaseEditConfigView): value.update(new_value) + def _delete_value(self, hass, data, config_key): + """Delete value.""" + index = next( + idx for idx, val in enumerate(data) + if val.get(CONF_ID) == config_key) + data.pop(index) + def _read(path): """Read YAML helper.""" diff --git a/tests/components/config/test_automation.py b/tests/components/config/test_automation.py index f97559a224f..30b4f72b0bc 100644 --- a/tests/components/config/test_automation.py +++ b/tests/components/config/test_automation.py @@ -134,3 +134,41 @@ async def test_bad_formatted_automations(hass, hass_client): 'condition': [], 'action': [], } + + +async def test_delete_automation(hass, hass_client): + """Test deleting an automation.""" + with patch.object(config, 'SECTIONS', ['automation']): + await async_setup_component(hass, 'config', {}) + + client = await hass_client() + + orig_data = [ + { + 'id': 'sun', + }, + { + 'id': 'moon', + } + ] + + def mock_read(path): + """Mock reading data.""" + return orig_data + + written = [] + + def mock_write(path, data): + """Mock writing data.""" + written.append(data) + + with patch('homeassistant.components.config._read', mock_read), \ + patch('homeassistant.components.config._write', mock_write): + resp = await client.delete('/api/config/automation/config/sun') + + assert resp.status == 200 + result = await resp.json() + assert result == {'result': 'ok'} + + assert len(written) == 1 + assert written[0][0]['id'] == 'moon' diff --git a/tests/components/config/test_script.py b/tests/components/config/test_script.py new file mode 100644 index 00000000000..d0848d18dcc --- /dev/null +++ b/tests/components/config/test_script.py @@ -0,0 +1,41 @@ +"""Tests for config/script.""" +from unittest.mock import patch + +from homeassistant.bootstrap import async_setup_component +from homeassistant.components import config + + +async def test_delete_script(hass, hass_client): + """Test deleting a script.""" + with patch.object(config, 'SECTIONS', ['script']): + await async_setup_component(hass, 'config', {}) + + client = await hass_client() + + orig_data = { + 'one': {}, + 'two': {}, + } + + def mock_read(path): + """Mock reading data.""" + return orig_data + + written = [] + + def mock_write(path, data): + """Mock writing data.""" + written.append(data) + + with patch('homeassistant.components.config._read', mock_read), \ + patch('homeassistant.components.config._write', mock_write): + resp = await client.delete('/api/config/script/config/two') + + assert resp.status == 200 + result = await resp.json() + assert result == {'result': 'ok'} + + assert len(written) == 1 + assert written[0] == { + 'one': {} + }