Require core config detection to be triggerd manually (#24019)

* Detect core config

* Remove elevation

* Lint

* Lint

* Fix type
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2019-05-22 17:24:46 -07:00
1 parent f207e01510
commit 9e96397e6a
12 files changed
+188 -323

No files matched your search

+57 -12
View File
@@ -5,12 +5,18 @@ import voluptuous as vol
from homeassistant.components.http import HomeAssistantView
from homeassistant.config import async_check_ha_config_file
from homeassistant.components import websocket_api
from homeassistant.const import (
CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL
)
from homeassistant.helpers import config_validation as cv
from homeassistant.util import location
async def async_setup(hass):
"""Set up the Hassbian config."""
hass.http.register_view(CheckConfigView)
hass.components.websocket_api.async_register_command(websocket_core_update)
websocket_api.async_register_command(hass, websocket_update_config)
websocket_api.async_register_command(hass, websocket_detect_config)
return True
@@ -35,18 +41,57 @@ class CheckConfigView(HomeAssistantView):
@websocket_api.require_admin
@websocket_api.async_response
@websocket_api.websocket_command({
vol.Required('type'): 'config/core/update',
vol.Optional('latitude'): vol.Coerce(float),
vol.Optional('longitude'): vol.Coerce(float),
vol.Optional('elevation'): vol.Coerce(int),
vol.Optional('unit_system'): vol.Coerce(str),
vol.Optional('location_name'): vol.Coerce(str),
vol.Optional('time_zone'): vol.Coerce(str),
'type': 'config/core/update',
vol.Optional('latitude'): cv.latitude,
vol.Optional('longitude'): cv.longitude,
vol.Optional('elevation'): int,
vol.Optional('unit_system'): cv.unit_system,
vol.Optional('location_name'): str,
vol.Optional('time_zone'): cv.time_zone,
})
async def websocket_core_update(hass, connection, msg):
"""Handle request for account info."""
async def websocket_update_config(hass, connection, msg):
"""Handle update core config command."""
data = dict(msg)
data.pop('id')
data.pop('type')
await hass.config.update(**data)
connection.send_result(msg['id'])
try:
await hass.config.update(**data)
connection.send_result(msg['id'])
except ValueError as err:
connection.send_error(
msg['id'], 'invalid_info', str(err)
)
@websocket_api.require_admin
@websocket_api.async_response
@websocket_api.websocket_command({
'type': 'config/core/detect',
})
async def websocket_detect_config(hass, connection, msg):
"""Detect core config."""
session = hass.helpers.aiohttp_client.async_get_clientsession()
location_info = await location.async_detect_location_info(session)
info = {}
if location_info is None:
connection.send_result(msg['id'], info)
return
if location_info.use_metric:
info['unit_system'] = CONF_UNIT_SYSTEM_METRIC
else:
info['unit_system'] = CONF_UNIT_SYSTEM_IMPERIAL
if location_info.latitude:
info['latitude'] = location_info.latitude
if location_info.longitude:
info['longitude'] = location_info.longitude
if location_info.time_zone:
info['time_zone'] = location_info.time_zone
connection.send_result(msg['id'], info)
@@ -3,10 +3,11 @@ from homeassistant.core import callback
from homeassistant.loader import bind_hass
from homeassistant.helpers.storage import Store
from .const import DOMAIN, STEP_USER, STEPS, STEP_INTEGRATION
from .const import (
DOMAIN, STEP_USER, STEPS, STEP_INTEGRATION, STEP_CORE_CONFIG)
STORAGE_KEY = DOMAIN
STORAGE_VERSION = 2
STORAGE_VERSION = 3
class OnboadingStorage(Store):
@@ -15,7 +16,10 @@ class OnboadingStorage(Store):
async def _async_migrate_func(self, old_version, old_data):
"""Migrate to the new version."""
# From version 1 -> 2, we automatically mark the integration step done
old_data['done'].append(STEP_INTEGRATION)
if old_version < 2:
old_data['done'].append(STEP_INTEGRATION)
if old_version < 3:
old_data['done'].append(STEP_CORE_CONFIG)
return old_data
@@ -1,10 +1,12 @@
"""Constants for the onboarding component."""
DOMAIN = 'onboarding'
STEP_USER = 'user'
STEP_CORE_CONFIG = 'core_config'
STEP_INTEGRATION = 'integration'
STEPS = [
STEP_USER,
STEP_CORE_CONFIG,
STEP_INTEGRATION,
]
+25 -2
View File
@@ -7,13 +7,16 @@ from homeassistant.components.http.data_validator import RequestDataValidator
from homeassistant.components.http.view import HomeAssistantView
from homeassistant.core import callback
from .const import DOMAIN, STEP_USER, STEPS, DEFAULT_AREAS, STEP_INTEGRATION
from .const import (
DOMAIN, STEP_USER, STEPS, DEFAULT_AREAS, STEP_INTEGRATION,
STEP_CORE_CONFIG)
async def async_setup(hass, data, store):
"""Set up the onboarding view."""
hass.http.register_view(OnboardingView(data, store))
hass.http.register_view(UserOnboardingView(data, store))
hass.http.register_view(CoreConfigOnboardingView(data, store))
hass.http.register_view(IntegrationOnboardingView(data, store))
@@ -128,6 +131,26 @@ class UserOnboardingView(_BaseOnboardingView):
})
class CoreConfigOnboardingView(_BaseOnboardingView):
"""View to finish core config onboarding step."""
url = '/api/onboarding/core_config'
name = 'api:onboarding:core_config'
step = STEP_CORE_CONFIG
async def post(self, request):
"""Handle finishing core config step."""
hass = request.app['hass']
async with self._lock:
if self._async_is_done():
return self.json_message('Core config step already done', 403)
await self._async_mark_done(hass)
return self.json({})
class IntegrationOnboardingView(_BaseOnboardingView):
"""View to finish integration onboarding step."""
@@ -139,7 +162,7 @@ class IntegrationOnboardingView(_BaseOnboardingView):
vol.Required('client_id'): str,
}))
async def post(self, request, data):
"""Handle user creation, area creation."""
"""Handle token creation."""
hass = request.app['hass']
user = request['hass_user']