Fix race condition for component loaded before listening (#13887)
* Fix race condition for component loaded before listening * async/await syntax
This commit is contained in:
parent
bf98b793c5
commit
bba997e484
1 changed files with 22 additions and 27 deletions
|
@ -18,37 +18,26 @@ SECTIONS = ('core', 'customize', 'group', 'hassbian', 'automation', 'script',
|
||||||
ON_DEMAND = ('zwave',)
|
ON_DEMAND = ('zwave',)
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def async_setup(hass, config):
|
||||||
def async_setup(hass, config):
|
|
||||||
"""Set up the config component."""
|
"""Set up the config component."""
|
||||||
yield from hass.components.frontend.async_register_built_in_panel(
|
await hass.components.frontend.async_register_built_in_panel(
|
||||||
'config', 'config', 'mdi:settings')
|
'config', 'config', 'mdi:settings')
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def setup_panel(panel_name):
|
||||||
def setup_panel(panel_name):
|
|
||||||
"""Set up a panel."""
|
"""Set up a panel."""
|
||||||
panel = yield from async_prepare_setup_platform(
|
panel = await async_prepare_setup_platform(
|
||||||
hass, config, DOMAIN, panel_name)
|
hass, config, DOMAIN, panel_name)
|
||||||
|
|
||||||
if not panel:
|
if not panel:
|
||||||
return
|
return
|
||||||
|
|
||||||
success = yield from panel.async_setup(hass)
|
success = await panel.async_setup(hass)
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
key = '{}.{}'.format(DOMAIN, panel_name)
|
key = '{}.{}'.format(DOMAIN, panel_name)
|
||||||
hass.bus.async_fire(EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: key})
|
hass.bus.async_fire(EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: key})
|
||||||
hass.config.components.add(key)
|
hass.config.components.add(key)
|
||||||
|
|
||||||
tasks = [setup_panel(panel_name) for panel_name in SECTIONS]
|
|
||||||
|
|
||||||
for panel_name in ON_DEMAND:
|
|
||||||
if panel_name in hass.config.components:
|
|
||||||
tasks.append(setup_panel(panel_name))
|
|
||||||
|
|
||||||
if tasks:
|
|
||||||
yield from asyncio.wait(tasks, loop=hass.loop)
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def component_loaded(event):
|
def component_loaded(event):
|
||||||
"""Respond to components being loaded."""
|
"""Respond to components being loaded."""
|
||||||
|
@ -58,6 +47,15 @@ def async_setup(hass, config):
|
||||||
|
|
||||||
hass.bus.async_listen(EVENT_COMPONENT_LOADED, component_loaded)
|
hass.bus.async_listen(EVENT_COMPONENT_LOADED, component_loaded)
|
||||||
|
|
||||||
|
tasks = [setup_panel(panel_name) for panel_name in SECTIONS]
|
||||||
|
|
||||||
|
for panel_name in ON_DEMAND:
|
||||||
|
if panel_name in hass.config.components:
|
||||||
|
tasks.append(setup_panel(panel_name))
|
||||||
|
|
||||||
|
if tasks:
|
||||||
|
await asyncio.wait(tasks, loop=hass.loop)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,11 +84,10 @@ class BaseEditConfigView(HomeAssistantView):
|
||||||
"""Set value."""
|
"""Set value."""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def get(self, request, config_key):
|
||||||
def get(self, request, config_key):
|
|
||||||
"""Fetch device specific config."""
|
"""Fetch device specific config."""
|
||||||
hass = request.app['hass']
|
hass = request.app['hass']
|
||||||
current = yield from self.read_config(hass)
|
current = await self.read_config(hass)
|
||||||
value = self._get_value(hass, current, config_key)
|
value = self._get_value(hass, current, config_key)
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
|
@ -98,11 +95,10 @@ class BaseEditConfigView(HomeAssistantView):
|
||||||
|
|
||||||
return self.json(value)
|
return self.json(value)
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def post(self, request, config_key):
|
||||||
def post(self, request, config_key):
|
|
||||||
"""Validate config and return results."""
|
"""Validate config and return results."""
|
||||||
try:
|
try:
|
||||||
data = yield from request.json()
|
data = await request.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return self.json_message('Invalid JSON specified', 400)
|
return self.json_message('Invalid JSON specified', 400)
|
||||||
|
|
||||||
|
@ -121,10 +117,10 @@ class BaseEditConfigView(HomeAssistantView):
|
||||||
hass = request.app['hass']
|
hass = request.app['hass']
|
||||||
path = hass.config.path(self.path)
|
path = hass.config.path(self.path)
|
||||||
|
|
||||||
current = yield from self.read_config(hass)
|
current = await self.read_config(hass)
|
||||||
self._write_value(hass, current, config_key, data)
|
self._write_value(hass, current, config_key, data)
|
||||||
|
|
||||||
yield from hass.async_add_job(_write, path, current)
|
await hass.async_add_job(_write, path, current)
|
||||||
|
|
||||||
if self.post_write_hook is not None:
|
if self.post_write_hook is not None:
|
||||||
hass.async_add_job(self.post_write_hook(hass))
|
hass.async_add_job(self.post_write_hook(hass))
|
||||||
|
@ -133,10 +129,9 @@ class BaseEditConfigView(HomeAssistantView):
|
||||||
'result': 'ok',
|
'result': 'ok',
|
||||||
})
|
})
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def read_config(self, hass):
|
||||||
def read_config(self, hass):
|
|
||||||
"""Read the config."""
|
"""Read the config."""
|
||||||
current = yield from hass.async_add_job(
|
current = await hass.async_add_job(
|
||||||
_read, hass.config.path(self.path))
|
_read, hass.config.path(self.path))
|
||||||
if not current:
|
if not current:
|
||||||
current = self._empty_config()
|
current = self._empty_config()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue