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

Store data on homeassistant shutdown #2178

Merged
merged 1 commit into from
Aug 27, 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
19 changes: 7 additions & 12 deletions custom_components/hacs/tasks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class HacsTaskBase(HacsMixin, LogMixin):
""""Hacs task base."""
"""Hacs task base."""

hass: HomeAssistant

Expand All @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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)
5 changes: 5 additions & 0 deletions custom_components/hacs/tasks/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions custom_components/hacs/tasks/store_hacs_data.py
Original file line number Diff line number Diff line change
@@ -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()