2017-02-12 19:31:46 +00:00
|
|
|
"""Test hassbian config."""
|
2019-05-23 00:24:46 +00:00
|
|
|
import pytest
|
|
|
|
|
2017-02-12 19:31:46 +00:00
|
|
|
from homeassistant.bootstrap import async_setup_component
|
|
|
|
from homeassistant.components import config
|
2019-05-21 05:21:31 +00:00
|
|
|
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
|
|
|
from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL
|
2019-05-23 00:24:46 +00:00
|
|
|
from homeassistant.util import dt as dt_util, location
|
2019-12-08 16:57:28 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import patch
|
|
|
|
|
2019-05-21 05:21:31 +00:00
|
|
|
ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE
|
2017-02-12 19:31:46 +00:00
|
|
|
|
2019-05-21 05:21:31 +00:00
|
|
|
|
2019-05-23 00:24:46 +00:00
|
|
|
@pytest.fixture
|
|
|
|
async def client(hass, hass_ws_client):
|
|
|
|
"""Fixture that can interact with the config manager API."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(config, "SECTIONS", ["core"]):
|
|
|
|
assert await async_setup_component(hass, "config", {})
|
2019-05-23 00:24:46 +00:00
|
|
|
return await hass_ws_client(hass)
|
|
|
|
|
|
|
|
|
2019-05-21 05:21:31 +00:00
|
|
|
async def test_validate_config_ok(hass, hass_client):
|
2017-02-14 05:34:36 +00:00
|
|
|
"""Test checking config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(config, "SECTIONS", ["core"]):
|
|
|
|
await async_setup_component(hass, "config", {})
|
2017-02-12 19:31:46 +00:00
|
|
|
|
2019-05-21 05:21:31 +00:00
|
|
|
client = await hass_client()
|
2017-02-12 19:31:46 +00:00
|
|
|
|
|
|
|
with patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.config.core.async_check_ha_config_file",
|
2020-04-25 21:32:55 +00:00
|
|
|
return_value=None,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
resp = await client.post("/api/config/core/check_config")
|
2017-02-12 19:31:46 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
2019-05-21 05:21:31 +00:00
|
|
|
result = await resp.json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["result"] == "valid"
|
|
|
|
assert result["errors"] is None
|
2017-02-12 19:31:46 +00:00
|
|
|
|
|
|
|
with patch(
|
2019-07-31 19:25:30 +00:00
|
|
|
"homeassistant.components.config.core.async_check_ha_config_file",
|
2020-04-25 21:32:55 +00:00
|
|
|
return_value="beer",
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
resp = await client.post("/api/config/core/check_config")
|
2017-02-12 19:31:46 +00:00
|
|
|
|
|
|
|
assert resp.status == 200
|
2019-05-21 05:21:31 +00:00
|
|
|
result = await resp.json()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["result"] == "invalid"
|
|
|
|
assert result["errors"] == "beer"
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
|
2019-05-23 00:24:46 +00:00
|
|
|
async def test_websocket_core_update(hass, client):
|
2019-05-21 05:21:31 +00:00
|
|
|
"""Test core config update websocket command."""
|
|
|
|
assert hass.config.latitude != 60
|
|
|
|
assert hass.config.longitude != 50
|
|
|
|
assert hass.config.elevation != 25
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.config.location_name != "Huis"
|
2019-05-21 05:21:31 +00:00
|
|
|
assert hass.config.units.name != CONF_UNIT_SYSTEM_IMPERIAL
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.config.time_zone.zone != "America/New_York"
|
2020-05-08 00:29:47 +00:00
|
|
|
assert hass.config.external_url != "https://www.example.com"
|
|
|
|
assert hass.config.internal_url != "http://example.com"
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
await client.send_json(
|
|
|
|
{
|
|
|
|
"id": 5,
|
|
|
|
"type": "config/core/update",
|
|
|
|
"latitude": 60,
|
|
|
|
"longitude": 50,
|
|
|
|
"elevation": 25,
|
|
|
|
"location_name": "Huis",
|
|
|
|
CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_IMPERIAL,
|
|
|
|
"time_zone": "America/New_York",
|
2020-05-08 00:29:47 +00:00
|
|
|
"external_url": "https://www.example.com",
|
|
|
|
"internal_url": "http://example.local",
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 5
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert msg["success"]
|
2019-05-21 05:21:31 +00:00
|
|
|
assert hass.config.latitude == 60
|
|
|
|
assert hass.config.longitude == 50
|
|
|
|
assert hass.config.elevation == 25
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.config.location_name == "Huis"
|
2019-05-21 05:21:31 +00:00
|
|
|
assert hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.config.time_zone.zone == "America/New_York"
|
2020-05-08 00:29:47 +00:00
|
|
|
assert hass.config.external_url == "https://www.example.com"
|
|
|
|
assert hass.config.internal_url == "http://example.local"
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
dt_util.set_default_time_zone(ORIG_TIME_ZONE)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def test_websocket_core_update_not_admin(hass, hass_ws_client, hass_admin_user):
|
2019-05-21 05:21:31 +00:00
|
|
|
"""Test core config fails for non admin."""
|
|
|
|
hass_admin_user.groups = []
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(config, "SECTIONS", ["core"]):
|
|
|
|
await async_setup_component(hass, "config", {})
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
client = await hass_ws_client(hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 6, "type": "config/core/update", "latitude": 23})
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 6
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert not msg["success"]
|
|
|
|
assert msg["error"]["code"] == "unauthorized"
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
|
2019-05-23 00:24:46 +00:00
|
|
|
async def test_websocket_bad_core_update(hass, client):
|
2019-05-21 05:21:31 +00:00
|
|
|
"""Test core config update fails with bad parameters."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await client.send_json({"id": 7, "type": "config/core/update", "latituude": 23})
|
2019-05-21 05:21:31 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["id"] == 7
|
|
|
|
assert msg["type"] == TYPE_RESULT
|
|
|
|
assert not msg["success"]
|
|
|
|
assert msg["error"]["code"] == "invalid_format"
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_detect_config(hass, client):
|
|
|
|
"""Test detect config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
2020-04-25 21:32:55 +00:00
|
|
|
"homeassistant.util.location.async_detect_location_info", return_value=None,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
|
|
|
await client.send_json({"id": 1, "type": "config/core/detect"})
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["success"] is True
|
|
|
|
assert msg["result"] == {}
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_detect_config_fail(hass, client):
|
|
|
|
"""Test detect config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.util.location.async_detect_location_info",
|
2020-04-25 21:32:55 +00:00
|
|
|
return_value=location.LocationInfo(
|
|
|
|
ip=None,
|
|
|
|
country_code=None,
|
|
|
|
country_name=None,
|
|
|
|
region_code=None,
|
|
|
|
region_name=None,
|
|
|
|
city=None,
|
|
|
|
zip_code=None,
|
|
|
|
latitude=None,
|
|
|
|
longitude=None,
|
|
|
|
use_metric=True,
|
|
|
|
time_zone="Europe/Amsterdam",
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
):
|
|
|
|
await client.send_json({"id": 1, "type": "config/core/detect"})
|
2019-05-23 00:24:46 +00:00
|
|
|
|
|
|
|
msg = await client.receive_json()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert msg["success"] is True
|
|
|
|
assert msg["result"] == {"unit_system": "metric", "time_zone": "Europe/Amsterdam"}
|