forked from hacs/integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
92 lines (73 loc) · 3.31 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Update entities for HACS."""
from __future__ import annotations
from typing import Any
from homeassistant.components.update import UpdateEntity
from .base import HacsBase
from .const import DOMAIN
from .entity import HacsRepositoryEntity
from .enums import HacsCategory
async def async_setup_entry(hass, _config_entry, async_add_devices):
"""Setup update platform."""
hacs: HacsBase = hass.data.get(DOMAIN)
async_add_devices(
HacsRepositoryUpdateEntity(hacs=hacs, repository=repository)
for repository in hacs.repositories.list_downloaded
)
class HacsRepositoryUpdateEntity(HacsRepositoryEntity, UpdateEntity):
"""Update entities for repositories downloaded with HACS."""
_attr_supported_features = 1 | 16
@property
def name(self) -> str | None:
return f"{self.repository.display_name} update"
@property
def latest_version(self) -> str:
"""Return latest version of the entity."""
if self.repository.pending_update:
return self.repository.display_available_version
return self.installed_version
@property
def release_url(self) -> str:
"""Return the URL of the release page."""
if self.repository.display_version_or_commit == "commit":
return f"https://github.com/{self.repository.data.full_name}"
return f"https://github.com/{self.repository.data.full_name}/releases/{self.latest_version}"
@property
def current_version(self) -> str:
"""Return downloaded version of the entity."""
return self.repository.display_installed_version
@property
def installed_version(self) -> str:
"""Return downloaded version of the entity."""
return self.repository.display_installed_version
@property
def release_summary(self) -> str | None:
"""Return the release summary."""
if self.repository.pending_restart:
return "<ha-alert alert-type='error'>Restart of Home Assistant required</ha-alert>"
return None
async def async_install(self, version: str | None, backup: bool, **kwargs: Any) -> None:
"""Install an update."""
if self.repository.display_version_or_commit == "version":
self.repository.data.selected_tag = self.latest_version
await self.repository.update_repository(force=True)
await self.repository.async_install()
async def async_release_notes(self) -> str | None:
"""Return the release notes."""
release_notes = ""
if self.repository.pending_restart:
return None
if len(self.repository.releases.objects) > 0:
release = self.repository.releases.objects[0]
release_notes += release.body
if self.repository.pending_update:
if self.repository.data.category == HacsCategory.INTEGRATION:
release_notes += (
"<ha-alert alert-type='warning'>You need to restart"
" Home Assistant manually after updating.</ha-alert>\n\n"
)
if self.repository.data.category == HacsCategory.PLUGIN:
release_notes += (
"<ha-alert alert-type='warning'>You need to manually"
" clear the frontend cache after updating.</ha-alert>\n\n"
)
return release_notes