mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Check config before restarting (#5609)
* Check config before restarting. * Make check_config on restart async * don't check if notification service exists * Use .communicate() * Reduce the number of notifications. Add tests.
This commit is contained in:
committed by
Paulus Schoutsen
parent
51810620fb
commit
f774538e66
@@ -330,11 +330,40 @@ class HomeAssistant(object):
|
||||
self.exit_code = 0
|
||||
self.loop.create_task(self.async_stop())
|
||||
|
||||
@asyncio.coroutine
|
||||
def _async_check_config_and_restart(self):
|
||||
"""Restart Home Assistant if config is valid.
|
||||
|
||||
This method is a coroutine.
|
||||
"""
|
||||
proc = yield from asyncio.create_subprocess_exec(
|
||||
sys.argv[0],
|
||||
'--script',
|
||||
'check_config',
|
||||
stdout=asyncio.subprocess.PIPE)
|
||||
# Wait for the subprocess exit
|
||||
(stdout_data, dummy) = yield from proc.communicate()
|
||||
result = yield from proc.wait()
|
||||
if result:
|
||||
_LOGGER.error("check_config failed. Not restarting.")
|
||||
content = re.sub(r'\033\[[^m]*m', '', str(stdout_data, 'utf-8'))
|
||||
# Put error cleaned from color codes in the error log so it
|
||||
# will be visible at the UI.
|
||||
_LOGGER.error(content)
|
||||
yield from self.services.async_call(
|
||||
'persistent_notification', 'create', {
|
||||
'message': 'Config error. See dev-info panel for details.',
|
||||
'title': 'Restarting',
|
||||
'notification_id': '{}.restart'.format(DOMAIN)})
|
||||
return
|
||||
|
||||
self.exit_code = RESTART_EXIT_CODE
|
||||
yield from self.async_stop()
|
||||
|
||||
@callback
|
||||
def _async_restart_handler(self, *args):
|
||||
"""Restart Home Assistant."""
|
||||
self.exit_code = RESTART_EXIT_CODE
|
||||
self.loop.create_task(self.async_stop())
|
||||
self.loop.create_task(self._async_check_config_and_restart())
|
||||
|
||||
|
||||
class EventOrigin(enum.Enum):
|
||||
|
||||
Reference in New Issue
Block a user