diff --git a/custom_components/hacs/update.py b/custom_components/hacs/update.py index ec4014302db..3bf7edd8468 100644 --- a/custom_components/hacs/update.py +++ b/custom_components/hacs/update.py @@ -90,6 +90,10 @@ async def async_install(self, version: str | None, backup: bool, **kwargs: Any) except HacsException as exception: raise HomeAssistantError(exception) from exception + # Persist the new installed state, otherwise a restart of + # Home Assistant before the next write reverts it. + await self.hacs.data.async_write() + async def async_release_notes(self) -> str | None: """Return the release notes.""" if self.repository.pending_restart: diff --git a/tests/repositories/test_update_repository.py b/tests/repositories/test_update_repository.py index 12d7a2cc73a..b2dfb3f4883 100644 --- a/tests/repositories/test_update_repository.py +++ b/tests/repositories/test_update_repository.py @@ -8,6 +8,7 @@ import pytest from custom_components.hacs.const import DOMAIN +from custom_components.hacs.utils.store import async_load_from_store from tests.common import ( CategoryTestData, @@ -93,6 +94,47 @@ async def test_update_repository_websocket( ) +async def test_update_repository_entity_persists_installed_version( + hass: HomeAssistant, + setup_integration: Generator, +): + """Ensure an install through the update entity is written to storage.""" + hacs = get_hacs(hass) + repo = hacs.repositories.get_by_full_name( + "hacs-test-org/integration-basic") + + assert repo is not None + + repo.data.installed = True + repo.data.installed_version = "1.0.0" + + await hass.config_entries.async_reload(hacs.configuration.config_entry.entry_id) + await hass.async_block_till_done() + + # Get a new HACS instance after reload + hacs = get_hacs(hass) + repo = hacs.repositories.get_by_full_name( + "hacs-test-org/integration-basic") + + er = async_get_entity_registry(hacs.hass) + entity_id = er.async_get_entity_id("update", DOMAIN, repo.data.id) + + await hass.services.async_call( + "update", + "install", + service_data={"entity_id": entity_id, "version": "2.0.0"}, + blocking=True, + ) + + stored = await async_load_from_store(hass, "data") + stored_repo = next( + entry + for entry in stored["repositories"]["integration"] + if entry["full_name"] == "hacs-test-org/integration-basic" + ) + assert stored_repo["version_installed"] == "2.0.0" + + async def test_update_repository_entity_no_manifest( hass: HomeAssistant, setup_integration: Generator, diff --git a/tests/snapshots/api-usage/tests/repositories/test_update_repositorytest-update-repository-entity-persists-installed-version.json b/tests/snapshots/api-usage/tests/repositories/test_update_repositorytest-update-repository-entity-persists-installed-version.json new file mode 100644 index 00000000000..08672f337d2 --- /dev/null +++ b/tests/snapshots/api-usage/tests/repositories/test_update_repositorytest-update-repository-entity-persists-installed-version.json @@ -0,0 +1,26 @@ +{ + "tests/repositories/test_update_repository.py::test_update_repository_entity_persists_installed_version": { + "https://api.github.com/repos/hacs-test-org/integration-basic": 1, + "https://api.github.com/repos/hacs-test-org/integration-basic/branches/main": 1, + "https://api.github.com/repos/hacs-test-org/integration-basic/contents/custom_components/example/manifest.json": 1, + "https://api.github.com/repos/hacs-test-org/integration-basic/contents/hacs.json": 1, + "https://api.github.com/repos/hacs-test-org/integration-basic/git/trees/1.0.0": 1, + "https://api.github.com/repos/hacs-test-org/integration-basic/releases": 1, + "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://data-v2.hacs.xyz/appdaemon/data.json": 1, + "https://data-v2.hacs.xyz/critical/data.json": 1, + "https://data-v2.hacs.xyz/integration/data.json": 1, + "https://data-v2.hacs.xyz/plugin/data.json": 1, + "https://data-v2.hacs.xyz/python_script/data.json": 1, + "https://data-v2.hacs.xyz/removed/data.json": 1, + "https://data-v2.hacs.xyz/template/data.json": 1, + "https://data-v2.hacs.xyz/theme/data.json": 1, + "https://github.com/hacs-test-org/integration-basic/archive/refs/tags/2.0.0.zip": 1, + "https://raw.githubusercontent.com/hacs-test-org/integration-basic/1.0.0/README.md": 1, + "https://raw.githubusercontent.com/hacs-test-org/integration-basic/2.0.0/hacs.json": 1 + } +}