Add HacsDisabledReason (#1868)

This commit is contained in:
Joakim Sørensen
2021-02-05 16:20:17 +01:00
committed by GitHub
parent 0bdb3c5ba5
commit cd7ed573a7
6 changed files with 39 additions and 9 deletions

3
.pylintrc Normal file
View File

@ -0,0 +1,3 @@
[MESSAGES CONTROL]
# pylint issue with Python 3.9 https://github.com/PyCQA/pylint/issues/3882
disable=unsubscriptable-object

View File

@ -8,7 +8,7 @@ from aiogithubapi.github import AIOGitHubAPI
from aiogithubapi.objects.repository import AIOGitHubAPIRepository
from homeassistant.core import HomeAssistant
from .enums import HacsStage
from .enums import HacsDisabledReason, HacsStage
from .helpers.functions.logger import getLogger
from .models.core import HacsCore
from .models.frontend import HacsFrontend
@ -112,3 +112,15 @@ class HacsBase(HacsBaseAttributes):
def integration_dir(self) -> pathlib.Path:
"""Return the HACS integration dir."""
return pathlib.Path(__file__).parent
def disable(self, reason: HacsDisabledReason) -> None:
"""Disable HACS."""
self.system.disabled = True
self.system.disabled_reason = reason
self.log.info("HACS is disabled - %s", reason)
def enable(self) -> None:
"""Enable HACS."""
self.system.disabled = False
self.system.disabled_reason = None
self.log.info("HACS is enabled")

View File

@ -37,3 +37,12 @@ class HacsSetupTask(str, Enum):
HACS_REPO = "Hacs Repository"
CATEGORIES = "Additional categories"
CLEAR_STORAGE = "Clear storage"
class HacsDisabledReason(str, Enum):
RATE_LIMIT = "rate_limit"
REMOVED = "removed"
INVALID_TOKEN = "invalid_token"
CONSTRAINS = "constrains"
LOAD_HACS = "load_hacs"
RESTORE = "restore"

View File

@ -1,4 +1,5 @@
"""HACS System info."""
from typing import Optional
import attr
from ..const import INTEGRATION_VERSION
@ -10,6 +11,7 @@ class HacsSystem:
"""HACS System info."""
disabled: bool = False
disabled_reason: Optional[str] = None
running: bool = False
version: str = INTEGRATION_VERSION
stage: HacsStage = attr.ib(HacsStage)

View File

@ -1,7 +1,7 @@
"""Remove HACS."""
from custom_components.hacs.share import get_hacs
from ..const import DOMAIN
from ..enums import HacsDisabledReason
from ..share import get_hacs
async def async_remove_entry(hass, config_entry):
@ -22,7 +22,6 @@ async def async_remove_entry(hass, config_entry):
hass.components.frontend.async_remove_panel("hacs")
except AttributeError:
pass
hacs.system.disabled = True
if DOMAIN in hass.data:
del hass.data[DOMAIN]
hacs.log.info("HACS is now disabled")
hacs.disable(HacsDisabledReason.REMOVED)

View File

@ -7,7 +7,7 @@ from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.event import async_call_later
from custom_components.hacs.const import DOMAIN, INTEGRATION_VERSION, STARTUP
from custom_components.hacs.enums import HacsStage
from custom_components.hacs.enums import HacsDisabledReason, HacsStage
from custom_components.hacs.hacsbase.configuration import Configuration
from custom_components.hacs.hacsbase.data import HacsData
from custom_components.hacs.helpers.functions.constrains import check_constrains
@ -94,7 +94,7 @@ async def async_startup_wrapper_for_config_entry():
if not startup_result:
hacs.system.disabled = True
raise ConfigEntryNotReady
hacs.system.disabled = False
hacs.enable()
return startup_result
@ -110,7 +110,7 @@ async def async_startup_wrapper_for_yaml(_=None):
hacs.log.info("Could not setup HACS, trying again in 15 min")
async_call_later(hacs.hass, 900, async_startup_wrapper_for_yaml)
return
hacs.system.disabled = False
hacs.enable()
async def async_hacs_startup():
@ -139,7 +139,7 @@ async def async_hacs_startup():
await async_clear_storage()
hacs.system.lovelace_mode = lovelace_info.get("mode", "yaml")
hacs.system.disabled = False
hacs.enable()
hacs.github = GitHub(
hacs.configuration.token, async_create_clientsession(hacs.hass)
)
@ -148,6 +148,7 @@ async def async_hacs_startup():
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:
@ -156,6 +157,7 @@ async def async_hacs_startup():
hacs.log.error(
"HACS is ratelimited, repository updates will resume when the limit is cleared, this can take up to 1 hour"
)
hacs.disable(HacsDisabledReason.RATE_LIMIT)
return False
# Check HACS Constrains
@ -163,6 +165,7 @@ async def async_hacs_startup():
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
@ -170,6 +173,7 @@ async def async_hacs_startup():
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
@ -179,6 +183,7 @@ async def async_hacs_startup():
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