From 16f2f630810d2d8751af266e6e8d3aa90f4ee787 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Sat, 19 Jun 2021 18:50:54 +0200 Subject: [PATCH] Allow downgrade to "hassos" from "haos" (#2970) * Add "os_name" as possible URL variable * Replace "os_name" for downgrades to OS versions before 6.0 --- supervisor/hassos.py | 21 +++++++++++++++++---- tests/test_hassos.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/supervisor/hassos.py b/supervisor/hassos.py index b29fe9990..41574aac2 100644 --- a/supervisor/hassos.py +++ b/supervisor/hassos.py @@ -26,6 +26,7 @@ class HassOS(CoreSysAttributes): self._available: bool = False self._version: Optional[AwesomeVersion] = None self._board: Optional[str] = None + self._os_name: Optional[str] = None @property def available(self) -> bool: @@ -55,18 +56,30 @@ class HassOS(CoreSysAttributes): """Return board name.""" return self._board + @property + def os_name(self) -> Optional[str]: + """Return OS name.""" + return self._os_name + def _get_download_url(self, version: AwesomeVersion) -> str: raw_url = self.sys_updater.ota_url if raw_url is None: raise HassOSUpdateError("Don't have an URL for OTA updates!", _LOGGER.error) update_board = self.board + update_os_name = self.os_name # OS version 6 and later renamed intel-nuc to generic-x86-64... if update_board == "intel-nuc" and version >= 6.0: update_board = "generic-x86-64" - url = raw_url.format(version=str(version), board=update_board) + # The OS name used to be hassos before renaming to haos... + if version < 6.0: + update_os_name = "hassos" + + url = raw_url.format( + version=str(version), board=update_board, os_name=update_os_name + ) return url async def _download_raucb(self, url: str, raucb: Path) -> None: @@ -115,13 +128,13 @@ class HassOS(CoreSysAttributes): except NotImplementedError: _LOGGER.info("No Home Assistant Operating System found") return - else: - self._available = True - self.sys_host.supported_features.cache_clear() # Store meta data + self._available = True + self.sys_host.supported_features.cache_clear() self._version = AwesomeVersion(cpe.get_version()[0]) self._board = cpe.get_target_hardware()[0] + self._os_name = cpe.get_product()[0] await self.sys_dbus.rauc.update() diff --git a/tests/test_hassos.py b/tests/test_hassos.py index 648aef641..4b8606903 100644 --- a/tests/test_hassos.py +++ b/tests/test_hassos.py @@ -20,3 +20,39 @@ async def test_ota_url_generic_x86_64_rename(coresys: CoreSys) -> None: url = coresys.updater.ota_url.format(version=str(version6), board="generic-x86-64") assert coresys.hassos._get_download_url(version6) == url + + +def test_ota_url_os_name(coresys: CoreSys) -> None: + """Test download URL generated with os_name.""" + + board = "generic-x86-64" + os_name = "haos" + versionstr = "6.0" + + url = "https://github.com/home-assistant/operating-system/releases/download/{version}/{os_name}_{board}-{version}.raucb" + url_formatted = url.format(version=versionstr, board=board, os_name=os_name) + + coresys.hassos._board = board + coresys.hassos._os_name = os_name + coresys.updater._data = {"ota": url} + + url = coresys.hassos._get_download_url(AwesomeVersion(versionstr)) + assert url == url_formatted + + +def test_ota_url_os_name_rel_5_downgrade(coresys: CoreSys) -> None: + """Test download URL generated with os_name.""" + + board = "generic-x86-64" + versionstr = "5.9" + + # On downgrade below 6.0 we need to use hassos as os_name. + url = "https://github.com/home-assistant/operating-system/releases/download/{version}/{os_name}_{board}-{version}.raucb" + url_formatted = url.format(version=versionstr, board=board, os_name="hassos") + + coresys.hassos._board = board + coresys.hassos._os_name = "haos" + coresys.updater._data = {"ota": url} + + url = coresys.hassos._get_download_url(AwesomeVersion(versionstr)) + assert url == url_formatted