Skip to content

Commit

Permalink
Allow tasks to be non-async (#2166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Aug 26, 2021
1 parent 189f72e commit 8de801b
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 42 deletions.
1 change: 0 additions & 1 deletion custom_components/hacs/hacsbase/hacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions custom_components/hacs/operational/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 20 additions & 10 deletions custom_components/hacs/tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 1 addition & 5 deletions custom_components/hacs/tasks/setup_clear_old_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 6 additions & 16 deletions custom_components/hacs/tasks/setup_constrains.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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"),
Expand All @@ -34,17 +28,13 @@ 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
):
self.log.critical(
"You need HA version %s or newer to use this integration.",
MINIMUM_HA_VERSION,
)
return False
return True
self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS)
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_verify_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions custom_components/hacs/utils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


from functools import lru_cache

from awesomeversion import AwesomeVersion


Expand Down

0 comments on commit 8de801b

Please sign in to comment.