-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add websocket API for updating core config #24009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,35 +4,120 @@ | |
|
|
||
| from homeassistant.bootstrap import async_setup_component | ||
| from homeassistant.components import config | ||
| from homeassistant.components.websocket_api.const import TYPE_RESULT | ||
| from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL | ||
| import homeassistant.util.dt as dt_util | ||
| from tests.common import mock_coro | ||
|
|
||
| ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE | ||
|
|
||
| @asyncio.coroutine | ||
| def test_validate_config_ok(hass, hass_client): | ||
|
|
||
| async def test_validate_config_ok(hass, hass_client): | ||
| """Test checking config.""" | ||
| with patch.object(config, 'SECTIONS', ['core']): | ||
| yield from async_setup_component(hass, 'config', {}) | ||
| await async_setup_component(hass, 'config', {}) | ||
|
|
||
| yield from asyncio.sleep(0.1, loop=hass.loop) | ||
| await asyncio.sleep(0.1, loop=hass.loop) | ||
|
|
||
| client = yield from hass_client() | ||
| client = await hass_client() | ||
|
|
||
| with patch( | ||
| 'homeassistant.components.config.core.async_check_ha_config_file', | ||
| return_value=mock_coro()): | ||
| resp = yield from client.post('/api/config/core/check_config') | ||
| resp = await client.post('/api/config/core/check_config') | ||
|
|
||
| assert resp.status == 200 | ||
| result = yield from resp.json() | ||
| result = await resp.json() | ||
| assert result['result'] == 'valid' | ||
| assert result['errors'] is None | ||
|
|
||
| with patch( | ||
| 'homeassistant.components.config.core.async_check_ha_config_file', | ||
| return_value=mock_coro('beer')): | ||
| resp = yield from client.post('/api/config/core/check_config') | ||
| resp = await client.post('/api/config/core/check_config') | ||
|
|
||
| assert resp.status == 200 | ||
| result = yield from resp.json() | ||
| result = await resp.json() | ||
| assert result['result'] == 'invalid' | ||
| assert result['errors'] == 'beer' | ||
|
|
||
|
|
||
| async def test_websocket_core_update(hass, hass_ws_client): | ||
| """Test core config update websocket command.""" | ||
| with patch.object(config, 'SECTIONS', ['core']): | ||
| await async_setup_component(hass, 'config', {}) | ||
|
|
||
| assert hass.config.latitude != 60 | ||
| assert hass.config.longitude != 50 | ||
| assert hass.config.elevation != 25 | ||
| assert hass.config.location_name != 'Huis' | ||
| assert hass.config.units.name != CONF_UNIT_SYSTEM_IMPERIAL | ||
| assert hass.config.time_zone.zone != 'America/New_York' | ||
|
|
||
| client = await hass_ws_client(hass) | ||
| 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', | ||
| }) | ||
|
|
||
| msg = await client.receive_json() | ||
|
|
||
| assert msg['id'] == 5 | ||
| assert msg['type'] == TYPE_RESULT | ||
| assert msg['success'] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please verify latitude got set, preferably test all other values too
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! I had a review comment which I forgot to publish: should the new WS command
Or is it fine like it is? |
||
| assert hass.config.latitude == 60 | ||
| assert hass.config.longitude == 50 | ||
| assert hass.config.elevation == 25 | ||
| assert hass.config.location_name == 'Huis' | ||
| assert hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL | ||
| assert hass.config.time_zone.zone == 'America/New_York' | ||
|
|
||
| dt_util.set_default_time_zone(ORIG_TIME_ZONE) | ||
|
|
||
|
|
||
| async def test_websocket_core_update_not_admin( | ||
| hass, hass_ws_client, hass_admin_user): | ||
| """Test core config fails for non admin.""" | ||
| hass_admin_user.groups = [] | ||
| with patch.object(config, 'SECTIONS', ['core']): | ||
| await async_setup_component(hass, 'config', {}) | ||
|
|
||
| client = await hass_ws_client(hass) | ||
| await client.send_json({ | ||
| 'id': 6, | ||
| 'type': 'config/core/update', | ||
| 'latitude': 123, | ||
| }) | ||
|
|
||
| msg = await client.receive_json() | ||
|
|
||
| assert msg['id'] == 6 | ||
| assert msg['type'] == TYPE_RESULT | ||
| assert not msg['success'] | ||
| assert msg['error']['code'] == 'unauthorized' | ||
|
|
||
|
|
||
| async def test_websocket_bad_core_update(hass, hass_ws_client): | ||
| """Test core config update fails with bad parameters.""" | ||
| with patch.object(config, 'SECTIONS', ['core']): | ||
| await async_setup_component(hass, 'config', {}) | ||
|
|
||
| client = await hass_ws_client(hass) | ||
| await client.send_json({ | ||
| 'id': 7, | ||
| 'type': 'config/core/update', | ||
| 'latituude': 123, | ||
| }) | ||
|
|
||
| msg = await client.receive_json() | ||
|
|
||
| assert msg['id'] == 7 | ||
| assert msg['type'] == TYPE_RESULT | ||
| assert not msg['success'] | ||
| assert msg['error']['code'] == 'invalid_format' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to return something here?