77import logging
88import os
99
10- from homeassistant .components .hassio import async_get_addon_info , async_start_addon
1110from homeassistant .config_entries import ConfigEntry
1211from homeassistant .const import CONF_NAME , Platform
1312from homeassistant .core import HomeAssistant
3130_LOGGER = logging .getLogger (__name__ )
3231
3332
33+ async def get_addon_info (hass : HomeAssistant , addon_slug : str ):
34+ """Fetch add-on information from the Supervisor API."""
35+ supervisor_token = os .getenv ("SUPERVISOR_TOKEN" )
36+ if not supervisor_token :
37+ _LOGGER .error ("Supervisor token not found in environment variables" )
38+ raise ConfigEntryNotReady ("Supervisor token not found in environment variables" )
39+
40+ supervisor_host = os .getenv ("SUPERVISOR" )
41+ url = f"http://{ supervisor_host } /addons/{ addon_slug } /info"
42+ headers = {
43+ "Authorization" : f"Bearer { supervisor_token } " ,
44+ "Content-Type" : "application/json" ,
45+ }
46+
47+ session = async_get_clientsession (hass )
48+ async with asyncio .timeout (
49+ 10
50+ ): # Replacing async_timeout.timeout with asyncio.timeout
51+ async with session .get (url , headers = headers ) as response :
52+ response_text = await response .text ()
53+ if response .status != 200 :
54+ raise ConfigEntryNotReady (
55+ f"Error fetching add-on info: { response .status } - { response_text } "
56+ )
57+ data = await response .json ()
58+ return data ["data" ]
59+
60+
3461async def get_addon_config (hass : HomeAssistant , addon_slug : str ):
3562 """Fetch add-on configuration from Supervisor."""
3663 try :
@@ -41,7 +68,8 @@ async def get_addon_config(hass: HomeAssistant, addon_slug: str):
4168 "Supervisor token not found in environment variables"
4269 )
4370
44- url = f"http://supervisor/addons/{ addon_slug } /info"
71+ supervisor_host = os .getenv ("SUPERVISOR" )
72+ url = f"http://{ supervisor_host } /addons/{ addon_slug } /info"
4573 headers = {
4674 "Authorization" : f"Bearer { supervisor_token } " ,
4775 "Content-Type" : "application/json" ,
@@ -195,19 +223,50 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
195223 return True
196224
197225
226+ async def start_addon (hass : HomeAssistant , addon_slug : str ):
227+ """Start the add-on using the Supervisor API."""
228+ supervisor_token = os .getenv ("SUPERVISOR_TOKEN" )
229+ if not supervisor_token :
230+ _LOGGER .error ("Supervisor token not found in environment variables" )
231+ raise ConfigEntryNotReady ("Supervisor token not found in environment variables" )
232+
233+ supervisor_host = os .getenv ("SUPERVISOR" )
234+ url = f"http://{ supervisor_host } /addons/{ addon_slug } /start"
235+ headers = {
236+ "Authorization" : f"Bearer { supervisor_token } " ,
237+ "Content-Type" : "application/json" ,
238+ }
239+
240+ session = async_get_clientsession (hass )
241+ try :
242+ async with asyncio .timeout (
243+ 10
244+ ): # Use asyncio.timeout instead of async_timeout.timeout
245+ async with session .post (url , headers = headers ) as response :
246+ response_text = await response .text ()
247+ if response .status != 200 :
248+ raise ConfigEntryNotReady (
249+ f"Error starting add-on: { response .status } - { response_text } "
250+ )
251+ return await response .json ()
252+ except TimeoutError :
253+ _LOGGER .error (f"Timeout occurred when starting add-on { addon_slug } " )
254+ raise ConfigEntryNotReady (f"Timeout occurred when starting add-on { addon_slug } " )
255+
256+
257+ # Updated function to ensure the add-on is started
198258async def ensure_addon_started (
199259 hass : HomeAssistant , addon_slug : str , retries = 10 , delay = 10
200260) -> None :
201- """Ensure the add-on is started."""
261+ """Ensure the add-on is started using the Supervisor API ."""
202262 for attempt in range (retries ):
203- addon_info = await async_get_addon_info (hass , addon_slug )
263+ addon_info = await get_addon_info (hass , addon_slug )
204264 if addon_info ["state" ] == "started" :
205265 _LOGGER .debug ("Add-on %s is already started" , addon_slug )
206266 return
207267 _LOGGER .debug ("Starting add-on %s" , addon_slug )
208268 try :
209- await async_start_addon (hass , addon_slug )
210- # Wait a bit before checking the status again
269+ await start_addon (hass , addon_slug )
211270 await asyncio .sleep (delay )
212271 except Exception as e :
213272 _LOGGER .error (f"Error starting add-on { addon_slug } : { e } " )
0 commit comments