Updated HACS and also fixed Garadget #727

This commit is contained in:
ccostan
2020-04-09 21:29:27 -04:00
parent 51aab60dea
commit e6e0d442e9
65 changed files with 1510 additions and 1047 deletions

105
config/custom_components/hacs/__init__.py Executable file → Normal file
View File

@@ -4,6 +4,7 @@ Custom element manager for community created elements.
For more details about this integration, please refer to the documentation at
https://hacs.xyz/
"""
import voluptuous as vol
from aiogithubapi import AIOGitHub
from homeassistant import config_entries
@@ -14,13 +15,23 @@ from homeassistant.exceptions import ConfigEntryNotReady, ServiceNotFound
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.event import async_call_later
from .configuration_schema import hacs_base_config_schema, hacs_config_option_schema
from .const import DOMAIN, ELEMENT_TYPES, STARTUP, VERSION
from .constrains import check_constans
from .hacsbase import Hacs
from .hacsbase.configuration import Configuration
from .hacsbase.data import HacsData
from .setup import add_sensor, load_hacs_repository, setup_frontend
from custom_components.hacs.configuration_schema import (
hacs_base_config_schema,
hacs_config_option_schema,
)
from custom_components.hacs.const import DOMAIN, ELEMENT_TYPES, STARTUP, VERSION
from custom_components.hacs.constrains import check_constans, check_requirements
from custom_components.hacs.hacsbase.configuration import Configuration
from custom_components.hacs.hacsbase.data import HacsData
from custom_components.hacs.setup import (
add_sensor,
load_hacs_repository,
setup_frontend,
)
from custom_components.hacs.globals import get_hacs
from custom_components.hacs.helpers.network import internet_connectivity_check
SCHEMA = hacs_base_config_schema()
SCHEMA[vol.Optional("options")] = hacs_config_option_schema()
@@ -29,16 +40,18 @@ CONFIG_SCHEMA = vol.Schema({DOMAIN: SCHEMA}, extra=vol.ALLOW_EXTRA)
async def async_setup(hass, config):
"""Set up this integration using yaml."""
hacs = get_hacs()
if DOMAIN not in config:
return True
hass.data[DOMAIN] = config
Hacs.hass = hass
Hacs.configuration = Configuration.from_dict(
hacs.hass = hass
hacs.session = async_create_clientsession(hass)
hacs.configuration = Configuration.from_dict(
config[DOMAIN], config[DOMAIN].get("options")
)
Hacs.configuration.config = config
Hacs.configuration.config_type = "yaml"
await startup_wrapper_for_yaml(Hacs)
hacs.configuration.config = config
hacs.configuration.config_type = "yaml"
await startup_wrapper_for_yaml()
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
@@ -49,6 +62,7 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, config_entry):
"""Set up this integration using UI."""
hacs = get_hacs()
conf = hass.data.get(DOMAIN)
if config_entry.source == config_entries.SOURCE_IMPORT:
if conf is None:
@@ -56,25 +70,26 @@ async def async_setup_entry(hass, config_entry):
hass.config_entries.async_remove(config_entry.entry_id)
)
return False
Hacs.hass = hass
Hacs.configuration = Configuration.from_dict(
hacs.hass = hass
hacs.session = async_create_clientsession(hass)
hacs.configuration = Configuration.from_dict(
config_entry.data, config_entry.options
)
Hacs.configuration.config_type = "flow"
Hacs.configuration.config_entry = config_entry
hacs.configuration.config_type = "flow"
hacs.configuration.config_entry = config_entry
config_entry.add_update_listener(reload_hacs)
startup_result = await hacs_startup(Hacs)
startup_result = await hacs_startup()
if not startup_result:
Hacs.system.disabled = True
hacs.system.disabled = True
raise ConfigEntryNotReady
Hacs.system.disabled = False
hacs.system.disabled = False
return startup_result
async def startup_wrapper_for_yaml(hacs):
async def startup_wrapper_for_yaml():
"""Startup wrapper for yaml config."""
startup_result = await hacs_startup(hacs)
hacs = get_hacs()
startup_result = await hacs_startup()
if not startup_result:
hacs.system.disabled = True
hacs.hass.components.frontend.async_remove_panel(
@@ -83,13 +98,16 @@ async def startup_wrapper_for_yaml(hacs):
.replace("-", "_")
)
hacs.logger.info("Could not setup HACS, trying again in 15 min")
async_call_later(hacs.hass, 900, startup_wrapper_for_yaml(hacs))
async_call_later(hacs.hass, 900, startup_wrapper_for_yaml())
return
hacs.system.disabled = False
async def hacs_startup(hacs):
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(
@@ -115,20 +133,21 @@ async def hacs_startup(hacs):
hacs.data = HacsData()
# Check HACS Constrains
if not await hacs.hass.async_add_executor_job(check_constans, hacs):
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(hacs)
await setup_frontend()
# Set up sensor
await hacs.hass.async_add_executor_job(add_sensor, hacs)
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(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)
@@ -136,7 +155,7 @@ async def hacs_startup(hacs):
# Restore from storefiles
if not await hacs.data.restore():
hacs_repo = hacs().get_by_name("hacs/integration")
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:
@@ -147,6 +166,8 @@ async def hacs_startup(hacs):
hacs.common.categories = ELEMENT_TYPES
if hacs.configuration.appdaemon:
hacs.common.categories.append("appdaemon")
if hacs.configuration.netdaemon:
hacs.common.categories.append("netdaemon")
if hacs.configuration.python_script:
hacs.configuration.python_script = False
if hacs.configuration.config_type == "yaml":
@@ -162,37 +183,39 @@ async def hacs_startup(hacs):
# Setup startup tasks
if hacs.configuration.config_type == "yaml":
hacs.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, hacs().startup_tasks()
)
hacs.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, hacs.startup_tasks())
else:
async_call_later(hacs.hass, 5, hacs().startup_tasks())
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
async def async_remove_entry(hass, config_entry):
"""Handle removal of an entry."""
Hacs().logger.info("Disabling HACS")
Hacs().logger.info("Removing recuring tasks")
for task in Hacs().recuring_tasks:
hacs = get_hacs()
hacs.logger.info("Disabling HACS")
hacs.logger.info("Removing recuring tasks")
for task in hacs.recuring_tasks:
task()
Hacs().logger.info("Removing sensor")
hacs.logger.info("Removing sensor")
try:
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
except ValueError:
pass
Hacs().logger.info("Removing sidepanel")
hacs.logger.info("Removing sidepanel")
try:
hass.components.frontend.async_remove_panel("hacs")
except AttributeError:
pass
Hacs().system.disabled = True
Hacs().logger.info("HACS is now disabled")
hacs.system.disabled = True
hacs.logger.info("HACS is now disabled")
async def reload_hacs(hass, config_entry):