From 65cf13836063392e27bad2e913a1e41c5b4ce30c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Wed, 5 May 2021 17:09:18 +0200 Subject: [PATCH] Allign error handling for restart for hassio with core (#50114) * Allign error handling for restart for hassio with core * Reuse HASS_DOMAIN * Address comments --- homeassistant/components/hassio/__init__.py | 36 ++++++++++++++----- .../components/homeassistant/__init__.py | 8 +++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index eabd9bc7cd9..2c25868dfcd 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -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() diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index fd7f2207bc7..44f0843871c 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -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: