Skip to content

Trigger auto-update throuogh Core service call #5834

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jinja2==3.1.6
log-rate-limit==1.4.2
orjson==3.10.16
pulsectl==24.12.0
python-slugify==8.0.4
pyudev==0.24.3
PyYAML==6.0.2
requests==2.32.3
Expand Down
1 change: 1 addition & 0 deletions supervisor/homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class WSType(StrEnum):
SUPERVISOR_EVENT = "supervisor/event"
BACKUP_START = "backup/start"
BACKUP_END = "backup/end"
CALL_SERVICE = "call_service"


class WSEvent(StrEnum):
Expand Down
18 changes: 18 additions & 0 deletions supervisor/homeassistant/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,24 @@ async def async_send_command(self, message: dict[str, Any]) -> T | None:
raise
return None

async def async_call_service(
self,
domain: str,
service: str,
return_response: bool = False,
service_data: dict[str, Any] | None = None,
) -> None:
"""Call a Home Assistant Core service (action)."""
message = {
ATTR_TYPE: WSType.CALL_SERVICE,
"domain": domain,
"service": service,
"return_response": return_response,
"service_data": service_data or {},
}
_LOGGER.debug("Sending service call: %s", message)
return await self.async_send_command(message)

def send_message(self, message: dict[str, Any]) -> None:
"""Send a supervisor/event message."""
if self.sys_core.state in CLOSING_STATES:
Expand Down
30 changes: 18 additions & 12 deletions supervisor/misc/tasks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""A collection of tasks."""

import asyncio
from collections.abc import Awaitable
from datetime import datetime, timedelta
import logging

import slugify

from ..addons.const import ADDON_UPDATE_CONDITIONS
from ..backups.const import LOCATION_CLOUD_BACKUP
from ..const import AddonState
Expand Down Expand Up @@ -106,7 +106,6 @@ async def load(self):
)
async def _update_addons(self):
"""Check if an update is available for an Add-on and update it."""
start_tasks: list[Awaitable[None]] = []
for addon in self.sys_addons.all:
if not addon.is_installed or not addon.auto_update:
continue
Expand All @@ -131,16 +130,23 @@ async def _update_addons(self):
)
continue

# Run Add-on update sequential
# avoid issue on slow IO
_LOGGER.info("Add-on auto update process %s", addon.slug)
try:
if start_task := await self.sys_addons.update(addon.slug, backup=True):
start_tasks.append(start_task)
except AddonsError:
_LOGGER.error("Can't auto update Add-on %s", addon.slug)

await asyncio.gather(*start_tasks)
# Call Home Assistant Core to update add-on to make sure that backups
# get created through the Home Assistant Core API (categorized correctly).
# Ultimately this should be handled by Home Assistant Core itself through
# the update entity.
entity_id = f"update.{slugify.slugify(addon.name, separator='_')}_update"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks if users rename the add-on update entities.

To me, it seems rather brittle to call Core here.

What I'd suggest is to extend the SupervisorAddonUpdateEntity class in Core instead. We are missing the breaking version info currently, but that can be added. Since we ultimately like to have auto-updating entities on Core end this seems like the right direction to me.

result = await self.sys_homeassistant.websocket.async_call_service(
domain="update",
service="install",
service_data={
"entity_id": entity_id,
"backup": True,
},
)
_LOGGER.info(
"Add-on auto update for %s processed, result %s", addon.slug, result
)

@Job(
name="tasks_update_supervisor",
Expand Down
Loading