core/tests/components/config/test_group.py

116 lines
3.3 KiB
Python
Raw Normal View History

"""Test Group config panel."""
2017-02-21 05:53:55 +00:00
import json
2017-02-21 05:53:55 +00:00
from homeassistant.bootstrap import async_setup_component
from homeassistant.components import config
from tests.async_mock import AsyncMock, patch
2019-07-31 19:25:30 +00:00
VIEW_NAME = "api:config:group:config"
2017-02-21 05:53:55 +00:00
async def test_get_device_config(hass, hass_client):
2017-02-21 05:53:55 +00:00
"""Test getting device config."""
2019-07-31 19:25:30 +00:00
with patch.object(config, "SECTIONS", ["group"]):
await async_setup_component(hass, "config", {})
2017-02-21 05:53:55 +00:00
client = await hass_client()
2017-02-21 05:53:55 +00:00
def mock_read(path):
"""Mock reading data."""
2019-07-31 19:25:30 +00:00
return {"hello.beer": {"free": "beer"}, "other.entity": {"do": "something"}}
with patch("homeassistant.components.config._read", mock_read):
resp = await client.get("/api/config/group/config/hello.beer")
2017-02-21 05:53:55 +00:00
assert resp.status == 200
result = await resp.json()
2017-02-21 05:53:55 +00:00
2019-07-31 19:25:30 +00:00
assert result == {"free": "beer"}
2017-02-21 05:53:55 +00:00
async def test_update_device_config(hass, hass_client):
2017-02-21 05:53:55 +00:00
"""Test updating device config."""
2019-07-31 19:25:30 +00:00
with patch.object(config, "SECTIONS", ["group"]):
await async_setup_component(hass, "config", {})
2017-02-21 05:53:55 +00:00
client = await hass_client()
2017-02-21 05:53:55 +00:00
orig_data = {
2019-07-31 19:25:30 +00:00
"hello.beer": {"ignored": True},
"other.entity": {"polling_intensity": 2},
2017-02-21 05:53:55 +00:00
}
def mock_read(path):
"""Mock reading data."""
return orig_data
written = []
def mock_write(path, data):
"""Mock writing data."""
written.append(data)
mock_call = AsyncMock()
2019-07-31 19:25:30 +00:00
with patch("homeassistant.components.config._read", mock_read), patch(
"homeassistant.components.config._write", mock_write
), patch.object(hass.services, "async_call", mock_call):
resp = await client.post(
2019-07-31 19:25:30 +00:00
"/api/config/group/config/hello_beer",
data=json.dumps(
{"name": "Beer", "entities": ["light.top", "light.bottom"]}
),
)
await hass.async_block_till_done()
2017-02-21 05:53:55 +00:00
assert resp.status == 200
result = await resp.json()
2019-07-31 19:25:30 +00:00
assert result == {"result": "ok"}
2017-02-21 05:53:55 +00:00
2019-07-31 19:25:30 +00:00
orig_data["hello_beer"]["name"] = "Beer"
orig_data["hello_beer"]["entities"] = ["light.top", "light.bottom"]
2017-02-21 05:53:55 +00:00
assert written[0] == orig_data
2019-07-31 19:25:30 +00:00
mock_call.assert_called_once_with("group", "reload")
2017-02-21 05:53:55 +00:00
async def test_update_device_config_invalid_key(hass, hass_client):
2017-02-21 05:53:55 +00:00
"""Test updating device config."""
2019-07-31 19:25:30 +00:00
with patch.object(config, "SECTIONS", ["group"]):
await async_setup_component(hass, "config", {})
2017-02-21 05:53:55 +00:00
client = await hass_client()
2017-02-21 05:53:55 +00:00
resp = await client.post(
2019-07-31 19:25:30 +00:00
"/api/config/group/config/not a slug", data=json.dumps({"name": "YO"})
)
2017-02-21 05:53:55 +00:00
assert resp.status == 400
async def test_update_device_config_invalid_data(hass, hass_client):
2017-02-21 05:53:55 +00:00
"""Test updating device config."""
2019-07-31 19:25:30 +00:00
with patch.object(config, "SECTIONS", ["group"]):
await async_setup_component(hass, "config", {})
2017-02-21 05:53:55 +00:00
client = await hass_client()
2017-02-21 05:53:55 +00:00
resp = await client.post(
2019-07-31 19:25:30 +00:00
"/api/config/group/config/hello_beer", data=json.dumps({"invalid_option": 2})
)
2017-02-21 05:53:55 +00:00
assert resp.status == 400
async def test_update_device_config_invalid_json(hass, hass_client):
2017-02-21 05:53:55 +00:00
"""Test updating device config."""
2019-07-31 19:25:30 +00:00
with patch.object(config, "SECTIONS", ["group"]):
await async_setup_component(hass, "config", {})
2017-02-21 05:53:55 +00:00
client = await hass_client()
2017-02-21 05:53:55 +00:00
resp = await client.post("/api/config/group/config/hello_beer", data="not json")
2017-02-21 05:53:55 +00:00
assert resp.status == 400