Skip to content

Commit 09d58de

Browse files
committed
Sync changes from hacore2/dwd_global_rad_api_server
1 parent de9324a commit 09d58de

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

custom_components/dwd_global_rad/__init__.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import asyncio
77
import logging
8+
import os
89

910
import aiohttp
1011
import dwd_global_radiation as dgr
@@ -14,6 +15,7 @@
1415
from homeassistant.const import Platform
1516
from homeassistant.core import HomeAssistant
1617
from homeassistant.exceptions import ConfigEntryNotReady
18+
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1719

1820
from .api_client import DWDGlobalRadAPIClient
1921
from .const import DOMAIN
@@ -33,22 +35,28 @@
3335

3436
async def get_addon_config(hass: HomeAssistant, addon_slug: str):
3537
"""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-
4038
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"]
5260
except Exception as e:
5361
raise ConfigEntryNotReady(f"Error fetching add-on configuration: {e}")
5462

@@ -139,19 +147,27 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
139147
return True
140148

141149

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:
143153
"""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
146159
_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}")
155171

156172

157173
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

custom_components/dwd_global_rad/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"documentation": "https://github.com/aschmere/dwd_global_rad_hass",
88
"homekit": {},
99
"iot_class": "cloud_polling",
10-
"requirements": ["dwd_global_radiation==1.1.0rc6"],
10+
"requirements": [],
1111
"ssdp": [],
12-
"version": "0.2.5-beta",
12+
"version": "0.2.6-beta",
1313
"zeroconf": []
1414
}

0 commit comments

Comments
 (0)