Backend changes for customize config panel. (#9134)

* Backend changes for customize config panel.

* Backend changes for customize config panel.

* Add customize.yaml to default config

* Precreate customize.yaml

* Add tests
This commit is contained in:
Andrey authored and Paulus Schoutsen committed 2017-08-26 10:02:32 -07:00
1 parent c537770786
commit c73338bf3e
6 files changed
+196 -15

No files matched your search

+118
View File
@@ -0,0 +1,118 @@
"""Test Customize config panel."""
import asyncio
import json
from unittest.mock import patch
from homeassistant.bootstrap import async_setup_component
from homeassistant.components import config
from homeassistant.config import DATA_CUSTOMIZE
@asyncio.coroutine
def test_get_entity(hass, test_client):
"""Test getting entity."""
with patch.object(config, 'SECTIONS', ['customize']):
yield from async_setup_component(hass, 'config', {})
client = yield from test_client(hass.http.app)
def mock_read(path):
"""Mock reading data."""
return {
'hello.beer': {
'free': 'beer',
},
'other.entity': {
'do': 'something',
},
}
hass.data[DATA_CUSTOMIZE] = {'hello.beer': {'cold': 'beer'}}
with patch('homeassistant.components.config._read', mock_read):
resp = yield from client.get(
'/api/config/customize/config/hello.beer')
assert resp.status == 200
result = yield from resp.json()
assert result == {'local': {'free': 'beer'}, 'global': {'cold': 'beer'}}
@asyncio.coroutine
def test_update_entity(hass, test_client):
"""Test updating entity."""
with patch.object(config, 'SECTIONS', ['customize']):
yield from async_setup_component(hass, 'config', {})
client = yield from test_client(hass.http.app)
orig_data = {
'hello.beer': {
'ignored': True,
},
'other.entity': {
'polling_intensity': 2,
},
}
def mock_read(path):
"""Mock reading data."""
return orig_data
written = []
def mock_write(path, data):
"""Mock writing data."""
written.append(data)
hass.states.async_set('hello.world', 'state', {'a': 'b'})
with patch('homeassistant.components.config._read', mock_read), \
patch('homeassistant.components.config._write', mock_write):
resp = yield from client.post(
'/api/config/customize/config/hello.world', data=json.dumps({
'name': 'Beer',
'entities': ['light.top', 'light.bottom'],
}))
assert resp.status == 200
result = yield from resp.json()
assert result == {'result': 'ok'}
state = hass.states.get('hello.world')
assert state.state == 'state'
assert dict(state.attributes) == {
'a': 'b', 'name': 'Beer', 'entities': ['light.top', 'light.bottom']}
orig_data['hello.world']['name'] = 'Beer'
orig_data['hello.world']['entities'] = ['light.top', 'light.bottom']
assert written[0] == orig_data
@asyncio.coroutine
def test_update_entity_invalid_key(hass, test_client):
"""Test updating entity."""
with patch.object(config, 'SECTIONS', ['customize']):
yield from async_setup_component(hass, 'config', {})
client = yield from test_client(hass.http.app)
resp = yield from client.post(
'/api/config/customize/config/not_entity', data=json.dumps({
'name': 'YO',
}))
assert resp.status == 400
@asyncio.coroutine
def test_update_entity_invalid_json(hass, test_client):
"""Test updating entity."""
with patch.object(config, 'SECTIONS', ['customize']):
yield from async_setup_component(hass, 'config', {})
client = yield from test_client(hass.http.app)
resp = yield from client.post(
'/api/config/customize/config/hello.beer', data='not json')
assert resp.status == 400
+16 -5
View File
@@ -22,6 +22,8 @@ from homeassistant.components.config.group import (
CONFIG_PATH as GROUP_CONFIG_PATH)
from homeassistant.components.config.automation import (
CONFIG_PATH as AUTOMATIONS_CONFIG_PATH)
from homeassistant.components.config.customize import (
CONFIG_PATH as CUSTOMIZE_CONFIG_PATH)
from tests.common import (
get_test_config_dir, get_test_home_assistant, mock_coro)
@@ -31,6 +33,7 @@ YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
VERSION_PATH = os.path.join(CONFIG_DIR, config_util.VERSION_FILE)
GROUP_PATH = os.path.join(CONFIG_DIR, GROUP_CONFIG_PATH)
AUTOMATIONS_PATH = os.path.join(CONFIG_DIR, AUTOMATIONS_CONFIG_PATH)
CUSTOMIZE_PATH = os.path.join(CONFIG_DIR, CUSTOMIZE_CONFIG_PATH)
ORIG_TIMEZONE = dt_util.DEFAULT_TIME_ZONE
@@ -65,8 +68,12 @@ class TestConfig(unittest.TestCase):
if os.path.isfile(AUTOMATIONS_PATH):
os.remove(AUTOMATIONS_PATH)
if os.path.isfile(CUSTOMIZE_PATH):
os.remove(CUSTOMIZE_PATH)
self.hass.stop()
# pylint: disable=no-self-use
def test_create_default_config(self):
"""Test creation of default config."""
config_util.create_default_config(CONFIG_DIR, False)
@@ -75,6 +82,7 @@ class TestConfig(unittest.TestCase):
assert os.path.isfile(VERSION_PATH)
assert os.path.isfile(GROUP_PATH)
assert os.path.isfile(AUTOMATIONS_PATH)
assert os.path.isfile(CUSTOMIZE_PATH)
def test_find_config_file_yaml(self):
"""Test if it finds a YAML config file."""
@@ -169,7 +177,8 @@ class TestConfig(unittest.TestCase):
CONF_ELEVATION: 101,
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC,
CONF_NAME: 'Home',
CONF_TIME_ZONE: 'America/Los_Angeles'
CONF_TIME_ZONE: 'America/Los_Angeles',
CONF_CUSTOMIZE: OrderedDict(),
}
assert expected_values == ha_conf
@@ -334,11 +343,12 @@ class TestConfig(unittest.TestCase):
mock_open = mock.mock_open()
def mock_isfile(filename):
def _mock_isfile(filename):
return True
with mock.patch('homeassistant.config.open', mock_open, create=True), \
mock.patch('homeassistant.config.os.path.isfile', mock_isfile):
mock.patch(
'homeassistant.config.os.path.isfile', _mock_isfile):
opened_file = mock_open.return_value
# pylint: disable=no-member
opened_file.readline.return_value = ha_version
@@ -359,11 +369,12 @@ class TestConfig(unittest.TestCase):
mock_open = mock.mock_open()
def mock_isfile(filename):
def _mock_isfile(filename):
return False
with mock.patch('homeassistant.config.open', mock_open, create=True), \
mock.patch('homeassistant.config.os.path.isfile', mock_isfile):
mock.patch(
'homeassistant.config.os.path.isfile', _mock_isfile):
opened_file = mock_open.return_value
# pylint: disable=no-member
opened_file.readline.return_value = ha_version