Skip to content

Commit

Permalink
Cleanup add-on detection exceptions (#2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Feb 13, 2022
1 parent c927d09 commit c9293eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
15 changes: 8 additions & 7 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import asyncio
from dataclasses import asdict, dataclass, field
from datetime import timedelta
import gzip
import json
import logging
Expand All @@ -26,7 +25,6 @@
from aiohttp.client import ClientSession, ClientTimeout
from awesomeversion import AwesomeVersion
from homeassistant.core import HomeAssistant
from homeassistant.helpers.event import async_call_later
from homeassistant.loader import Integration

from .const import TV
Expand All @@ -39,16 +37,17 @@
LovelaceMode,
)
from .exceptions import (
AddonRepositoryException,
HacsException,
HacsExpectedException,
HacsRepositoryArchivedException,
HacsRepositoryExistException,
HomeAssistantCoreRepositoryException,
)
from .repositories import RERPOSITORY_CLASSES
from .utils.decode import decode_content
from .utils.logger import get_hacs_logger
from .utils.queue_manager import QueueManager
from .utils.store import async_load_from_store, async_save_to_store

if TYPE_CHECKING:
from .repositories.base import HacsRepository
Expand Down Expand Up @@ -494,10 +493,12 @@ async def async_register_repository(
raise HacsExpectedException(f"Skipping {repository_full_name}")

if repository_full_name == "home-assistant/core":
raise HacsExpectedException(
"You can not add homeassistant/core, to use core integrations "
"check the Home Assistant documentation for how to add them."
)
raise HomeAssistantCoreRepositoryException()

if repository_full_name == "home-assistant/addons" or repository_full_name.startswith(
"hassio-addons/"
):
raise AddonRepositoryException()

if category not in RERPOSITORY_CLASSES:
raise HacsException(f"{category} is not a valid repository category.")
Expand Down
24 changes: 24 additions & 0 deletions custom_components/hacs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@ class HacsRepositoryExistException(HacsException):

class HacsExecutionStillInProgress(HacsException):
"""Exception to raise if execution is still in progress."""


class AddonRepositoryException(HacsException):
"""Exception to raise when user tries to add add-on repository."""

exception_message = (
"The repository does not seem to be a integration, "
"but an add-on repository. HACS does not manage add-ons."
)

def __init__(self) -> None:
super().__init__(self.exception_message)


class HomeAssistantCoreRepositoryException(HacsException):
"""Exception to raise when user tries to add the home-assistant/core repository."""

exception_message = (
"You can not add homeassistant/core, to use core integrations "
"check the Home Assistant documentation for how to add them."
)

def __init__(self) -> None:
super().__init__(self.exception_message)
11 changes: 3 additions & 8 deletions custom_components/hacs/repositories/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.loader import async_get_custom_components

from ..enums import HacsCategory, HacsGitHubRepo, RepositoryFile
from ..exceptions import HacsException
from ..exceptions import AddonRepositoryException, HacsException
from ..utils.decode import decode_content
from ..utils.decorator import concurrent
from ..utils.filters import get_first_directory_in_directory
Expand Down Expand Up @@ -57,16 +57,11 @@ async def validate_repository(self):
name = get_first_directory_in_directory(self.tree, "custom_components")
if name is None:
if (
self.data.full_name == "home-assistant/addons"
or self.data.full_name.startswith("hassio-addons/")
or "repository.json" in self.treefiles
"repository.json" in self.treefiles
or "repository.yaml" in self.treefiles
or "repository.yml" in self.treefiles
):
raise HacsException(
"The repository does not seem to be a integration, "
"but an add-on repository. HACS does not manage add-ons."
)
raise AddonRepositoryException()
raise HacsException(
f"Repository structure for {self.ref.replace('tags/','')} is not compliant"
)
Expand Down
28 changes: 12 additions & 16 deletions tests/stories/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

from custom_components.hacs.base import HacsBase
from custom_components.hacs.enums import HacsCategory
from custom_components.hacs.exceptions import HacsException
from custom_components.hacs.exceptions import (
AddonRepositoryException,
HacsException,
HomeAssistantCoreRepositoryException,
)

from tests.sample_data import (
category_test_treefiles,
Expand Down Expand Up @@ -85,21 +89,10 @@ async def test_registration(
@pytest.mark.parametrize(
"repository_full_name,expected_message",
(
(
"home-assistant/core",
"You can not add homeassistant/core, to use core integrations "
"check the Home Assistant documentation for how to add them.",
),
(
"home-assistant/addons",
"The repository does not seem to be a integration, "
"but an add-on repository. HACS does not manage add-ons.",
),
(
"some-user/addons",
"The repository does not seem to be a integration, "
"but an add-on repository. HACS does not manage add-ons.",
),
("home-assistant/core", HomeAssistantCoreRepositoryException.exception_message),
("home-assistant/addons", AddonRepositoryException.exception_message),
("hassio-addons/some-addon", AddonRepositoryException.exception_message),
("some-user/addons", AddonRepositoryException.exception_message),
("some-user/some-invalid-repo", "Repository structure for main is not compliant"),
),
)
Expand Down Expand Up @@ -137,6 +130,9 @@ async def test_registration_issues(
"home-assistant/addons": [
{"path": "repository.json", "type": "blob"},
],
"hassio-addons/some-addon": [
{"path": "repository.json", "type": "blob"},
],
"some-user/addons": [
{"path": "repository.yaml", "type": "blob"},
],
Expand Down

0 comments on commit c9293eb

Please sign in to comment.