Beispiel #1
0
async def test_hacs_data_async_write2(tmpdir):
    data = HacsData()
    hacs = get_hacs()
    hacs.hass = HomeAssistant()
    hacs.hass.config.config_dir = tmpdir
    hacs.configuration = Configuration()
    hacs.system.status.background_task = False
    hacs.system.disabled = False
    await data.async_write()
Beispiel #2
0
async def _async_common_setup(hass: HomeAssistant):
    """Common setup stages."""
    integration = await async_get_integration(hass, DOMAIN)

    hacs = get_hacs()

    hacs.enable_hacs()
    await hacs.async_set_stage(None)

    hacs.log.info(STARTUP.format(version=integration.version))

    hacs.integration = integration
    hacs.version = integration.version
    hacs.hass = hass
    hacs.data = HacsData()
    hacs.system.running = True
    hacs.session = async_create_clientsession(hass)
    hacs.tasks = HacsTaskManager(hacs=hacs, hass=hass)

    hacs.core.lovelace_mode = LovelaceMode.YAML
    try:
        lovelace_info = await system_health_info(hacs.hass)
        hacs.core.lovelace_mode = LovelaceMode(
            lovelace_info.get("mode", "yaml"))
    except Exception:  # pylint: disable=broad-except
        # If this happens, the users YAML is not valid, we assume YAML mode
        pass
    hacs.log.debug(f"Configuration type: {hacs.configuration.config_type}")
    hacs.core.config_path = hacs.hass.config.path()
    hacs.core.ha_version = HAVERSION

    await hacs.tasks.async_load()

    # Setup session for API clients
    session = async_create_clientsession(hacs.hass)

    ## Legacy GitHub client
    hacs.github = GitHub(
        hacs.configuration.token,
        session,
        headers={
            "User-Agent": f"HACS/{hacs.version}",
            "Accept": ACCEPT_HEADERS["preview"],
        },
    )

    ## New GitHub client
    hacs.githubapi = GitHubAPI(
        token=hacs.configuration.token,
        session=session,
        **{"client_name": f"HACS/{hacs.version}"},
    )

    hass.data[DOMAIN] = hacs
Beispiel #3
0
async def test_hacs_data_async_write1(tmpdir):
    data = HacsData()
    hacs = get_hacs()
    repository = dummy_repository_base()
    repository.data.installed = True
    repository.data.installed_version = "1"
    hacs.repositories = [repository]
    hacs.hass = HomeAssistant()
    hacs.hass.config.config_dir = tmpdir
    hacs.configuration = Configuration()
    await data.async_write()
Beispiel #4
0
async def hacs_startup():
    """HACS startup tasks."""
    hacs = get_hacs()

    if hacs.configuration.debug:
        try:
            await hacs.hass.services.async_call(
                "logger", "set_level", {"hacs": "debug"}
            )
            await hacs.hass.services.async_call(
                "logger", "set_level", {"queueman": "debug"}
            )
            await hacs.hass.services.async_call(
                "logger", "set_level", {"AioGitHub": "debug"}
            )
        except ServiceNotFound:
            hacs.logger.error(
                "Could not set logging level to debug, logger is not enabled"
            )

    lovelace_info = await system_health_info(hacs.hass)
    hacs.logger.debug(f"Configuration type: {hacs.configuration.config_type}")
    hacs.version = VERSION
    hacs.logger.info(STARTUP)
    hacs.system.config_path = hacs.hass.config.path()
    hacs.system.ha_version = HAVERSION

    await hacs.hass.async_add_executor_job(clear_storage)

    hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml")
    hacs.system.disabled = False
    hacs.github = GitHub(
        hacs.configuration.token, async_create_clientsession(hacs.hass)
    )
    hacs.data = HacsData()

    can_update = await get_fetch_updates_for(hacs.github)
    if can_update == 0:
        hacs.logger.info("HACS is ratelimited, repository updates will resume in 1h.")
    else:
        hacs.logger.debug(f"Can update {can_update} repositories")

    # Check HACS Constrains
    if not await hacs.hass.async_add_executor_job(check_constrains):
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Set up frontend
    await setup_frontend()

    if not await hacs.hass.async_add_executor_job(internet_connectivity_check):
        hacs.logger.critical("No network connectivity")
        return False

    # Load HACS
    if not await load_hacs_repository():
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Restore from storefiles
    if not await hacs.data.restore():
        hacs_repo = hacs.get_by_name("hacs/integration")
        hacs_repo.pending_restart = True
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Add aditional categories
    hacs.common.categories = ELEMENT_TYPES
    if hacs.configuration.appdaemon:
        hacs.common.categories.append("appdaemon")
    if hacs.configuration.netdaemon:
        hacs.common.categories.append("netdaemon")

    # Setup startup tasks
    if hacs.configuration.config_type == "yaml":
        hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, hacs.startup_tasks())
    else:
        async_call_later(hacs.hass, 5, hacs.startup_tasks())

    # Show the configuration
    hacs.configuration.print()

    # Set up sensor
    await hacs.hass.async_add_executor_job(add_sensor)

    # Mischief managed!
    return True
