11import logging
2+ import traceback
23
34import aiohttp
45
@@ -15,14 +16,18 @@ def __init__(self, hass: HomeAssistant, hostname: str, port_number: int):
1516 self .session = aiohttp .ClientSession ()
1617 self ._locations = None
1718 self .base_url = f"http://{ self .hostname } :{ self .port_number } "
19+ self .debug_mode = _LOGGER .isEnabledFor (logging .DEBUG )
20+ self .tracing_enabled = False # Separate flag for tracing
1821
1922 @property
2023 async def locations (self ):
24+ self ._log_debug_info ("Fetching locations property" )
2125 if self ._locations is None :
2226 await self .fetch_locations ()
2327 return self ._locations
2428
2529 async def fetch_locations (self ):
30+ self ._log_debug_info ("Fetching locations" )
2631 url = f"{ self .base_url } /locations"
2732 async with self .session .get (url ) as response :
2833 if response .status == 200 :
@@ -32,6 +37,7 @@ async def fetch_locations(self):
3237 return self ._locations
3338
3439 async def get_location_by_name (self , name : str ):
40+ self ._log_debug_info (f"Fetching location by name: { name } " )
3541 url = f"{ self .base_url } /locations/{ name } "
3642 async with self .session .get (url ) as response :
3743 data = await response .json ()
@@ -40,27 +46,32 @@ async def get_location_by_name(self, name: str):
4046 return data
4147
4248 async def add_location (self , name : str , latitude : float , longitude : float ):
49+ self ._log_debug_info (f"Adding location: { name } " )
4350 url = f"{ self .base_url } /locations"
4451 data = {"name" : name , "latitude" : latitude , "longitude" : longitude }
4552 async with self .session .post (url , json = data ) as response :
4653 return await response .json ()
4754
4855 async def fetch_forecasts (self ):
56+ self ._log_debug_info ("Fetching forecasts" )
4957 url = f"{ self .base_url } /forecasts"
5058 async with self .session .get (url ) as response :
5159 return await response .json ()
5260
5361 async def fetch_measurements (self , hours : int = 3 ):
62+ self ._log_debug_info (f"Fetching measurements for { hours } hours" )
5463 url = f"{ self .base_url } /measurements?hours={ hours } "
5564 async with self .session .get (url ) as response :
5665 return await response .json ()
5766
5867 async def remove_location (self , name : str ):
68+ self ._log_debug_info (f"Removing location: { name } " )
5969 url = f"{ self .base_url } /locations/{ name } "
6070 async with self .session .delete (url ) as response :
6171 return await response .json ()
6272
6373 async def get_status (self ):
74+ self ._log_debug_info ("Checking API server status" )
6475 url = f"{ self .base_url } /status"
6576 async with self .session .get (url ) as response :
6677 if response .status == 204 :
@@ -69,4 +80,13 @@ async def get_status(self):
6980 return False
7081
7182 async def close (self ):
83+ self ._log_debug_info ("Closing session" )
7284 await self .session .close ()
85+
86+ def _log_debug_info (self , message : str ):
87+ _LOGGER .debug (message )
88+ if self .debug_mode and self .tracing_enabled :
89+ _LOGGER .debug ("Call stack:" )
90+ stack = traceback .format_stack ()
91+ for line in stack :
92+ _LOGGER .debug (line .strip ())
0 commit comments