Allow deletion of automations and scripts (#23845)

This commit is contained in:
Paulus Schoutsen 2019-05-14 09:16:37 +02:00 committed by Pascal Vizeli
parent 6f9860b25e
commit a859997190
3 changed files with 115 additions and 1 deletions

View File

@ -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."""

View File

@ -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'

View File

@ -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': {}
}