updating to latest

This commit is contained in:
root
2021-11-04 01:18:18 -04:00
parent f92b773514
commit 7eadb4c49c
153 changed files with 19015 additions and 5168 deletions

View File

@@ -0,0 +1 @@
"""Init HACS tasks."""

View File

@@ -0,0 +1,35 @@
"""Starting setup task: extra stores."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..enums import HacsCategory, HacsStage
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Set up extra stores in HACS if enabled in Home Assistant."""
stages = [HacsStage.SETUP]
async def async_execute(self) -> None:
self.hacs.common.categories = set()
for category in (HacsCategory.INTEGRATION, HacsCategory.PLUGIN):
self.hacs.enable_hacs_category(HacsCategory(category))
if HacsCategory.PYTHON_SCRIPT in self.hacs.hass.config.components:
self.hacs.enable_hacs_category(HacsCategory.PYTHON_SCRIPT)
if self.hacs.hass.services.has_service("frontend", "reload_themes"):
self.hacs.enable_hacs_category(HacsCategory.THEME)
if self.hacs.configuration.appdaemon:
self.hacs.enable_hacs_category(HacsCategory.APPDAEMON)
if self.hacs.configuration.netdaemon:
self.hacs.enable_hacs_category(HacsCategory.NETDAEMON)

View File

@@ -0,0 +1,58 @@
""""Hacs base setup task."""
# pylint: disable=abstract-method
from __future__ import annotations
from datetime import timedelta
from timeit import default_timer as timer
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..enums import HacsStage
from ..mixin import LogMixin
class HacsTask(LogMixin):
"""Hacs task base."""
hass: HomeAssistant
events: list[str] | None = None
schedule: timedelta | None = None
stages: list[HacsStage] | None = None
def __init__(self, hacs: HacsBase, hass: HomeAssistant) -> None:
self.hacs = hacs
self.hass = hass
@property
def slug(self) -> str:
"""Return the check slug."""
return self.__class__.__module__.rsplit(".", maxsplit=1)[-1]
async def execute_task(self, *_, **__) -> None:
"""Execute the task defined in subclass."""
if self.hacs.system.disabled:
self.log.warning(
"Skipping task %s, HACS is disabled - %s",
self.slug,
self.hacs.system.disabled_reason,
)
return
self.log.info("Executing task: %s", self.slug)
start_time = timer()
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
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,
)

View File

@@ -0,0 +1,43 @@
""""Starting setup task: Constrains"."""
from __future__ import annotations
import os
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..const import MINIMUM_HA_VERSION
from ..enums import HacsDisabledReason, HacsStage
from ..utils.version import version_left_higher_then_right
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Check env Constrains."""
stages = [HacsStage.SETUP]
def execute(self) -> None:
for location in (
self.hass.config.path("custom_components/custom_updater.py"),
self.hass.config.path("custom_components/custom_updater/__init__.py"),
):
if os.path.exists(location):
self.log.critical(
"This cannot be used with custom_updater. "
"To use this you need to remove custom_updater form %s",
location,
)
self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS)
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,
)
self.hacs.disable_hacs(HacsDisabledReason.CONSTRAINS)

View File

@@ -0,0 +1,28 @@
"""Starting setup task: clear storage."""
from __future__ import annotations
import os
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..enums import HacsStage
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Clear old files from storage."""
stages = [HacsStage.SETUP]
def execute(self) -> None:
for storage_file in ("hacs",):
path = f"{self.hacs.core.config_path}/.storage/{storage_file}"
if os.path.isfile(path):
self.log.info("Cleaning up old storage file: %s", path)
os.remove(path)

View File

@@ -0,0 +1,23 @@
""""Hacs base setup task."""
from __future__ import annotations
from datetime import timedelta
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
""" "Hacs task base."""
schedule = timedelta(weeks=52)
def execute(self) -> None:
self.log.debug("Hello World!")

View File

