|
5 | 5 |
|
6 | 6 | import asyncio |
7 | 7 | import logging |
| 8 | +import os |
8 | 9 |
|
9 | 10 | import aiohttp |
10 | 11 | import dwd_global_radiation as dgr |
|
14 | 15 | from homeassistant.const import Platform |
15 | 16 | from homeassistant.core import HomeAssistant |
16 | 17 | from homeassistant.exceptions import ConfigEntryNotReady |
| 18 | +from homeassistant.helpers.aiohttp_client import async_get_clientsession |
17 | 19 |
|
18 | 20 | from .api_client import DWDGlobalRadAPIClient |
19 | 21 | from .const import DOMAIN |
|
33 | 35 |
|
34 | 36 | async def get_addon_config(hass: HomeAssistant, addon_slug: str): |
35 | 37 | """Fetch add-on configuration from Supervisor.""" |
36 | | - supervisor_token = hass.data["hassio_user"]["access_token"] |
37 | | - if not supervisor_token: |
38 | | - raise ConfigEntryNotReady("No supervisor token found") |
39 | | - |
40 | 38 | try: |
41 | | - async with aiohttp.ClientSession() as session: |
42 | | - url = f"http://supervisor/addons/{addon_slug}/info" |
43 | | - headers = {"Authorization": f"Bearer {supervisor_token}"} |
44 | | - async with session.get(url, headers=headers) as response: |
45 | | - if response.status != 200: |
46 | | - raise ConfigEntryNotReady( |
47 | | - f"Failed to fetch add-on info: {response.status}" |
48 | | - ) |
49 | | - addon_info = await response.json() |
50 | | - options = addon_info.get("options", {}) |
51 | | - return options |
| 39 | + supervisor_token = os.getenv("SUPERVISOR_TOKEN") |
| 40 | + if not supervisor_token: |
| 41 | + _LOGGER.error("Supervisor token not found in environment variables") |
| 42 | + raise ConfigEntryNotReady( |
| 43 | + "Supervisor token not found in environment variables" |
| 44 | + ) |
| 45 | + |
| 46 | + url = f"http://supervisor/addons/{addon_slug}/info" |
| 47 | + headers = { |
| 48 | + "Authorization": f"Bearer {supervisor_token}", |
| 49 | + "Content-Type": "application/json", |
| 50 | + } |
| 51 | + |
| 52 | + session = async_get_clientsession(hass) |
| 53 | + async with session.get(url, headers=headers) as response: |
| 54 | + if response.status != 200: |
| 55 | + raise ConfigEntryNotReady( |
| 56 | + f"Error fetching add-on config: {response.status}" |
| 57 | + ) |
| 58 | + data = await response.json() |
| 59 | + return data["data"]["options"] |
52 | 60 | except Exception as e: |
53 | 61 | raise ConfigEntryNotReady(f"Error fetching add-on configuration: {e}") |
54 | 62 |
|
@@ -139,19 +147,27 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
139 | 147 | return True |
140 | 148 |
|
141 | 149 |
|
142 | | -async def ensure_addon_started(hass: HomeAssistant, addon_slug: str) -> None: |
| 150 | +async def ensure_addon_started( |
| 151 | + hass: HomeAssistant, addon_slug: str, retries=10, delay=10 |
| 152 | +) -> None: |
143 | 153 | """Ensure the add-on is started.""" |
144 | | - addon_info = await async_get_addon_info(hass, addon_slug) |
145 | | - if addon_info["state"] != "started": |
| 154 | + for attempt in range(retries): |
| 155 | + addon_info = await async_get_addon_info(hass, addon_slug) |
| 156 | + if addon_info["state"] == "started": |
| 157 | + _LOGGER.debug("Add-on %s is already started", addon_slug) |
| 158 | + return |
146 | 159 | _LOGGER.debug("Starting add-on %s", addon_slug) |
147 | | - await async_start_addon(hass, addon_slug) |
148 | | - for _ in range(10): # Retry up to 10 times |
149 | | - await asyncio.sleep(10) |
150 | | - addon_info = await async_get_addon_info(hass, addon_slug) |
151 | | - if addon_info["state"] == "started": |
152 | | - _LOGGER.debug("Add-on %s started", addon_slug) |
153 | | - return |
154 | | - raise ConfigEntryNotReady(f"Add-on {addon_slug} not started") |
| 160 | + try: |
| 161 | + await async_start_addon(hass, addon_slug) |
| 162 | + # Wait a bit before checking the status again |
| 163 | + await asyncio.sleep(delay) |
| 164 | + except Exception as e: |
| 165 | + _LOGGER.error("Error starting add-on %s: %s", addon_slug, e) |
| 166 | + if attempt < retries - 1: |
| 167 | + _LOGGER.debug("Retrying to start add-on %s", addon_slug) |
| 168 | + await asyncio.sleep(delay) |
| 169 | + else: |
| 170 | + raise ConfigEntryNotReady(f"Error starting add-on {addon_slug}: {e}") |
155 | 171 |
|
156 | 172 |
|
157 | 173 | async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
|
0 commit comments