|
1 | 1 | """Store client for supervisor.""" |
2 | 2 |
|
| 3 | +import re |
| 4 | + |
3 | 5 | from .client import _SupervisorComponentClient |
4 | 6 | from .const import ResponseType |
| 7 | +from .exceptions import ( |
| 8 | + AddonNotSupportedArchitectureError, |
| 9 | + AddonNotSupportedError, |
| 10 | + AddonNotSupportedHomeAssistantVersionError, |
| 11 | + AddonNotSupportedMachineTypeError, |
| 12 | + SupervisorBadRequestError, |
| 13 | +) |
5 | 14 | from .models.addons import ( |
6 | 15 | Repository, |
7 | 16 | StoreAddon, |
|
12 | 21 | StoreInfo, |
13 | 22 | ) |
14 | 23 |
|
| 24 | +RE_ADDON_UNAVAILABLE_ARCHITECTURE = re.compile( |
| 25 | + r"^Add\-on (?P<addon>\w+) not supported on this platform, " |
| 26 | + r"supported architectures: (?P<architectures>.*)$" |
| 27 | +) |
| 28 | +RE_ADDON_UNAVAILABLE_MACHINE_TYPE = re.compile( |
| 29 | + r"^Add\-on (?P<addon>\w+) not supported on this machine, " |
| 30 | + r"supported machine types: (?P<machine_types>.*)$" |
| 31 | +) |
| 32 | +RE_ADDON_UNAVAILABLE_HOME_ASSISTANT = re.compile( |
| 33 | + r"^Add\-on (?P<addon>\w+) not supported on this system, " |
| 34 | + r"requires Home Assistant version (?P<version>\S+) or greater$" |
| 35 | +) |
| 36 | + |
15 | 37 |
|
16 | 38 | class StoreClient(_SupervisorComponentClient): |
17 | 39 | """Handles store access in Supervisor.""" |
@@ -45,6 +67,37 @@ async def addon_documentation(self, addon: str) -> str: |
45 | 67 | ) |
46 | 68 | return result.data |
47 | 69 |
|
| 70 | + async def addon_availability(self, addon: str) -> None: |
| 71 | + """Determine if latest version of addon can be installed on this system. |
| 72 | +
|
| 73 | + No return means it can be. If not, raises one of the following errors: |
| 74 | + - AddonUnavailableHomeAssistantVersionError |
| 75 | + - AddonUnavailableArchitectureError |
| 76 | + - AddonUnavailableMachineTypeError |
| 77 | +
|
| 78 | + If Supervisor adds a new reason an add-on can be restricted from being |
| 79 | + installed on some systems in the future, older versions of this client |
| 80 | + will raise the generic AddonNotSupportedError for that reason. |
| 81 | + """ |
| 82 | + try: |
| 83 | + await self._client.get( |
| 84 | + f"store/addons/{addon}/availability", response_type=ResponseType.NONE |
| 85 | + ) |
| 86 | + except SupervisorBadRequestError as err: |
| 87 | + if match := RE_ADDON_UNAVAILABLE_ARCHITECTURE.match(str(err)): |
| 88 | + raise AddonNotSupportedArchitectureError( |
| 89 | + match.group("addon"), match.group("architectures"), err.job_id |
| 90 | + ) from None |
| 91 | + if match := RE_ADDON_UNAVAILABLE_HOME_ASSISTANT.match(str(err)): |
| 92 | + raise AddonNotSupportedHomeAssistantVersionError( |
| 93 | + match.group("addon"), match.group("version"), err.job_id |
| 94 | + ) from None |
| 95 | + if match := RE_ADDON_UNAVAILABLE_MACHINE_TYPE.match(str(err)): |
| 96 | + raise AddonNotSupportedMachineTypeError( |
| 97 | + match.group("addon"), match.group("machine_types"), err.job_id |
| 98 | + ) from None |
| 99 | + raise AddonNotSupportedError(str(err), err.job_id) from None |
| 100 | + |
48 | 101 | async def install_addon(self, addon: str) -> None: |
49 | 102 | """Install an addon.""" |
50 | 103 | await self._client.post(f"store/addons/{addon}/install", timeout=None) |
|
0 commit comments