@@ -0,0 +1,40 @@
"""Starting setup task: load HACS repository."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..enums import HacsDisabledReason, HacsStage
from ..exceptions import HacsException
from ..helpers.functions.register_repository import register_repository
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Load HACS repositroy."""
stages = [HacsStage.STARTUP]
async def async_execute(self) -> None:
try:
repository = self.hacs.get_by_name("hacs/integration")
if repository is None:
await register_repository("hacs/integration", "integration")
repository = self.hacs.get_by_name("hacs/integration")
if repository is None:
raise HacsException("Unknown error")
repository.data.installed = True
repository.data.installed_version = self.hacs.integration.version
repository.data.new = False
self.hacs.repository = repository.repository_object
except HacsException as exception:
if "403" in f"{exception}":
self.log.critical("GitHub API is ratelimited, or the token is wrong.")
else:
self.log.critical("[%s] - Could not load HACS!", exception)
self.hacs.disable_hacs(HacsDisabledReason.LOAD_HACS)

View File

@@ -0,0 +1,75 @@
"""Hacs task manager."""
from __future__ import annotations
import asyncio
from importlib import import_module
from pathlib import Path
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..mixin import LogMixin
from .base import HacsTask
class HacsTaskManager(LogMixin):
"""Hacs task manager."""
def __init__(self, hacs: HacsBase, hass: HomeAssistant) -> None:
"""Initialize the setup manager class."""
self.hacs = hacs
self.hass = hass
self.__tasks: dict[str, HacsTask] = {}
@property
def tasks(self) -> list[HacsTask]:
"""Return all list of all tasks."""
return list(self.__tasks.values())
async def async_load(self) -> None:
"""Load all tasks."""
task_files = Path(__file__).parent
task_modules = (
module.stem
for module in task_files.glob("*.py")
if module.name not in ("base.py", "__init__.py", "manager.py")
)
async def _load_module(module: str):
task_module = import_module(f"{__package__}.{module}")
if task := await task_module.async_setup_task(hacs=self.hacs, hass=self.hass):
self.__tasks[task.slug] = task
await asyncio.gather(*[_load_module(task) for task in task_modules])
self.log.info("Loaded %s tasks", len(self.tasks))
schedule_tasks = len(self.hacs.recuring_tasks) == 0
for task in self.tasks:
if task.events is not None:
for event in task.events:
self.hass.bus.async_listen_once(event, task.execute_task)
if task.schedule is not None and schedule_tasks:
self.log.debug("Scheduling the %s task to run every %s", task.slug, task.schedule)
self.hacs.recuring_tasks.append(
self.hacs.hass.helpers.event.async_track_time_interval(
task.execute_task, task.schedule
)
)
def get(self, slug: str) -> HacsTask | None:
"""Return a task."""
return self.__tasks.get(slug)
async def async_execute_runtume_tasks(self) -> None:
"""Execute the the execute methods of each runtime task if the stage matches."""
self.hacs.status.background_task = True
await asyncio.gather(
*(
task.execute_task()
for task in self.tasks
if task.stages is not None and self.hacs.stage in task.stages
)
)
self.hacs.status.background_task = False

View File

@@ -0,0 +1,23 @@
""""Starting setup task: Restore"."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..enums import HacsDisabledReason, HacsStage
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Restore HACS data."""
stages = [HacsStage.SETUP]
async def async_execute(self) -> None:
if not await self.hacs.data.restore():
self.hacs.disable_hacs(HacsDisabledReason.RESTORE)

View File