Beispiel #5
0
async def async_hacs_startup():
    """HACS startup tasks."""
    hacs = get_hacs()

    lovelace_info = await system_health_info(hacs.hass)
    hacs.logger.debug(f"Configuration type: {hacs.configuration.config_type}")
    hacs.version = VERSION
    hacs.logger.info(STARTUP)
    hacs.system.config_path = hacs.hass.config.path()
    hacs.system.ha_version = HAVERSION

    # Clear old storage files
    await async_clear_storage()

    # Setup websocket API
    await async_setup_hacs_websockt_api()

    hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml")
    hacs.system.disabled = False
    hacs.github = GitHub(
        hacs.configuration.token, async_create_clientsession(hacs.hass)
    )
    hacs.data = HacsData()

    can_update = await get_fetch_updates_for(hacs.github)
    if can_update is None:
        hacs.logger.critical("Your GitHub token is not valid")
        return False
    elif can_update != 0:
        hacs.logger.debug(f"Can update {can_update} repositories")
    else:
        hacs.logger.info(
            "HACS is ratelimited, repository updates will resume when the limit is cleared, this can take up to 1 hour"
        )
        return False

    # Check HACS Constrains
    if not await hacs.hass.async_add_executor_job(check_constrains):
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Load HACS
    if not await async_load_hacs_repository():
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Restore from storefiles
    if not await hacs.data.restore():
        hacs_repo = hacs.get_by_name("hacs/integration")
        hacs_repo.pending_restart = True
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Add additional categories
    hacs.common.categories = ELEMENT_TYPES
    if hacs.configuration.appdaemon:
        hacs.common.categories.append("appdaemon")
    if hacs.configuration.netdaemon:
        hacs.common.categories.append("netdaemon")

    # Set up frontend
    await async_setup_frontend()

    # Setup startup tasks
    hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _wait_for_startup)

    # Set up sensor
    await async_add_sensor()

    # Mischief managed!
    return True
Beispiel #6
0
async def async_hacs_startup():
    """HACS startup tasks."""
    hacs = get_hacs()
    hacs.hass.data[DOMAIN] = hacs

    try:
        lovelace_info = await system_health_info(hacs.hass)
    except (TypeError, KeyError, HomeAssistantError):
        # If this happens, the users YAML is not valid, we assume YAML mode
        lovelace_info = {"mode": "yaml"}
    hacs.log.debug(f"Configuration type: {hacs.configuration.config_type}")
    hacs.version = INTEGRATION_VERSION
    hacs.log.info(STARTUP)
    hacs.core.config_path = hacs.hass.config.path()
    hacs.system.ha_version = HAVERSION

    hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml")
    hacs.core.lovelace_mode = LovelaceMode(lovelace_info.get("mode", "yaml"))

    # Setup websocket API
    await async_setup_hacs_websockt_api()

    # Set up frontend
    await async_setup_frontend()

    # Clear old storage files
    await async_clear_storage()

    hacs.enable()
    hacs.github = GitHub(
        hacs.configuration.token,
        async_create_clientsession(hacs.hass),
        headers=HACS_GITHUB_API_HEADERS,
    )
    hacs.data = HacsData()

    can_update = await get_fetch_updates_for(hacs.github)
    if can_update is None:
        hacs.log.critical("Your GitHub token is not valid")
        hacs.disable(HacsDisabledReason.INVALID_TOKEN)
        return False

    if can_update != 0:
        hacs.log.debug(f"Can update {can_update} repositories")
    else:
        reset = datetime.fromtimestamp(int(hacs.github.client.ratelimits.reset))
        hacs.log.error(
            "HACS is ratelimited, HACS will resume setup when the limit is cleared (%02d:%02d:%02d)",
            reset.hour,
            reset.minute,
            reset.second,
        )
        hacs.disable(HacsDisabledReason.RATE_LIMIT)
        return False

    # Check HACS Constrains
    if not await hacs.hass.async_add_executor_job(check_constrains):
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        hacs.disable(HacsDisabledReason.CONSTRAINS)
        return False

    # Load HACS
    if not await async_load_hacs_repository():
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        hacs.disable(HacsDisabledReason.LOAD_HACS)
        return False

    # Restore from storefiles
    if not await hacs.data.restore():
        hacs_repo = hacs.get_by_name("hacs/integration")
        hacs_repo.pending_restart = True
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        hacs.disable(HacsDisabledReason.RESTORE)
        return False

    # Setup startup tasks
    if hacs.hass.state == CoreState.running:
        async_call_later(hacs.hass, 5, hacs.startup_tasks)
    else:
        hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, hacs.startup_tasks)

    # Set up sensor
    await async_add_sensor()

    # Mischief managed!
    await hacs.async_set_stage(HacsStage.WAITING)
    hacs.log.info(
        "Setup complete, waiting for Home Assistant before startup tasks starts"
    )
    return True
