Add new method version_is_newer to Update platform (#124797)

* Allow string comparing in update platform

* new approach after architecture discussion

* cleanup

* Update homeassistant/components/update/__init__.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/components/update/__init__.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* add tests

* Update tests/components/update/test_init.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update tests/components/update/test_init.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update tests/components/update/test_init.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* update docstrings

* one more docstring

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/125987/head
Simone Chemelli 2024-09-19 11:00:22 +02:00 committed by GitHub
parent b787c2617b
commit c94bb6c1db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 2 deletions

View File

@ -181,7 +181,7 @@ class UpdateEntityDescription(EntityDescription, frozen_or_thawed=True):
@lru_cache(maxsize=256) @lru_cache(maxsize=256)
def _version_is_newer(latest_version: str, installed_version: str) -> bool: def _version_is_newer(latest_version: str, installed_version: str) -> bool:
"""Return True if version is newer.""" """Return True if latest_version is newer than installed_version."""
return AwesomeVersion(latest_version) > installed_version return AwesomeVersion(latest_version) > installed_version
@ -384,6 +384,11 @@ class UpdateEntity(
""" """
raise NotImplementedError raise NotImplementedError
def version_is_newer(self, latest_version: str, installed_version: str) -> bool:
"""Return True if latest_version is newer than installed_version."""
# We don't inline the `_version_is_newer` function because of caching
return _version_is_newer(latest_version, installed_version)
@property @property
@final @final
def state(self) -> str | None: def state(self) -> str | None:
@ -399,7 +404,7 @@ class UpdateEntity(
return STATE_OFF return STATE_OFF
try: try:
newer = _version_is_newer(latest_version, installed_version) newer = self.version_is_newer(latest_version, installed_version)
except AwesomeVersionCompareException: except AwesomeVersionCompareException:
# Can't compare versions, already tried exact match # Can't compare versions, already tried exact match
return STATE_ON return STATE_ON

View File

@ -3,6 +3,7 @@
from collections.abc import Generator from collections.abc import Generator
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
from awesomeversion import AwesomeVersion, AwesomeVersionStrategy
import pytest import pytest
from homeassistant.components.update import ( from homeassistant.components.update import (
@ -956,3 +957,43 @@ async def test_deprecated_supported_features_ints_with_service_call(
}, },
blocking=True, blocking=True,
) )
async def test_custom_version_is_newer(hass: HomeAssistant) -> None:
"""Test UpdateEntity with overridden version_is_newer method."""
class MockUpdateEntity(UpdateEntity):
def version_is_newer(self, latest_version: str, installed_version: str) -> bool:
"""Return True if latest_version is newer than installed_version."""
return AwesomeVersion(
latest_version,
find_first_match=True,
ensure_strategy=[AwesomeVersionStrategy.SEMVER],
) > AwesomeVersion(
installed_version,
find_first_match=True,
ensure_strategy=[AwesomeVersionStrategy.SEMVER],
)
update = MockUpdateEntity()
update.hass = hass
update.platform = MockEntityPlatform(hass)
STABLE = "20230913-111730/v1.14.0-gcb84623"
BETA = "20231107-162609/v1.14.1-rc1-g0617c15"
# Set current installed version to STABLE
update._attr_installed_version = STABLE
update._attr_latest_version = BETA
assert update.installed_version == STABLE
assert update.latest_version == BETA
assert update.state == STATE_ON
# Set current installed version to BETA
update._attr_installed_version = BETA
update._attr_latest_version = STABLE
assert update.installed_version == BETA
assert update.latest_version == STABLE
assert update.state == STATE_OFF