2018-04-23 17:47:06 +00:00
|
|
|
"""Test Automation config panel."""
|
|
|
|
import json
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from homeassistant.bootstrap import async_setup_component
|
|
|
|
from homeassistant.components import config
|
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_get_device_config(hass, hass_client):
|
2018-04-23 17:47:06 +00:00
|
|
|
"""Test getting device config."""
|
|
|
|
with patch.object(config, 'SECTIONS', ['automation']):
|
|
|
|
await async_setup_component(hass, 'config', {})
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await hass_client()
|
2018-04-23 17:47:06 +00:00
|
|
|
|
|
|
|
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'}
|
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_update_device_config(hass, hass_client):
|
2018-04-23 17:47:06 +00:00
|
|
|
"""Test updating device config."""
|
|
|
|
with patch.object(config, 'SECTIONS', ['automation']):
|
|
|
|
await async_setup_component(hass, 'config', {})
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await hass_client()
|
2018-04-23 17:47:06 +00:00
|
|
|
|
|
|
|
orig_data = [
|
2018-04-30 13:56:42 +00:00
|
|
|
{
|
|
|
|
'id': 'sun',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'id': 'moon',
|
|
|
|
}
|
|
|
|
]
|
2018-04-23 17:47:06 +00:00
|
|
|
|
|
|
|
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
|
2018-04-30 13:56:42 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_bad_formatted_automations(hass, hass_client):
|
2018-04-30 13:56:42 +00:00
|
|
|
"""Test that we handle automations without ID."""
|
|
|
|
with patch.object(config, 'SECTIONS', ['automation']):
|
|
|
|
await async_setup_component(hass, 'config', {})
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await hass_client()
|
2018-04-30 13:56:42 +00:00
|
|
|
|
|
|
|
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': [],
|
|
|
|
}
|
2019-05-14 07:16:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
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'
|