mirror of
https://github.com/home-assistant/core.git
synced 2025-05-19 13:29:17 +00:00
Allow deletion of automations and scripts (#23845)
This commit is contained in:
parent
6f9860b25e
commit
a859997190
@ -92,6 +92,10 @@ class BaseEditConfigView(HomeAssistantView):
|
|||||||
"""Set value."""
|
"""Set value."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def _delete_value(self, hass, data, config_key):
|
||||||
|
"""Delete value."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
async def get(self, request, config_key):
|
async def get(self, request, config_key):
|
||||||
"""Fetch device specific config."""
|
"""Fetch device specific config."""
|
||||||
hass = request.app['hass']
|
hass = request.app['hass']
|
||||||
@ -128,7 +132,27 @@ class BaseEditConfigView(HomeAssistantView):
|
|||||||
current = await self.read_config(hass)
|
current = await self.read_config(hass)
|
||||||
self._write_value(hass, current, config_key, data)
|
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:
|
if self.post_write_hook is not None:
|
||||||
hass.async_create_task(self.post_write_hook(hass))
|
hass.async_create_task(self.post_write_hook(hass))
|
||||||
@ -161,6 +185,10 @@ class EditKeyBasedConfigView(BaseEditConfigView):
|
|||||||
"""Set value."""
|
"""Set value."""
|
||||||
data.setdefault(config_key, {}).update(new_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):
|
class EditIdBasedConfigView(BaseEditConfigView):
|
||||||
"""Configure key based config entries."""
|
"""Configure key based config entries."""
|
||||||
@ -184,6 +212,13 @@ class EditIdBasedConfigView(BaseEditConfigView):
|
|||||||
|
|
||||||
value.update(new_value)
|
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):
|
def _read(path):
|
||||||
"""Read YAML helper."""
|
"""Read YAML helper."""
|
||||||
|
@ -134,3 +134,41 @@ async def test_bad_formatted_automations(hass, hass_client):
|
|||||||
'condition': [],
|
'condition': [],
|
||||||
'action': [],
|
'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'
|
||||||
|
41
tests/components/config/test_script.py
Normal file
41
tests/components/config/test_script.py
Normal 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': {}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user