Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion custom_components/hacs/validate/brands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..repositories.base import HacsRepository

URL = "https://brands.home-assistant.io/domains.json"
ASSET_FILENAME = "icon.png"


async def async_setup_validator(repository: HacsRepository) -> Validator:
Expand All @@ -26,10 +27,31 @@ class Validator(ActionValidationBase):
async def async_validate(self) -> None:
"""Validate the repository."""

treefiles = self.repository.treefiles

if self.repository.repository_manifest.content_in_root:
asset_path = f"brands/{ASSET_FILENAME}"
else:
asset_path = f"{self.repository.content.path.remote}/brands/{ASSET_FILENAME}"

# Check if the integraiton provides local brands assets
Comment thread
ludeeus marked this conversation as resolved.
Outdated
if asset_path in treefiles:
self.repository.logger.debug(
"The repository contains the required asset: %s", asset_path
)
return

self.repository.logger.warning(
"The repository does not contain: %s. Falling back to checking the brands repository.",
asset_path,
)

# Fallback the checking the Home Assistant brands repository for the domain
Comment thread
ludeeus marked this conversation as resolved.
response = await self.hacs.session.get(URL)
content = await response.json()

if self.repository.data.domain not in content["custom"]:
raise ValidationException(
"The repository has not been added as a custom domain to the brands repo"
"The repository does not provide brands assets "
"and is not listed in the Home Assistant brands repository."
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests/validate/test_brands_check.py::test_local_brands_asset_content_in_root": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tests/validate/test_brands_check.py::test_local_brands_asset_missing_falls_back_to_remote": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1,
"https://brands.home-assistant.io/domains.json": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tests/validate/test_brands_check.py::test_local_brands_asset_not_in_root": {
"https://api.github.com/repos/hacs/integration": 1,
"https://api.github.com/repos/hacs/integration/contents/custom_components/hacs/manifest.json": 1,
"https://api.github.com/repos/hacs/integration/contents/hacs.json": 1,
"https://api.github.com/repos/hacs/integration/git/trees/main": 1,
"https://api.github.com/repos/hacs/integration/releases": 1
}
}
46 changes: 44 additions & 2 deletions tests/validate/test_brands_check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from custom_components.hacs.validate.brands import Validator
from custom_components.hacs.repositories.base import HacsManifest
from custom_components.hacs.validate.brands import ASSET_FILENAME, Validator

from tests.common import MockedResponse, ResponseMocker

Expand All @@ -16,9 +17,50 @@ async def test_added_to_brands(repository, response_mocker: ResponseMocker):

async def test_not_added_to_brands(repository, response_mocker: ResponseMocker):
response_mocker.add(
"https://brands.home-assistant.io/domains.json", MockedResponse(content={"custom": []}),
"https://brands.home-assistant.io/domains.json", MockedResponse(content={
"custom": []}),
Comment thread
ludeeus marked this conversation as resolved.
Outdated
)
repository.data.domain = "test"
check = Validator(repository)
await check.execute_validation()
assert check.failed


async def test_local_brands_asset_content_in_root(repository):
"""Test that validation passes when the brands asset exists locally with content_in_root."""
repository.repository_manifest = HacsManifest.from_dict(
{"content_in_root": True})
repository.treefiles = [f"brands/{ASSET_FILENAME}"]
repository.data.domain = "test"
check = Validator(repository)
await check.execute_validation()
assert not check.failed


async def test_local_brands_asset_not_in_root(repository):
"""Test that validation passes when the brands asset exists locally in a subdirectory."""
repository.repository_manifest = HacsManifest.from_dict(
{"content_in_root": False})
repository.content.path.remote = "custom_components/test"
repository.treefiles = [f"custom_components/test/brands/{ASSET_FILENAME}"]
repository.data.domain = "test"
check = Validator(repository)
await check.execute_validation()
assert not check.failed


async def test_local_brands_asset_missing_falls_back_to_remote(
repository, response_mocker: ResponseMocker
):
"""Test that when local asset is missing, it falls back to checking the brands repo."""
repository.repository_manifest = HacsManifest.from_dict(
{"content_in_root": True})
repository.treefiles = []
repository.data.domain = "test"
response_mocker.add(
"https://brands.home-assistant.io/domains.json",
MockedResponse(content={"custom": ["test"]}),
)
check = Validator(repository)
await check.execute_validation()
assert not check.failed
Loading