From 1a1ad5920cdb7269c5c421ad83f1ec4ca3b2b00a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sat, 28 Aug 2021 14:15:10 +0200 Subject: [PATCH] Simplify task base (#2182) --- custom_components/hacs/enums.py | 10 ----- ...p_categories.py => activate_categories.py} | 6 +-- custom_components/hacs/tasks/base.py | 41 +++---------------- ...etup_constrains.py => check_constrains.py} | 4 +- ...ar_old_storage.py => clear_old_storage.py} | 4 +- custom_components/hacs/tasks/hello_world.py | 4 +- ..._repository.py => load_hacs_repository.py} | 4 +- custom_components/hacs/tasks/manager.py | 13 +++--- .../{setup_restore.py => restore_data.py} | 4 +- .../hacs/tasks/setup_frontend.py | 4 +- custom_components/hacs/tasks/setup_sensor.py | 4 +- .../hacs/tasks/setup_websocket_api.py | 4 +- .../hacs/tasks/store_hacs_data.py | 4 +- .../{setup_verify_api.py => verify_api.py} | 4 +- 14 files changed, 34 insertions(+), 76 deletions(-) rename custom_components/hacs/tasks/{setup_categories.py => activate_categories.py} (88%) rename custom_components/hacs/tasks/{setup_constrains.py => check_constrains.py} (94%) rename custom_components/hacs/tasks/{setup_clear_old_storage.py => clear_old_storage.py} (88%) rename custom_components/hacs/tasks/{startup_load_hacs_repository.py => load_hacs_repository.py} (95%) rename custom_components/hacs/tasks/{setup_restore.py => restore_data.py} (84%) rename custom_components/hacs/tasks/{setup_verify_api.py => verify_api.py} (84%) diff --git a/custom_components/hacs/enums.py b/custom_components/hacs/enums.py index 2156a47e14f..e03c93de4b9 100644 --- a/custom_components/hacs/enums.py +++ b/custom_components/hacs/enums.py @@ -28,16 +28,6 @@ class LovelaceMode(str, Enum): YAML = "yaml" -class HacsTaskType(str, Enum): - """HacsTaskType""" - - RUNTIME = "runtime" - EVENT = "event" - SCHEDULE = "schedule" - MANUAL = "manual" - BASE = "base" - - class HacsStage(str, Enum): SETUP = "setup" STARTUP = "startup" diff --git a/custom_components/hacs/tasks/setup_categories.py b/custom_components/hacs/tasks/activate_categories.py similarity index 88% rename from custom_components/hacs/tasks/setup_categories.py rename to custom_components/hacs/tasks/activate_categories.py index 36d75625091..2af1de5c486 100644 --- a/custom_components/hacs/tasks/setup_categories.py +++ b/custom_components/hacs/tasks/activate_categories.py @@ -1,6 +1,6 @@ """Starting setup task: extra stores.""" from ..enums import HacsCategory, HacsStage -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -8,10 +8,10 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Set up extra stores in HACS if enabled in Home Assistant.""" - stages = [HacsStage.SETUP, HacsStage.RUNNING] + stages = [HacsStage.SETUP] def execute(self) -> None: self.hacs.common.categories = set() diff --git a/custom_components/hacs/tasks/base.py b/custom_components/hacs/tasks/base.py index f814e656ff7..a0379b889ec 100644 --- a/custom_components/hacs/tasks/base.py +++ b/custom_components/hacs/tasks/base.py @@ -2,22 +2,23 @@ # pylint: disable=abstract-method from __future__ import annotations -from abc import abstractmethod from datetime import timedelta from timeit import default_timer as timer from homeassistant.core import HomeAssistant -from ..enums import HacsStage, HacsTaskType +from ..enums import HacsStage from ..mixin import HacsMixin, LogMixin -class HacsTaskBase(HacsMixin, LogMixin): +class HacsTask(HacsMixin, LogMixin): """Hacs task base.""" hass: HomeAssistant - type = HacsTaskType.BASE + events: list[str] | None = None + schedule: timedelta | None = None + stages: list[HacsStage] | None = None def __init__(self) -> None: self.hass = self.hacs.hass @@ -55,35 +56,3 @@ async def execute_task(self, *_, **__) -> None: self.slug, timer() - start_time, ) - - -class HacsTaskEventBase(HacsTaskBase): - """HacsTaskEventBase.""" - - type = HacsTaskType.EVENT - events: list[str] = [] - - -class HacsTaskScheduleBase(HacsTaskBase): - """HacsTaskScheduleBase.""" - - type = HacsTaskType.SCHEDULE - - @property - @abstractmethod - def schedule(self) -> timedelta: - """Return the schedule.""" - raise NotImplementedError - - -class HacsTaskManualBase(HacsTaskBase): - """HacsTaskManualBase.""" - - type = HacsTaskType.MANUAL - - -class HacsTaskRuntimeBase(HacsTaskBase): - """HacsTaskRuntimeBase.""" - - type = HacsTaskType.RUNTIME - stages = list(HacsStage) diff --git a/custom_components/hacs/tasks/setup_constrains.py b/custom_components/hacs/tasks/check_constrains.py similarity index 94% rename from custom_components/hacs/tasks/setup_constrains.py rename to custom_components/hacs/tasks/check_constrains.py index 3498872c149..b157eccaccb 100644 --- a/custom_components/hacs/tasks/setup_constrains.py +++ b/custom_components/hacs/tasks/check_constrains.py @@ -4,7 +4,7 @@ from ..const import MINIMUM_HA_VERSION from ..enums import HacsDisabledReason, HacsStage from ..utils.version import version_left_higher_then_right -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -12,7 +12,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Check env Constrains.""" stages = [HacsStage.SETUP] diff --git a/custom_components/hacs/tasks/setup_clear_old_storage.py b/custom_components/hacs/tasks/clear_old_storage.py similarity index 88% rename from custom_components/hacs/tasks/setup_clear_old_storage.py rename to custom_components/hacs/tasks/clear_old_storage.py index 422a5dd431a..3f649e1ed5e 100644 --- a/custom_components/hacs/tasks/setup_clear_old_storage.py +++ b/custom_components/hacs/tasks/clear_old_storage.py @@ -2,7 +2,7 @@ import os from ..enums import HacsStage -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -10,7 +10,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Clear old files from storage.""" stages = [HacsStage.SETUP] diff --git a/custom_components/hacs/tasks/hello_world.py b/custom_components/hacs/tasks/hello_world.py index 18cf0dc1337..8f76d0ecbfb 100644 --- a/custom_components/hacs/tasks/hello_world.py +++ b/custom_components/hacs/tasks/hello_world.py @@ -1,5 +1,5 @@ """"Hacs base setup task.""" -from .base import HacsTaskManualBase +from .base import HacsTask async def async_setup() -> None: @@ -7,7 +7,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskManualBase): +class Task(HacsTask): """ "Hacs task base.""" def execute(self) -> None: diff --git a/custom_components/hacs/tasks/startup_load_hacs_repository.py b/custom_components/hacs/tasks/load_hacs_repository.py similarity index 95% rename from custom_components/hacs/tasks/startup_load_hacs_repository.py rename to custom_components/hacs/tasks/load_hacs_repository.py index 1c9fd1ce146..b87b078ff09 100644 --- a/custom_components/hacs/tasks/startup_load_hacs_repository.py +++ b/custom_components/hacs/tasks/load_hacs_repository.py @@ -3,7 +3,7 @@ from ..exceptions import HacsException from ..helpers.functions.information import get_repository from ..helpers.functions.register_repository import register_repository -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -11,7 +11,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Load HACS repositroy.""" stages = [HacsStage.STARTUP] diff --git a/custom_components/hacs/tasks/manager.py b/custom_components/hacs/tasks/manager.py index 13417364ee5..c69351a22ce 100644 --- a/custom_components/hacs/tasks/manager.py +++ b/custom_components/hacs/tasks/manager.py @@ -5,9 +5,8 @@ from importlib import import_module from pathlib import Path -from ..enums import HacsTaskType from ..mixin import HacsMixin, LogMixin -from .base import HacsTaskBase +from .base import HacsTask class HacsTaskManager(HacsMixin, LogMixin): @@ -15,10 +14,10 @@ class HacsTaskManager(HacsMixin, LogMixin): def __init__(self) -> None: """Initialize the setup manager class.""" - self.__tasks: dict[str, HacsTaskBase] = {} + self.__tasks: dict[str, HacsTask] = {} @property - def tasks(self) -> list[HacsTaskBase]: + def tasks(self) -> list[HacsTask]: """Return all list of all tasks.""" return list(self.__tasks.values()) @@ -40,11 +39,11 @@ async def _load_module(module: str): self.log.info("Loaded %s tasks", len(self.tasks)) for task in self.tasks: - if task.type == HacsTaskType.EVENT: + if task.events is not None: for event in task.events: self.hacs.hass.bus.async_listen_once(event, task.execute_task) - def get(self, slug: str) -> HacsTaskBase | None: + def get(self, slug: str) -> HacsTask | None: """Return a task.""" return self.__tasks.get(slug) @@ -54,6 +53,6 @@ async def async_execute_runtume_tasks(self) -> None: *( task.execute_task() for task in self.tasks - if task.type == HacsTaskType.RUNTIME and self.hacs.stage in task.stages + if task.stages is not None and self.hacs.stage in task.stages ) ) diff --git a/custom_components/hacs/tasks/setup_restore.py b/custom_components/hacs/tasks/restore_data.py similarity index 84% rename from custom_components/hacs/tasks/setup_restore.py rename to custom_components/hacs/tasks/restore_data.py index c2292a73fab..9c2dbb2e37b 100644 --- a/custom_components/hacs/tasks/setup_restore.py +++ b/custom_components/hacs/tasks/restore_data.py @@ -1,6 +1,6 @@ """"Starting setup task: Restore".""" from ..enums import HacsDisabledReason, HacsStage -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -8,7 +8,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Restore HACS data.""" stages = [HacsStage.SETUP] diff --git a/custom_components/hacs/tasks/setup_frontend.py b/custom_components/hacs/tasks/setup_frontend.py index 59c6d00ea40..8e96e77b6d9 100644 --- a/custom_components/hacs/tasks/setup_frontend.py +++ b/custom_components/hacs/tasks/setup_frontend.py @@ -6,7 +6,7 @@ from ..const import DOMAIN from ..enums import HacsStage from ..webresponses.frontend import HacsFrontendDev -from .base import HacsTaskRuntimeBase +from .base import HacsTask URL_BASE = "/hacsfiles" @@ -16,7 +16,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Setup the HACS frontend.""" stages = [HacsStage.SETUP] diff --git a/custom_components/hacs/tasks/setup_sensor.py b/custom_components/hacs/tasks/setup_sensor.py index 57bf6c5fdba..fcda411ebd6 100644 --- a/custom_components/hacs/tasks/setup_sensor.py +++ b/custom_components/hacs/tasks/setup_sensor.py @@ -4,7 +4,7 @@ from ..const import DOMAIN, PLATFORMS from ..enums import ConfigurationType, HacsStage -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -12,7 +12,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Setup the HACS sensor platform.""" stages = [HacsStage.SETUP] diff --git a/custom_components/hacs/tasks/setup_websocket_api.py b/custom_components/hacs/tasks/setup_websocket_api.py index bc7be35a888..c52f67b7ad2 100644 --- a/custom_components/hacs/tasks/setup_websocket_api.py +++ b/custom_components/hacs/tasks/setup_websocket_api.py @@ -12,7 +12,7 @@ from ..api.hacs_settings import hacs_settings from ..api.hacs_status import hacs_status from ..enums import HacsStage -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -20,7 +20,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Setup the HACS websocket API.""" stages = [HacsStage.SETUP] diff --git a/custom_components/hacs/tasks/store_hacs_data.py b/custom_components/hacs/tasks/store_hacs_data.py index a4fe667417f..f92d23e5759 100644 --- a/custom_components/hacs/tasks/store_hacs_data.py +++ b/custom_components/hacs/tasks/store_hacs_data.py @@ -1,7 +1,7 @@ """"Store HACS data.""" from homeassistant.const import EVENT_HOMEASSISTANT_FINAL_WRITE -from .base import HacsTaskEventBase +from .base import HacsTask async def async_setup() -> None: @@ -9,7 +9,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskEventBase): +class Task(HacsTask): """ "Hacs task base.""" events = [EVENT_HOMEASSISTANT_FINAL_WRITE] diff --git a/custom_components/hacs/tasks/setup_verify_api.py b/custom_components/hacs/tasks/verify_api.py similarity index 84% rename from custom_components/hacs/tasks/setup_verify_api.py rename to custom_components/hacs/tasks/verify_api.py index 833449703d6..b07ac878dcd 100644 --- a/custom_components/hacs/tasks/setup_verify_api.py +++ b/custom_components/hacs/tasks/verify_api.py @@ -1,6 +1,6 @@ """"Starting setup task: Verify API".""" from ..enums import HacsStage -from .base import HacsTaskRuntimeBase +from .base import HacsTask async def async_setup() -> None: @@ -8,7 +8,7 @@ async def async_setup() -> None: return Task() -class Task(HacsTaskRuntimeBase): +class Task(HacsTask): """Verify the connection to the GitHub API.""" stages = [HacsStage.SETUP]