@@ -0,0 +1,87 @@
""""Starting setup task: Frontend"."""
from __future__ import annotations
from hacs_frontend import locate_dir
from hacs_frontend.version import VERSION as FE_VERSION
from ..const import DOMAIN
from ..enums import HacsStage
from ..webresponses.frontend import HacsFrontendDev
from .base import HacsTask
URL_BASE = "/hacsfiles"
from homeassistant.core import HomeAssistant
from ..base import HacsBase
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Setup the HACS frontend."""
stages = [HacsStage.SETUP]
async def async_execute(self) -> None:
# Register themes
self.hass.http.register_static_path(f"{URL_BASE}/themes", self.hass.config.path("themes"))
# Register frontend
if self.hacs.configuration.frontend_repo_url:
self.log.warning("Frontend development mode enabled. Do not run in production!")
self.hass.http.register_view(HacsFrontendDev())
else:
#
self.hass.http.register_static_path(
f"{URL_BASE}/frontend", locate_dir(), cache_headers=False
)
# Custom iconset
self.hass.http.register_static_path(
f"{URL_BASE}/iconset.js", str(self.hacs.integration_dir / "iconset.js")
)
if "frontend_extra_module_url" not in self.hass.data:
self.hass.data["frontend_extra_module_url"] = set()
self.hass.data["frontend_extra_module_url"].add(f"{URL_BASE}/iconset.js")
# Register www/community for all other files
use_cache = self.hacs.core.lovelace_mode == "storage"
self.log.info(
"%s mode, cache for /hacsfiles/: %s",
self.hacs.core.lovelace_mode,
use_cache,
)
self.hass.http.register_static_path(
URL_BASE,
self.hass.config.path("www/community"),
cache_headers=use_cache,
)
self.hacs.frontend.version_running = FE_VERSION
for requirement in self.hacs.integration.requirements:
if "hacs_frontend" in requirement:
self.hacs.frontend.version_expected = requirement.split("==")[-1]
# Add to sidepanel if needed
if DOMAIN not in self.hass.data.get("frontend_panels", {}):
self.hass.components.frontend.async_register_built_in_panel(
component_name="custom",
sidebar_title=self.hacs.configuration.sidepanel_title,
sidebar_icon=self.hacs.configuration.sidepanel_icon,
frontend_url_path=DOMAIN,
config={
"_panel_custom": {
"name": "hacs-frontend",
"embed_iframe": True,
"trust_external": False,
"js_url": f"/hacsfiles/frontend/entrypoint.js?hacstag={FE_VERSION}",
}
},
require_admin=True,
)

View File

@@ -0,0 +1,31 @@
""""Starting setup task: Sensor"."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from homeassistant.helpers.discovery import async_load_platform
from ..base import HacsBase
from ..const import DOMAIN, PLATFORMS
from ..enums import ConfigurationType, HacsStage
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Setup the HACS sensor platform."""
stages = [HacsStage.SETUP]
async def async_execute(self) -> None:
if self.hacs.configuration.config_type == ConfigurationType.YAML:
self.hass.async_create_task(
async_load_platform(self.hass, "sensor", DOMAIN, {}, self.hacs.configuration.config)
)
else:
self.hass.config_entries.async_setup_platforms(
self.hacs.configuration.config_entry, PLATFORMS
)

View File

@@ -0,0 +1,42 @@
"""Register WS API endpoints for HACS."""
from __future__ import annotations
from homeassistant.components.websocket_api import async_register_command
from homeassistant.core import HomeAssistant
from ..api.acknowledge_critical_repository import acknowledge_critical_repository
from ..api.check_local_path import check_local_path
from ..api.get_critical_repositories import get_critical_repositories
from ..api.hacs_config import hacs_config
from ..api.hacs_removed import hacs_removed
from ..api.hacs_repositories import hacs_repositories
from ..api.hacs_repository import hacs_repository
from ..api.hacs_repository_data import hacs_repository_data
from ..api.hacs_settings import hacs_settings
from ..api.hacs_status import hacs_status
from ..base import HacsBase
from ..enums import HacsStage
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Setup the HACS websocket API."""
stages = [HacsStage.SETUP]
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)
async_register_command(self.hass, hacs_repository)
async_register_command(self.hass, hacs_repository_data)
async_register_command(self.hass, check_local_path)
async_register_command(self.hass, hacs_status)
async_register_command(self.hass, hacs_removed)
async_register_command(self.hass, acknowledge_critical_repository)
async_register_command(self.hass, get_critical_repositories)

View File

@@ -0,0 +1,22 @@
""""Store HACS data."""
from __future__ import annotations
from homeassistant.const import EVENT_HOMEASSISTANT_FINAL_WRITE
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
""" "Hacs task base."""
events = [EVENT_HOMEASSISTANT_FINAL_WRITE]
async def async_execute(self) -> None:
await self.hacs.data.async_write()

View File

@@ -0,0 +1,23 @@
""""Starting setup task: Verify API"."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from ..base import HacsBase
from ..enums import HacsStage
from .base import HacsTask
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
"""Set up this task."""
return Task(hacs=hacs, hass=hass)
class Task(HacsTask):
"""Verify the connection to the GitHub API."""
stages = [HacsStage.SETUP]
async def async_execute(self) -> None:
can_update = await self.hacs.async_can_update()
self.log.debug("Can update %s repositories", can_update)