diff --git a/custom_components/hacs/hacsbase/hacs.py b/custom_components/hacs/hacsbase/hacs.py index 49d5dbd9e3c..220efa5f386 100644 --- a/custom_components/hacs/hacsbase/hacs.py +++ b/custom_components/hacs/hacsbase/hacs.py @@ -13,7 +13,6 @@ from custom_components.hacs.helpers.functions.register_repository import ( register_repository, ) - from custom_components.hacs.helpers.functions.store import ( async_load_from_store, async_save_to_store, diff --git a/custom_components/hacs/operational/setup.py b/custom_components/hacs/operational/setup.py index 8c3255502c5..375487612c1 100644 --- a/custom_components/hacs/operational/setup.py +++ b/custom_components/hacs/operational/setup.py @@ -18,10 +18,8 @@ LovelaceMode, ) from custom_components.hacs.hacsbase.data import HacsData - from custom_components.hacs.operational.reload import async_reload_entry from custom_components.hacs.operational.remove import async_remove_entry - from custom_components.hacs.share import get_hacs from custom_components.hacs.tasks.manager import HacsTaskManager diff --git a/custom_components/hacs/tasks/base.py b/custom_components/hacs/tasks/base.py index 04ac048845b..d53abc3b21a 100644 --- a/custom_components/hacs/tasks/base.py +++ b/custom_components/hacs/tasks/base.py @@ -27,13 +27,8 @@ def slug(self) -> str: """Return the check slug.""" return self.__class__.__module__.rsplit(".", maxsplit=1)[-1] - @abstractmethod - async def execute(self) -> None: - """Execute the task.""" - raise NotImplementedError - async def execute_task(self) -> None: - """This should only be executed by the manager.""" + """Execute the task defined in subclass.""" if self.hacs.system.disabled: self.log.warning( "Skipping task %s, HACS is disabled - %s", @@ -43,10 +38,25 @@ async def execute_task(self) -> None: return self.log.info("Executing task: %s", self.slug) start_time = timer() - await self.execute() - self.log.debug( - "Task %s took " "%.2f seconds to complete", self.slug, timer() - start_time - ) + + try: + if task := getattr(self, "execute", None): + await self.hass.async_add_executor_job(task) + elif task := getattr(self, "async_execute", None): + await task() # pylint: disable=not-callable + else: + raise NotImplementedError( + f"{self.slug} does not have a execute method defined." + ) + except BaseException as exception: # pylint: disable=broad-except + self.log.error("Task %s failed: %s", self.slug, exception) + + else: + self.log.debug( + "Task %s took " "%.2f seconds to complete", + self.slug, + timer() - start_time, + ) class HacsTaskEventBase(HacsTaskBase): diff --git a/custom_components/hacs/tasks/hello_world.py b/custom_components/hacs/tasks/hello_world.py index b49163fb21d..5aaf91c8052 100644 --- a/custom_components/hacs/tasks/hello_world.py +++ b/custom_components/hacs/tasks/hello_world.py @@ -10,5 +10,5 @@ async def async_setup() -> None: class Task(HacsTaskManualBase): """"Hacs task base.""" - async def execute(self) -> None: + def execute(self) -> None: self.log.debug("Hello World!") diff --git a/custom_components/hacs/tasks/setup_categories.py b/custom_components/hacs/tasks/setup_categories.py index 1d2074362c1..36d75625091 100644 --- a/custom_components/hacs/tasks/setup_categories.py +++ b/custom_components/hacs/tasks/setup_categories.py @@ -13,7 +13,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP, HacsStage.RUNNING] - async def execute(self) -> None: + def execute(self) -> None: self.hacs.common.categories = set() for category in (HacsCategory.INTEGRATION, HacsCategory.PLUGIN): self.hacs.enable_hacs_category(HacsCategory(category)) diff --git a/custom_components/hacs/tasks/setup_clear_old_storage.py b/custom_components/hacs/tasks/setup_clear_old_storage.py index 6a088eb838b..422a5dd431a 100644 --- a/custom_components/hacs/tasks/setup_clear_old_storage.py +++ b/custom_components/hacs/tasks/setup_clear_old_storage.py @@ -15,11 +15,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: - await self.hacs.hass.async_add_executor_job(self._clear_storage) - - def _clear_storage(self) -> None: - """Clear old files from storage.""" + def execute(self) -> None: for storage_file in ("hacs",): path = f"{self.hacs.core.config_path}/.storage/{storage_file}" if os.path.isfile(path): diff --git a/custom_components/hacs/tasks/setup_constrains.py b/custom_components/hacs/tasks/setup_constrains.py index c749cdf8a74..f9690decab7 100644 --- a/custom_components/hacs/tasks/setup_constrains.py +++ b/custom_components/hacs/tasks/setup_constrains.py @@ -1,8 +1,9 @@ """"Starting setup task: Constrains".""" -from ..utils.version import version_left_higher_then_right -from ..const import MINIMUM_HA_VERSION import os + +from ..const import MINIMUM_HA_VERSION from ..enums import HacsDisabledReason, HacsStage +from ..utils.version import version_left_higher_then_right from .base import HacsTaskRuntimeBase @@ -16,14 +17,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: - if not await self.hass.async_add_executor_job(self.constrain_custom_updater): - self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS) - if not await self.hass.async_add_executor_job(self.constrain_version): - self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS) - - def constrain_custom_updater(self) -> None: - """Check if custom_updater exist.""" + def execute(self) -> None: for location in ( self.hass.config.path("custom_components/custom_updater.py"), self.hass.config.path("custom_components/custom_updater.py"), @@ -34,11 +28,8 @@ def constrain_custom_updater(self) -> None: "To use this you need to remove custom_updater form %s", location, ) - return False - return True + self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS) - def constrain_version(self) -> None: - """Check if the version is valid.""" if not version_left_higher_then_right( self.hacs.core.ha_version, MINIMUM_HA_VERSION ): @@ -46,5 +37,4 @@ def constrain_version(self) -> None: "You need HA version %s or newer to use this integration.", MINIMUM_HA_VERSION, ) - return False - return True + self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS) diff --git a/custom_components/hacs/tasks/setup_frontend.py b/custom_components/hacs/tasks/setup_frontend.py index 126d4c9cea5..8cedc037f79 100644 --- a/custom_components/hacs/tasks/setup_frontend.py +++ b/custom_components/hacs/tasks/setup_frontend.py @@ -21,7 +21,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: + def execute(self) -> None: # Register themes self.hass.http.register_static_path( diff --git a/custom_components/hacs/tasks/setup_restore.py b/custom_components/hacs/tasks/setup_restore.py index 33ec29daaec..c2292a73fab 100644 --- a/custom_components/hacs/tasks/setup_restore.py +++ b/custom_components/hacs/tasks/setup_restore.py @@ -13,6 +13,6 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: + async def async_execute(self) -> None: if not await self.hacs.data.restore(): self.hacs.disable_hacs(HacsDisabledReason.RESTORE) diff --git a/custom_components/hacs/tasks/setup_sensor.py b/custom_components/hacs/tasks/setup_sensor.py index df4728f447d..b7e27380783 100644 --- a/custom_components/hacs/tasks/setup_sensor.py +++ b/custom_components/hacs/tasks/setup_sensor.py @@ -17,7 +17,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: + async def async_execute(self) -> None: if self.hacs.configuration.config_type == ConfigurationType.YAML: self.hass.async_create_task( async_load_platform( diff --git a/custom_components/hacs/tasks/setup_verify_api.py b/custom_components/hacs/tasks/setup_verify_api.py index e606a358c27..833449703d6 100644 --- a/custom_components/hacs/tasks/setup_verify_api.py +++ b/custom_components/hacs/tasks/setup_verify_api.py @@ -13,6 +13,6 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: + async def async_execute(self) -> None: can_update = await self.hacs.async_can_update() self.log.debug("Can update %s repositories", can_update) diff --git a/custom_components/hacs/tasks/setup_websocket_api.py b/custom_components/hacs/tasks/setup_websocket_api.py index 6ce207ec240..bc7be35a888 100644 --- a/custom_components/hacs/tasks/setup_websocket_api.py +++ b/custom_components/hacs/tasks/setup_websocket_api.py @@ -25,7 +25,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.SETUP] - async def execute(self) -> None: + async def async_execute(self) -> None: async_register_command(self.hass, hacs_settings) async_register_command(self.hass, hacs_config) async_register_command(self.hass, hacs_repositories) diff --git a/custom_components/hacs/tasks/startup_load_hacs_repository.py b/custom_components/hacs/tasks/startup_load_hacs_repository.py index 8781d61b63b..1c9fd1ce146 100644 --- a/custom_components/hacs/tasks/startup_load_hacs_repository.py +++ b/custom_components/hacs/tasks/startup_load_hacs_repository.py @@ -16,7 +16,7 @@ class Task(HacsTaskRuntimeBase): stages = [HacsStage.STARTUP] - async def execute(self) -> None: + async def async_execute(self) -> None: try: repository = self.hacs.get_by_name("hacs/integration") if repository is None: diff --git a/custom_components/hacs/utils/version.py b/custom_components/hacs/utils/version.py index f7dc62303bc..9f2275a54c5 100644 --- a/custom_components/hacs/utils/version.py +++ b/custom_components/hacs/utils/version.py @@ -2,6 +2,7 @@ from functools import lru_cache + from awesomeversion import AwesomeVersion