Beispiel #7
0
async def async_hacs_startup():
    """HACS startup tasks."""
    hacs = get_hacs()

    lovelace_info = await system_health_info(hacs.hass)
    hacs.log.debug(f"Configuration type: {hacs.configuration.config_type}")
    hacs.version = VERSION
    hacs.log.info(STARTUP)
    hacs.system.config_path = hacs.hass.config.path()
    hacs.system.ha_version = HAVERSION

    # Setup websocket API
    await async_setup_hacs_websockt_api()

    # Set up frontend
    await async_setup_frontend()

    # Clear old storage files
    await async_clear_storage()

    hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml")
    hacs.system.disabled = False
    hacs.github = GitHub(hacs.configuration.token,
                         async_create_clientsession(hacs.hass))
    hacs.data = HacsData()

    can_update = await get_fetch_updates_for(hacs.github)
    if can_update is None:
        hacs.log.critical("Your GitHub token is not valid")
        return False

    if can_update != 0:
        hacs.log.debug(f"Can update {can_update} repositories")
    else:
        hacs.log.info(
            "HACS is ratelimited, repository updates will resume when the limit is cleared, this can take up to 1 hour"
        )
        return False

    # Check HACS Constrains
    if not await hacs.hass.async_add_executor_job(check_constrains):
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass,
                                         hacs.configuration.config_entry)
        return False

    # Load HACS
    if not await async_load_hacs_repository():
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass,
                                         hacs.configuration.config_entry)
        return False

    # Restore from storefiles
    if not await hacs.data.restore():
        hacs_repo = hacs.get_by_name("hacs/integration")
        hacs_repo.pending_restart = True
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass,
                                         hacs.configuration.config_entry)
        return False

    # Setup startup tasks
    if hacs.status.new:
        if int(hacs.system.ha_version.split(".")[1]) >= 117:
            async_call_later(hacs.hass, 5, hacs.startup_tasks)
        else:
            async_call_later(hacs.hass, 5, hacs.startup_tasks())
    else:
        hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED,
                                        hacs.startup_tasks)

    # Set up sensor
    await async_add_sensor()

    # Mischief managed!
    await hacs.async_set_stage(HacsStage.WAITING)
    hacs.log.info(
        "Setup complete, waiting for Home Assistant before startup tasks starts"
    )
    return True
Beispiel #8
0
async def test_hacs_data_restore(tmpdir):
    data = HacsData()
    hacs = get_hacs()
    hacs.hass = HomeAssistant()
    hacs.hass.config.config_dir = tmpdir
    await data.restore()
async def hacs_startup():
    """HACS startup tasks."""
    hacs = get_hacs()
    if not check_requirements():
        return False
    if hacs.configuration.debug:
        try:
            await hacs.hass.services.async_call(
                "logger", "set_level", {"hacs": "debug"}
            )
        except ServiceNotFound:
            hacs.logger.error(
                "Could not set logging level to debug, logger is not enabled"
            )

    lovelace_info = await system_health_info(hacs.hass)
    hacs.logger.debug(f"Configuration type: {hacs.configuration.config_type}")
    hacs.version = VERSION
    hacs.logger.info(STARTUP)
    hacs.system.config_path = hacs.hass.config.path()
    hacs.system.ha_version = HAVERSION

    hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml")
    hacs.system.disabled = False
    hacs.github = AIOGitHub(
        hacs.configuration.token, async_create_clientsession(hacs.hass)
    )
    hacs.data = HacsData()

    # Check HACS Constrains
    if not await hacs.hass.async_add_executor_job(check_constans):
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Set up frontend
    await setup_frontend()

    if not await hacs.hass.async_add_executor_job(internet_connectivity_check):
        hacs.logger.critical("No network connectivity")
        return False

    # Load HACS
    if not await load_hacs_repository():
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Restore from storefiles
    if not await hacs.data.restore():
        hacs_repo = hacs.get_by_name("hacs/integration")
        hacs_repo.pending_restart = True
        if hacs.configuration.config_type == "flow":
            if hacs.configuration.config_entry is not None:
                await async_remove_entry(hacs.hass, hacs.configuration.config_entry)
        return False

    # Add aditional categories
    hacs.common.categories = ELEMENT_TYPES
    if hacs.configuration.appdaemon:
        hacs.common.categories.append("appdaemon")
    if hacs.configuration.python_script:
        hacs.configuration.python_script = False
        if hacs.configuration.config_type == "yaml":
            hacs.logger.warning(
                "Configuration option 'python_script' is deprecated and you should remove it from your configuration, HACS will know if you use 'python_script' in your Home Assistant configuration, this option will be removed in a future release."
            )
    if hacs.configuration.theme:
        hacs.configuration.theme = False
        if hacs.configuration.config_type == "yaml":
            hacs.logger.warning(
                "Configuration option 'theme' is deprecated and you should remove it from your configuration, HACS will know if you use 'theme' in your Home Assistant configuration, this option will be removed in a future release."
            )

    # Setup startup tasks
    if hacs.configuration.config_type == "yaml":
        hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, hacs.startup_tasks())
    else:
        async_call_later(hacs.hass, 5, hacs.startup_tasks())

    # Show the configuration
    hacs.configuration.print()

    # Set up sensor
    await hacs.hass.async_add_executor_job(add_sensor)

    # Mischief managed!
    return True