diff --git a/custom_components/hacs/tasks/base.py b/custom_components/hacs/tasks/base.py index d53abc3b21a..2cb9f555c08 100644 --- a/custom_components/hacs/tasks/base.py +++ b/custom_components/hacs/tasks/base.py @@ -13,7 +13,7 @@ class HacsTaskBase(HacsMixin, LogMixin): - """"Hacs task base.""" + """Hacs task base.""" hass: HomeAssistant @@ -27,7 +27,7 @@ def slug(self) -> str: """Return the check slug.""" return self.__class__.__module__.rsplit(".", maxsplit=1)[-1] - async def execute_task(self) -> None: + async def execute_task(self, *_, **__) -> None: """Execute the task defined in subclass.""" if self.hacs.system.disabled: self.log.warning( @@ -60,19 +60,14 @@ async def execute_task(self) -> None: class HacsTaskEventBase(HacsTaskBase): - """"HacsTaskEventBase.""" + """HacsTaskEventBase.""" type = HacsTaskType.EVENT - - @property - @abstractmethod - def event(self) -> str: - """Return the event to listen to.""" - raise NotImplementedError + events: list[str] = [] class HacsTaskScheduleBase(HacsTaskBase): - """"HacsTaskScheduleBase.""" + """HacsTaskScheduleBase.""" type = HacsTaskType.SCHEDULE @@ -84,13 +79,13 @@ def schedule(self) -> timedelta: class HacsTaskManualBase(HacsTaskBase): - """"HacsTaskManualBase.""" + """HacsTaskManualBase.""" type = HacsTaskType.MANUAL class HacsTaskRuntimeBase(HacsTaskBase): - """"HacsTaskRuntimeBase.""" + """HacsTaskRuntimeBase.""" type = HacsTaskType.RUNTIME stages = list(HacsStage) diff --git a/custom_components/hacs/tasks/manager.py b/custom_components/hacs/tasks/manager.py index 5c99ae703f2..13417364ee5 100644 --- a/custom_components/hacs/tasks/manager.py +++ b/custom_components/hacs/tasks/manager.py @@ -39,6 +39,11 @@ async def _load_module(module: str): await asyncio.gather(*[_load_module(task) for task in task_modules]) self.log.info("Loaded %s tasks", len(self.tasks)) + for task in self.tasks: + if task.type == HacsTaskType.EVENT: + for event in task.events: + self.hacs.hass.bus.async_listen_once(event, task.execute_task) + def get(self, slug: str) -> HacsTaskBase | None: """Return a task.""" return self.__tasks.get(slug) diff --git a/custom_components/hacs/tasks/store_hacs_data.py b/custom_components/hacs/tasks/store_hacs_data.py new file mode 100644 index 00000000000..0d6bf7eb1d9 --- /dev/null +++ b/custom_components/hacs/tasks/store_hacs_data.py @@ -0,0 +1,20 @@ +""""Store HACS data.""" +from homeassistant.const import EVENT_HOMEASSISTANT_FINAL_WRITE +from .base import HacsTaskEventBase + + +async def async_setup() -> None: + """Set up this task.""" + return Task() + + +class Task(HacsTaskEventBase): + """ "Hacs task base.""" + + events = [EVENT_HOMEASSISTANT_FINAL_WRITE] + + async def async_execute(self) -> None: + if self.hacs.system.disabled: + return + + await self.hacs.data.async_write()