Cache version compare in update entity (#96978)

pull/96990/head
J. Nick Koston 2023-07-21 01:56:34 -05:00 committed by GitHub
parent 32d63ae890
commit e2394b34bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 1 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from functools import lru_cache
import logging import logging
from typing import Any, Final, final from typing import Any, Final, final
@ -182,6 +183,12 @@ class UpdateEntityDescription(EntityDescription):
entity_category: EntityCategory | None = EntityCategory.CONFIG entity_category: EntityCategory | None = EntityCategory.CONFIG
@lru_cache(maxsize=256)
def _version_is_newer(latest_version: str, installed_version: str) -> bool:
"""Return True if version is newer."""
return AwesomeVersion(latest_version) > installed_version
class UpdateEntity(RestoreEntity): class UpdateEntity(RestoreEntity):
"""Representation of an update entity.""" """Representation of an update entity."""
@ -355,7 +362,7 @@ class UpdateEntity(RestoreEntity):
return STATE_OFF return STATE_OFF
try: try:
newer = AwesomeVersion(latest_version) > installed_version newer = _version_is_newer(latest_version, installed_version)
return STATE_ON if newer else STATE_OFF return STATE_ON if newer else STATE_OFF
except AwesomeVersionCompareException: except AwesomeVersionCompareException:
# Can't compare versions, already tried exact match # Can't compare versions, already tried exact match