示例#1
0
 def __init__(self):
     """Set up HacsRepository."""
     self.hacs = get_hacs()
     self.data = RepositoryData()
     self.content = RepositoryContent()
     self.content.path = RepositoryPath()
     self.information = RepositoryInformation()
     self.repository_object = None
     self.status = RepositoryStatus()
     self.state = None
     self.force_branch = False
     self.integration_manifest = {}
     self.repository_manifest = HacsManifest.from_dict({})
     self.validate = Validate()
     self.releases = RepositoryReleases()
     self.versions = RepositoryVersions()
     self.pending_restart = False
     self.tree = []
     self.treefiles = []
     self.ref = None
     self.logger = getLogger()
示例#2
0
"""Download."""
import gzip
import os
import shutil

import aiofiles

from custom_components.hacs.utils.logger import getLogger

_LOGGER = getLogger()


async def async_save_file(location, content):
    """Save files."""
    _LOGGER.debug("Saving %s", location)
    mode = "w"
    encoding = "utf-8"
    errors = "ignore"

    if not isinstance(content, str):
        mode = "wb"
        encoding = None
        errors = None

    try:
        async with aiofiles.open(location,
                                 mode=mode,
                                 encoding=encoding,
                                 errors=errors) as outfile:
            await outfile.write(content)
            outfile.close()
async def hacs_repository(hass, connection, msg):
    """Handle get media player cover command."""
    hacs = get_hacs()
    logger = getLogger()
    data = {}
    repository = None

    repo_id = msg.get("repository")
    action = msg.get("action")
    if repo_id is None or action is None:
        return

    try:
        repository = hacs.get_by_id(repo_id)
        logger.debug(f"Running {action} for {repository.data.full_name}")

        if action == "update":
            await repository.update_repository(ignore_issues=True, force=True)
            repository.status.updated_info = True

        elif action == "install":
            repository.data.new = False
            was_installed = repository.data.installed
            await repository.async_install()
            if not was_installed:
                hass.bus.async_fire("hacs/reload", {"force": True})

        elif action == "not_new":
            repository.data.new = False

        elif action == "uninstall":
            repository.data.new = False
            await repository.update_repository(True)
            await repository.uninstall()

        elif action == "hide":
            repository.data.hide = True

        elif action == "unhide":
            repository.data.hide = False

        elif action == "show_beta":
            repository.data.show_beta = True
            await repository.update_repository()

        elif action == "hide_beta":
            repository.data.show_beta = False
            await repository.update_repository()

        elif action == "toggle_beta":
            repository.data.show_beta = not repository.data.show_beta
            await repository.update_repository()

        elif action == "delete":
            repository.data.show_beta = False
            repository.remove()

        elif action == "release_notes":
            data = [{
                "name": x.attributes["name"],
                "body": x.attributes["body"],
                "tag": x.attributes["tag_name"],
            } for x in repository.releases.objects]

        elif action == "set_version":
            if msg["version"] == repository.data.default_branch:
                repository.data.selected_tag = None
            else:
                repository.data.selected_tag = msg["version"]
            await repository.update_repository()

            hass.bus.async_fire("hacs/reload", {"force": True})

        else:
            logger.error(f"WS action '{action}' is not valid")

        await hacs.data.async_write()
        message = None
    except AIOGitHubAPIException as exception:
        message = exception
    except AttributeError as exception:
        message = f"Could not use repository with ID {repo_id} ({exception})"
    except (Exception, BaseException) as exception:  # pylint: disable=broad-except
        message = exception

    if message is not None:
        logger.error(message)
        hass.bus.async_fire("hacs/error", {"message": str(message)})

    if repository:
        repository.state = None
        connection.send_message(websocket_api.result_message(msg["id"], data))
示例#4
0
 def __init__(self):
     """Initialize."""
     self.logger = getLogger()
     self.hacs = get_hacs()
     self.content = {}
示例#5
0
async def async_setup_frontend():
    """Configure the HACS frontend elements."""
    hacs = get_hacs()
    hacs.log.info("Setup task %s", HacsSetupTask.FRONTEND)
    hass = hacs.hass

    # Register themes
    hass.http.register_static_path(f"{URL_BASE}/themes",
                                   hass.config.path("themes"))

    # Register frontend
    if hacs.configuration.frontend_repo_url:
        getLogger().warning(
            "Frontend development mode enabled. Do not run in production.")
        hass.http.register_view(HacsFrontendDev())
    else:
        #
        hass.http.register_static_path(f"{URL_BASE}/frontend",
                                       locate_dir(),
                                       cache_headers=False)

    # Custom iconset
    hass.http.register_static_path(f"{URL_BASE}/iconset.js",
                                   str(hacs.integration_dir / "iconset.js"))
    if "frontend_extra_module_url" not in hass.data:
        hass.data["frontend_extra_module_url"] = set()
    hass.data["frontend_extra_module_url"].add("/hacsfiles/iconset.js")

    # Register www/community for all other files
    use_cache = hacs.core.lovelace_mode == "storage"
    hacs.log.info(
        "%s mode, cache for /hacsfiles/: %s",
        hacs.core.lovelace_mode,
        use_cache,
    )
    hass.http.register_static_path(
        URL_BASE,
        hass.config.path("www/community"),
        cache_headers=use_cache,
    )

    hacs.frontend.version_running = FE_VERSION
    hacs.frontend.version_expected = await hass.async_add_executor_job(
        get_frontend_version)

    # Add to sidepanel
    if "hacs" not in hass.data.get("frontend_panels", {}):
        hass.components.frontend.async_register_built_in_panel(
            component_name="custom",
            sidebar_title=hacs.configuration.sidepanel_title,
            sidebar_icon=hacs.configuration.sidepanel_icon,
            frontend_url_path="hacs",
            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,
        )