Skip to content

Commit

Permalink
Raise an error when update service target the downloaded version (#3984)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Aug 19, 2024
1 parent a34d9c4 commit 2e81517
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
4 changes: 4 additions & 0 deletions custom_components/hacs/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def entity_picture(self) -> str | None:

async def async_install(self, version: str | None, backup: bool, **kwargs: Any) -> None:
"""Install an update."""
to_download = version or self.latest_version
if to_download == self.installed_version:
raise HomeAssistantError(f"Version {self.installed_version} of {
self.repository.data.full_name} is already downloaded")
try:
await self.repository.async_download_repository(ref=version or self.latest_version)
except HacsException as exception:
Expand Down
75 changes: 73 additions & 2 deletions tests/repositories/test_update_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def test_update_repository_entity(
await snapshots.assert_hacs_data(
hacs,
f"{category_test_data['repository']
}/test_update_repository_entity.json",
}/test_update_repository_entity.json",
)


Expand Down Expand Up @@ -87,7 +87,7 @@ async def test_update_repository_websocket(
await snapshots.assert_hacs_data(
hacs,
f"{category_test_data['repository']
}/test_update_repository_websocket.json",
}/test_update_repository_websocket.json",
)


Expand Down Expand Up @@ -256,3 +256,74 @@ async def test_update_repository_entity_download_failure(
service_data={"entity_id": entity_id, "version": "2.0.0"},
blocking=True,
)


async def test_update_repository_entity_same_provided_version(
hass: HomeAssistant, setup_integration: Generator
):
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 = "2.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)

er = async_get_entity_registry(hacs.hass)

entity_id = er.async_get_entity_id("update", DOMAIN, repo.data.id)

with pytest.raises(
HomeAssistantError,
match=re.escape(
"Version 2.0.0 of hacs-test-org/integration-basic is already downloaded",
),
):
await hass.services.async_call(
"update",
"install",
service_data={"entity_id": entity_id, "version": "2.0.0"},
blocking=True,
)


async def test_update_repository_entity_no_update(
hass: HomeAssistant,
setup_integration: Generator,
):
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)

er = async_get_entity_registry(hacs.hass)

entity_id = er.async_get_entity_id("update", DOMAIN, repo.data.id)

with pytest.raises(
HomeAssistantError,
match=re.escape(
"No update available for update.basic_integration_update",
),
):
await hass.services.async_call(
"update",
"install",
service_data={"entity_id": entity_id},
blocking=True,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"tests/repositories/test_update_repository.py::test_update_repository_entity_no_update": {
"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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"tests/repositories/test_update_repository.py::test_update_repository_entity_same_provided_version": {
"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
}
}

0 comments on commit 2e81517

Please sign in to comment.