Skip to content

Commit 2a857ab

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

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

custom_components/dwd_global_rad/__init__.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1919

2020
from .api_client import DWDGlobalRadAPIClient
21-
from .const import DOMAIN
21+
from .const import ADDON_SLUG, DOMAIN
2222
from .coordinator import (
2323
ForecastUpdateCoordinator,
2424
LocationDataUpdateCoordinator,
@@ -50,14 +50,20 @@ async def get_addon_config(hass: HomeAssistant, addon_slug: str):
5050
}
5151

5252
session = async_get_clientsession(hass)
53+
_LOGGER.debug(f"Requesting add-on info from URL: {url} with headers: {headers}")
5354
async with session.get(url, headers=headers) as response:
55+
response_text = await response.text()
56+
_LOGGER.debug(
57+
f"Received response status: {response.status}, body: {response_text}"
58+
)
5459
if response.status != 200:
5560
raise ConfigEntryNotReady(
56-
f"Error fetching add-on config: {response.status}"
61+
f"Error fetching add-on config: {response.status} - {response_text}"
5762
)
5863
data = await response.json()
59-
return data["data"]["options"]
64+
return data["data"]
6065
except Exception as e:
66+
_LOGGER.error(f"Error fetching add-on configuration: {e}")
6167
raise ConfigEntryNotReady(f"Error fetching add-on configuration: {e}")
6268

6369

@@ -73,17 +79,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
7379
# entry.async_on_unload(entry.add_update_listener(update_listener))
7480

7581
use_addon = True
76-
addon_slug = "dwd_global_rad_api_server"
7782

7883
if use_addon:
79-
options = await get_addon_config(hass, addon_slug)
80-
if not options:
84+
addon_info = await get_addon_config(hass, ADDON_SLUG)
85+
if not addon_info:
8186
raise ConfigEntryNotReady("No configuration found for the add-on")
8287

83-
hostname = options.get("hostname")
84-
port_number = options.get("port_number")
88+
hostname = addon_info.get("hostname")
89+
port_number = next(iter(addon_info.get("network", {}).values()), 5001)
8590

86-
await ensure_addon_started(hass, addon_slug)
91+
# Ensure the add-on is started
92+
await ensure_addon_started(hass, ADDON_SLUG)
8793
else:
8894
hostname = "homeassistant.local"
8995
port_number = "5001"
@@ -102,6 +108,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
102108
longitude = entry.data["longitude"]
103109
name = entry.data["name"]
104110

111+
if not await wait_for_api_server(api_client):
112+
raise ConfigEntryNotReady("API server not available after multiple attempts")
113+
105114
location = await api_client.get_location_by_name(name)
106115
if location is None:
107116
await api_client.add_location(name=name, latitude=latitude, longitude=longitude)
@@ -162,7 +171,7 @@ async def ensure_addon_started(
162171
# Wait a bit before checking the status again
163172
await asyncio.sleep(delay)
164173
except Exception as e:
165-
_LOGGER.error("Error starting add-on %s: %s", addon_slug, e)
174+
_LOGGER.error(f"Error starting add-on {addon_slug}: {e}")
166175
if attempt < retries - 1:
167176
_LOGGER.debug("Retrying to start add-on %s", addon_slug)
168177
await asyncio.sleep(delay)
@@ -220,3 +229,19 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
220229
# If hass.data[DOMAIN] is now empty, remove it completely
221230
if not hass.data[DOMAIN]:
222231
hass.data.pop(DOMAIN)
232+
233+
234+
async def wait_for_api_server(api_client, retries=5, delay=10):
235+
"""Wait for the API server to be available."""
236+
for attempt in range(retries):
237+
try:
238+
await (
239+
api_client.get_status()
240+
) # Assuming get_status is a method to check the API server status
241+
return True
242+
except Exception as e:
243+
_LOGGER.warning(
244+
f"API server not available, retrying in {delay} seconds... (Attempt {attempt+1}/{retries})"
245+
)
246+
await asyncio.sleep(delay)
247+
return False

custom_components/dwd_global_rad/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
DOMAIN = "dwd_global_rad"
77
MANUFACTURER = "DWD Global Radiation"
88
DEFAULT_NAME = "DWD Global Radiation"
9+
ADDON_SLUG = "cfd37756_dwd_global_rad_api_server"

custom_components/dwd_global_rad/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"iot_class": "cloud_polling",
1010
"requirements": [],
1111
"ssdp": [],
12-
"version": "0.2.6-beta",
12+
"version": "0.2.7-beta",
1313
"zeroconf": []
1414
}

0 commit comments

Comments
 (0)