Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new API structure to get hacs.json #2393

Merged
merged 1 commit into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 28 additions & 6 deletions custom_components/hacs/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import attr
from homeassistant.helpers.json import JSONEncoder

from ..enums import RepositoryFile
from ..exceptions import (
HacsException,
HacsNotModifiedException,
HacsRepositoryExistException,
)
from ..utils.backup import Backup, BackupNetDaemon
from ..utils.decode import decode_content
from ..utils.download import async_download_file, download_content
from ..utils.information import get_info_md_content
from ..utils.logger import getLogger
Expand Down Expand Up @@ -104,7 +106,10 @@ def name(self):

def to_json(self):
"""Export to json."""
return attr.asdict(self, filter=lambda attr, _: attr.name != "_storage_data")
return attr.asdict(
self,
filter=lambda attr, _: attr.name != "_storage_data" and attr.name != "last_fetched",
)

def memorize_storage(self, data) -> None:
"""Memorize the storage data."""
Expand Down Expand Up @@ -199,6 +204,10 @@ class HacsManifest:
iot_class: str = None
render_readme: bool = False

def to_dict(self):
"""Export to json."""
return attr.asdict(self)

@staticmethod
def from_dict(manifest: dict):
"""Set attributes from dicts."""
Expand Down Expand Up @@ -452,6 +461,9 @@ def can_download(self) -> bool:
return False
return True

async def update_repository(self, ignore_issues=False, force=False) -> None:
"""Update the repository"""

async def common_validate(self, ignore_issues=False) -> None:
"""Common validation steps of the repository."""
await common_validate(self, ignore_issues)
Expand Down Expand Up @@ -585,15 +597,25 @@ async def download_content(

async def get_repository_manifest_content(self) -> None:
"""Get the content of the hacs.json file."""
if not "hacs.json" in [x.filename for x in self.tree]:
if not RepositoryFile.HACS_JSON in [x.filename for x in self.tree]:
return

self.ref = version_to_download(self)
if manifest := await self.async_get_hacs_json():
self.repository_manifest = HacsManifest.from_dict(manifest)
self.data.update_data(self.repository_manifest.to_dict())

async def async_get_hacs_json(self, ref: str = None) -> dict[str, Any] | None:
"""Get the content of the hacs.json file."""
ref = ref or version_to_download(self)

try:
manifest = await self.repository_object.get_contents("hacs.json", self.ref)
self.repository_manifest = HacsManifest.from_dict(json.loads(manifest.content))
self.data.update_data(json.loads(manifest.content))
response = await self.hacs.async_github_api_method(
method=self.hacs.githubapi.repos.contents.get,
repository=self.data.full_name,
path=RepositoryFile.HACS_JSON,
**{"params": {"ref": ref}},
)
return json.loads(decode_content(response.data.content))
except BaseException: # pylint: disable=broad-except
pass

Expand Down
2 changes: 1 addition & 1 deletion custom_components/hacs/tasks/setup_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async def hacs_repository_data(hass, connection, msg):

elif action == "set_version":
repository.data.selected_tag = data
await repository.update_repository()
await repository.update_repository(force=True)

repository.state = None

Expand Down