From e2394b34bd37bd86bec659cc2581cb5355f89c65 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 21 Jul 2023 01:56:34 -0500 Subject: [PATCH] Cache version compare in update entity (#96978) --- homeassistant/components/update/__init__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/update/__init__.py b/homeassistant/components/update/__init__.py index f788ad21098..13ab6d38e8a 100644 --- a/homeassistant/components/update/__init__.py +++ b/homeassistant/components/update/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from dataclasses import dataclass from datetime import timedelta +from functools import lru_cache import logging from typing import Any, Final, final @@ -182,6 +183,12 @@ class UpdateEntityDescription(EntityDescription): 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): """Representation of an update entity.""" @@ -355,7 +362,7 @@ class UpdateEntity(RestoreEntity): return STATE_OFF try: - newer = AwesomeVersion(latest_version) > installed_version + newer = _version_is_newer(latest_version, installed_version) return STATE_ON if newer else STATE_OFF except AwesomeVersionCompareException: # Can't compare versions, already tried exact match