|
| 1 | +"""Evaluation class for Core version.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from awesomeversion import ( |
| 6 | + AwesomeVersion, |
| 7 | + AwesomeVersionException, |
| 8 | + AwesomeVersionStrategy, |
| 9 | +) |
| 10 | + |
| 11 | +from ...const import CoreState |
| 12 | +from ...coresys import CoreSys |
| 13 | +from ...homeassistant.const import LANDINGPAGE |
| 14 | +from ..const import UnsupportedReason |
| 15 | +from .base import EvaluateBase |
| 16 | + |
| 17 | +_LOGGER: logging.Logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def setup(coresys: CoreSys) -> EvaluateBase: |
| 21 | + """Initialize evaluation-setup function.""" |
| 22 | + return EvaluateHomeAssistantCoreVersion(coresys) |
| 23 | + |
| 24 | + |
| 25 | +class EvaluateHomeAssistantCoreVersion(EvaluateBase): |
| 26 | + """Evaluate the Home Assistant Core version.""" |
| 27 | + |
| 28 | + @property |
| 29 | + def reason(self) -> UnsupportedReason: |
| 30 | + """Return a UnsupportedReason enum.""" |
| 31 | + return UnsupportedReason.HOME_ASSISTANT_CORE_VERSION |
| 32 | + |
| 33 | + @property |
| 34 | + def on_failure(self) -> str: |
| 35 | + """Return a string that is printed when self.evaluate is True.""" |
| 36 | + return f"Home Assistant Core version '{self.sys_homeassistant.version}' is more than 2 years old!" |
| 37 | + |
| 38 | + @property |
| 39 | + def states(self) -> list[CoreState]: |
| 40 | + """Return a list of valid states when this evaluation can run.""" |
| 41 | + return [CoreState.RUNNING, CoreState.SETUP] |
| 42 | + |
| 43 | + async def evaluate(self) -> bool: |
| 44 | + """Run evaluation.""" |
| 45 | + if not (current := self.sys_homeassistant.version) or not ( |
| 46 | + latest := self.sys_homeassistant.latest_version |
| 47 | + ): |
| 48 | + return False |
| 49 | + |
| 50 | + # Skip evaluation for landingpage version |
| 51 | + if current == LANDINGPAGE: |
| 52 | + return False |
| 53 | + |
| 54 | + try: |
| 55 | + # We use the latest known version as reference instead of current date. |
| 56 | + # This is crucial because when update information refresh is disabled due to |
| 57 | + # unsupported Core version, using date would create a permanent unsupported state. |
| 58 | + # Even if the user updates to the last known version, the system would remain |
| 59 | + # unsupported in 4+ years. By using latest known version, updating Core to the |
| 60 | + # last known version makes the system supported again, allowing update refresh. |
| 61 | + # |
| 62 | + # Home Assistant uses CalVer versioning (2024.1, 2024.2, etc.) with monthly releases. |
| 63 | + # We consider versions more than 2 years behind as unsupported. |
| 64 | + if ( |
| 65 | + latest.strategy != AwesomeVersionStrategy.CALVER |
| 66 | + or latest.year is None |
| 67 | + or latest.minor is None |
| 68 | + ): |
| 69 | + return True # Invalid latest version format |
| 70 | + |
| 71 | + # Calculate 24 months back from latest version |
| 72 | + cutoff_month = int(latest.minor) |
| 73 | + cutoff_year = int(latest.year) - 2 |
| 74 | + |
| 75 | + # Create cutoff version |
| 76 | + cutoff_version = AwesomeVersion( |
| 77 | + f"{cutoff_year}.{cutoff_month}", |
| 78 | + ensure_strategy=AwesomeVersionStrategy.CALVER, |
| 79 | + ) |
| 80 | + |
| 81 | + # Compare current version with the cutoff |
| 82 | + return current < cutoff_version |
| 83 | + |
| 84 | + except (AwesomeVersionException, ValueError, IndexError) as err: |
| 85 | + # This is run regularly, avoid log spam by logging at debug level |
| 86 | + _LOGGER.debug( |
| 87 | + "Failed to parse Home Assistant version '%s' or latest version '%s': %s", |
| 88 | + current, |
| 89 | + latest, |
| 90 | + err, |
| 91 | + ) |
| 92 | + # Consider non-parseable versions as unsupported |
| 93 | + return True |
0 commit comments