mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Order the output of the automation editor (#14019)
* Order the output of the automation editor * Lint
This commit is contained in:
parent
8a10fcd985
commit
5ed73fecd3
@ -1,6 +1,8 @@
|
|||||||
"""Provide configuration end points for Automations."""
|
"""Provide configuration end points for Automations."""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
from homeassistant.const import CONF_ID
|
||||||
from homeassistant.components.config import EditIdBasedConfigView
|
from homeassistant.components.config import EditIdBasedConfigView
|
||||||
from homeassistant.components.automation import (
|
from homeassistant.components.automation import (
|
||||||
PLATFORM_SCHEMA, DOMAIN, async_reload)
|
PLATFORM_SCHEMA, DOMAIN, async_reload)
|
||||||
@ -13,8 +15,38 @@ CONFIG_PATH = 'automations.yaml'
|
|||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_setup(hass):
|
def async_setup(hass):
|
||||||
"""Set up the Automation config API."""
|
"""Set up the Automation config API."""
|
||||||
hass.http.register_view(EditIdBasedConfigView(
|
hass.http.register_view(EditAutomationConfigView(
|
||||||
DOMAIN, 'config', CONFIG_PATH, cv.string,
|
DOMAIN, 'config', CONFIG_PATH, cv.string,
|
||||||
PLATFORM_SCHEMA, post_write_hook=async_reload
|
PLATFORM_SCHEMA, post_write_hook=async_reload
|
||||||
))
|
))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class EditAutomationConfigView(EditIdBasedConfigView):
|
||||||
|
"""Edit automation config."""
|
||||||
|
|
||||||
|
def _write_value(self, hass, data, config_key, new_value):
|
||||||
|
"""Set value."""
|
||||||
|
index = None
|
||||||
|
for index, cur_value in enumerate(data):
|
||||||
|
if cur_value[CONF_ID] == config_key:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
cur_value = OrderedDict()
|
||||||
|
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', 'alias', 'trigger', 'condition', 'action'):
|
||||||
|
if key in cur_value:
|
||||||
|
updated_value[key] = cur_value[key]
|
||||||
|
if key in new_value:
|
||||||
|
updated_value[key] = new_value[key]
|
||||||
|
|
||||||
|
# We cover all current fields above, but just in case we start
|
||||||
|
# supporting more fields in the future.
|
||||||
|
updated_value.update(cur_value)
|
||||||
|
updated_value.update(new_value)
|
||||||
|
data[index] = updated_value
|
||||||
|
83
tests/components/config/test_automation.py
Normal file
83
tests/components/config/test_automation.py
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
"""Test Automation config panel."""
|
||||||
|
import json
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from homeassistant.bootstrap import async_setup_component
|
||||||
|
from homeassistant.components import config
|
||||||
|
|
||||||
|
|
||||||
|
async def test_get_device_config(hass, aiohttp_client):
|
||||||
|
"""Test getting device config."""
|
||||||
|
with patch.object(config, 'SECTIONS', ['automation']):
|
||||||
|
await async_setup_component(hass, 'config', {})
|
||||||
|
|
||||||
|
client = await aiohttp_client(hass.http.app)
|
||||||
|
|
||||||
|
def mock_read(path):
|
||||||
|
"""Mock reading data."""
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
'id': 'sun',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id': 'moon',
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
with patch('homeassistant.components.config._read', mock_read):
|
||||||
|
resp = await client.get(
|
||||||
|
'/api/config/automation/config/moon')
|
||||||
|
|
||||||
|
assert resp.status == 200
|
||||||
|
result = await resp.json()
|
||||||
|
|
||||||
|
assert result == {'id': 'moon'}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_update_device_config(hass, aiohttp_client):
|
||||||
|
"""Test updating device config."""
|
||||||
|
with patch.object(config, 'SECTIONS', ['automation']):
|
||||||
|
await async_setup_component(hass, 'config', {})
|
||||||
|
|
||||||
|
client = await aiohttp_client(hass.http.app)
|
||||||
|
|
||||||
|
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.post(
|
||||||
|
'/api/config/automation/config/moon', data=json.dumps({
|
||||||
|
'trigger': [],
|
||||||
|
'action': [],
|
||||||
|
'condition': [],
|
||||||
|
}))
|
||||||
|
|
||||||
|
assert resp.status == 200
|
||||||
|
result = await resp.json()
|
||||||
|
assert result == {'result': 'ok'}
|
||||||
|
|
||||||
|
assert list(orig_data[1]) == ['id', 'trigger', 'condition', 'action']
|
||||||
|
assert orig_data[1] == {
|
||||||
|
'id': 'moon',
|
||||||
|
'trigger': [],
|
||||||
|
'condition': [],
|
||||||
|
'action': [],
|
||||||
|
}
|
||||||
|
assert written[0] == orig_data
|
Loading…
x
Reference in New Issue
Block a user