mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Fix poorly formatted automations (#14196)
This commit is contained in:
parent
5dcad89a0d
commit
853a16938b
@ -1,6 +1,7 @@
|
|||||||
"""Provide configuration end points for Automations."""
|
"""Provide configuration end points for Automations."""
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
import uuid
|
||||||
|
|
||||||
from homeassistant.const import CONF_ID
|
from homeassistant.const import CONF_ID
|
||||||
from homeassistant.components.config import EditIdBasedConfigView
|
from homeassistant.components.config import EditIdBasedConfigView
|
||||||
@ -29,7 +30,12 @@ class EditAutomationConfigView(EditIdBasedConfigView):
|
|||||||
"""Set value."""
|
"""Set value."""
|
||||||
index = None
|
index = None
|
||||||
for index, cur_value in enumerate(data):
|
for index, cur_value in enumerate(data):
|
||||||
if cur_value[CONF_ID] == config_key:
|
# When people copy paste their automations 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
|
break
|
||||||
else:
|
else:
|
||||||
cur_value = OrderedDict()
|
cur_value = OrderedDict()
|
||||||
|
@ -81,3 +81,56 @@ async def test_update_device_config(hass, aiohttp_client):
|
|||||||
'action': [],
|
'action': [],
|
||||||
}
|
}
|
||||||
assert written[0] == orig_data
|
assert written[0] == orig_data
|
||||||
|
|
||||||
|
|
||||||
|
async def test_bad_formatted_automations(hass, aiohttp_client):
|
||||||
|
"""Test that we handle automations without ID."""
|
||||||
|
with patch.object(config, 'SECTIONS', ['automation']):
|
||||||
|
await async_setup_component(hass, 'config', {})
|
||||||
|
|
||||||
|
client = await aiohttp_client(hass.http.app)
|
||||||
|
|
||||||
|
orig_data = [
|
||||||
|
{
|
||||||
|
# No ID
|
||||||
|
'action': {
|
||||||
|
'event': 'hello'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'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'}
|
||||||
|
|
||||||
|
# Verify ID added to orig_data
|
||||||
|
assert 'id' in orig_data[0]
|
||||||
|
|
||||||
|
assert orig_data[1] == {
|
||||||
|
'id': 'moon',
|
||||||
|
'trigger': [],
|
||||||
|
'condition': [],
|
||||||
|
'action': [],
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user