1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Backend support for themes (#8419)

* Backend support for themes

* Fix test

* Add theme_updated event

* Shorten name

* Add tests
This commit is contained in:
Andrey
2017-07-13 04:08:13 +03:00
committed by Paulus Schoutsen
parent bb9db28c95
commit a65f22378e
4 changed files with 183 additions and 2 deletions

View File

@@ -1,10 +1,12 @@
"""The tests for Home Assistant frontend."""
import asyncio
import re
from unittest.mock import patch
import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components.frontend import DOMAIN, ATTR_THEMES
@pytest.fixture
@@ -14,6 +16,20 @@ def mock_http_client(hass, test_client):
return hass.loop.run_until_complete(test_client(hass.http.app))
@pytest.fixture
def mock_http_client_with_themes(hass, test_client):
"""Start the Hass HTTP component."""
hass.loop.run_until_complete(async_setup_component(hass, 'frontend', {
DOMAIN: {
ATTR_THEMES: {
'happy': {
'primary-color': 'red'
}
}
}}))
return hass.loop.run_until_complete(test_client(hass.http.app))
@asyncio.coroutine
def test_frontend_and_static(mock_http_client):
"""Test if we can get the frontend."""
@@ -56,10 +72,64 @@ def test_we_cannot_POST_to_root(mock_http_client):
@asyncio.coroutine
def test_states_routes(hass, mock_http_client):
def test_states_routes(mock_http_client):
"""All served by index."""
resp = yield from mock_http_client.get('/states')
assert resp.status == 200
resp = yield from mock_http_client.get('/states/group.existing')
assert resp.status == 200
@asyncio.coroutine
def test_themes_api(mock_http_client_with_themes):
"""Test that /api/themes returns correct data."""
resp = yield from mock_http_client_with_themes.get('/api/themes')
json = yield from resp.json()
assert json['default_theme'] == 'default'
assert json['themes'] == {'happy': {'primary-color': 'red'}}
@asyncio.coroutine
def test_themes_set_theme(hass, mock_http_client_with_themes):
"""Test frontend.set_theme service."""
yield from hass.services.async_call(DOMAIN, 'set_theme', {'name': 'happy'})
yield from hass.async_block_till_done()
resp = yield from mock_http_client_with_themes.get('/api/themes')
json = yield from resp.json()
assert json['default_theme'] == 'happy'
yield from hass.services.async_call(
DOMAIN, 'set_theme', {'name': 'default'})
yield from hass.async_block_till_done()
resp = yield from mock_http_client_with_themes.get('/api/themes')
json = yield from resp.json()
assert json['default_theme'] == 'default'
@asyncio.coroutine
def test_themes_set_theme_wrong_name(hass, mock_http_client_with_themes):
"""Test frontend.set_theme service called with wrong name."""
yield from hass.services.async_call(DOMAIN, 'set_theme', {'name': 'wrong'})
yield from hass.async_block_till_done()
resp = yield from mock_http_client_with_themes.get('/api/themes')
json = yield from resp.json()
assert json['default_theme'] == 'default'
@asyncio.coroutine
def test_themes_reload_themes(hass, mock_http_client_with_themes):
"""Test frontend.reload_themes service."""
with patch('homeassistant.components.frontend.load_yaml_config_file',
return_value={DOMAIN: {
ATTR_THEMES: {
'sad': {'primary-color': 'blue'}
}}}):
yield from hass.services.async_call(DOMAIN, 'set_theme',
{'name': 'happy'})
yield from hass.services.async_call(DOMAIN, 'reload_themes')
yield from hass.async_block_till_done()
resp = yield from mock_http_client_with_themes.get('/api/themes')
json = yield from resp.json()
assert json['themes'] == {'sad': {'primary-color': 'blue'}}
assert json['default_theme'] == 'default'