Allign error handling for restart for hassio with core (#50114)

* Allign error handling for restart for hassio with core

* Reuse HASS_DOMAIN

* Address comments
This commit is contained in:
Joakim Sørensen 2021-05-05 17:09:18 +02:00 committed by GitHub
parent 93572bfe02
commit 65cf138360
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 11 deletions

View file

@ -9,7 +9,10 @@ from typing import Any
import voluptuous as vol
from homeassistant.auth.const import GROUP_ID_ADMIN
from homeassistant.components.homeassistant import SERVICE_CHECK_CONFIG
from homeassistant.components.homeassistant import (
SERVICE_CHECK_CONFIG,
SHUTDOWN_SERVICES,
)
import homeassistant.config as conf_util
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -21,7 +24,7 @@ from homeassistant.const import (
)
from homeassistant.core import DOMAIN as HASS_DOMAIN, Config, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import config_validation as cv, recorder
from homeassistant.helpers.device_registry import DeviceRegistry, async_get_registry
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.loader import bind_hass
@ -469,23 +472,40 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool: # noqa: C90
async def async_handle_core_service(call):
"""Service handler for handling core services."""
if (
call.service in SHUTDOWN_SERVICES
and await recorder.async_migration_in_progress(hass)
):
_LOGGER.error(
"The system cannot %s while a database upgrade is in progress",
call.service,
)
raise HomeAssistantError(
f"The system cannot {call.service} "
"while a database upgrade is in progress."
)
if call.service == SERVICE_HOMEASSISTANT_STOP:
await hassio.stop_homeassistant()
return
try:
errors = await conf_util.async_check_ha_config_file(hass)
except HomeAssistantError:
return
errors = await conf_util.async_check_ha_config_file(hass)
if errors:
_LOGGER.error(errors)
_LOGGER.error(
"The system cannot %s because the configuration is not valid: %s",
call.service,
errors,
)
hass.components.persistent_notification.async_create(
"Config error. See [the logs](/config/logs) for details.",
"Config validating",
f"{HASS_DOMAIN}.check_config",
)
return
raise HomeAssistantError(
f"The system cannot {call.service} "
f"because the configuration is not valid: {errors}"
)
if call.service == SERVICE_HOMEASSISTANT_RESTART:
await hassio.restart_homeassistant()

View file

@ -133,11 +133,12 @@ async def async_setup(hass: ha.HomeAssistant, config: dict) -> bool: # noqa: C9
and await recorder.async_migration_in_progress(hass)
):
_LOGGER.error(
"The system cannot %s while a database upgrade in progress",
"The system cannot %s while a database upgrade is in progress",
call.service,
)
raise HomeAssistantError(
f"The system cannot {call.service} while a database upgrade in progress."
f"The system cannot {call.service} "
"while a database upgrade is in progress."
)
if call.service == SERVICE_HOMEASSISTANT_STOP:
@ -158,7 +159,8 @@ async def async_setup(hass: ha.HomeAssistant, config: dict) -> bool: # noqa: C9
f"{ha.DOMAIN}.check_config",
)
raise HomeAssistantError(
f"The system cannot {call.service} because the configuration is not valid: {errors}"
f"The system cannot {call.service} "
f"because the configuration is not valid: {errors}"
)
if call.service == SERVICE_HOMEASSISTANT_RESTART: