Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify task base #2182

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions custom_components/hacs/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Starting setup task: extra stores."""
from ..enums import HacsCategory, HacsStage
from .base import HacsTaskRuntimeBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
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()
Expand Down
41 changes: 5 additions & 36 deletions custom_components/hacs/tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
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:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Check env Constrains."""

stages = [HacsStage.SETUP]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import os

from ..enums import HacsStage
from .base import HacsTaskRuntimeBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Clear old files from storage."""

stages = [HacsStage.SETUP]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/tasks/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
""""Hacs base setup task."""
from .base import HacsTaskManualBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
return Task()


class Task(HacsTaskManualBase):
class Task(HacsTask):
""" "Hacs task base."""

def execute(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
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:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Load HACS repositroy."""

stages = [HacsStage.STARTUP]
Expand Down
13 changes: 6 additions & 7 deletions custom_components/hacs/tasks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
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):
"""Hacs task manager."""

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())

Expand All @@ -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)

Expand All @@ -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
)
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
""""Starting setup task: Restore"."""
from ..enums import HacsDisabledReason, HacsStage
from .base import HacsTaskRuntimeBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Restore HACS data."""

stages = [HacsStage.SETUP]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/tasks/setup_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -16,7 +16,7 @@ async def async_setup() -> None:
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Setup the HACS frontend."""

stages = [HacsStage.SETUP]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/tasks/setup_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from ..const import DOMAIN, PLATFORMS
from ..enums import ConfigurationType, HacsStage
from .base import HacsTaskRuntimeBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Setup the HACS sensor platform."""

stages = [HacsStage.SETUP]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/tasks/setup_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
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:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Setup the HACS websocket API."""

stages = [HacsStage.SETUP]
Expand Down
4 changes: 2 additions & 2 deletions custom_components/hacs/tasks/store_hacs_data.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
""""Store HACS data."""
from homeassistant.const import EVENT_HOMEASSISTANT_FINAL_WRITE

from .base import HacsTaskEventBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
return Task()


class Task(HacsTaskEventBase):
class Task(HacsTask):
""" "Hacs task base."""

events = [EVENT_HOMEASSISTANT_FINAL_WRITE]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
""""Starting setup task: Verify API"."""
from ..enums import HacsStage
from .base import HacsTaskRuntimeBase
from .base import HacsTask


async def async_setup() -> None:
"""Set up this task."""
return Task()


class Task(HacsTaskRuntimeBase):
class Task(HacsTask):
"""Verify the connection to the GitHub API."""

stages = [HacsStage.SETUP]
Expand Down