Skip to content

Commit 41a253b

Browse files
committed
Sync changes from hacore2/dwd_global_rad_api_server
1 parent 2e13093 commit 41a253b

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

custom_components/dwd_global_rad/api_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import traceback
23

34
import 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())

custom_components/dwd_global_rad/coordinator.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,16 @@ async def get_location_data(self):
216216
)
217217
return None
218218

219-
if "measurements" not in measurement_data or "forecasts" not in forecast_data:
220-
_LOGGER.debug(
221-
"Measurement or forecast data lists are empty for %s", self.name
222-
)
219+
# Check if measurements and forecasts exist and are not empty
220+
if (
221+
"measurements" not in measurement_data
222+
or not measurement_data["measurements"]
223+
):
224+
_LOGGER.debug("Measurement data list is empty for %s", self.name)
225+
return None
226+
227+
if "forecasts" not in forecast_data or not forecast_data["forecasts"]:
228+
_LOGGER.debug("Forecast data list is empty for %s", self.name)
223229
return None
224230

225231
self._location_data = {

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.8-beta",
12+
"version": "0.2.9-beta",
1313
"zeroconf": []
1414
}

custom_components/dwd_global_rad/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- homeassistant.util.dt: Provides date and time utility functions for Home Assistant.
1010
"""
1111

12+
import logging
13+
1214
from homeassistant.util.dt import as_local, utc_from_timestamp
1315

1416

@@ -18,3 +20,33 @@ def convert_to_local(timestamp):
1820
utc_dt = utc_from_timestamp(timestamp)
1921
# Return local datetime object
2022
return as_local(utc_dt)
23+
24+
25+
def setup_logger(name, level=logging.DEBUG, log_file=None):
26+
"""Set up logger with the specified name and level."""
27+
logger = logging.getLogger(name)
28+
logger.setLevel(level)
29+
30+
# Create console handler and set level to debug
31+
ch = logging.StreamHandler()
32+
ch.setLevel(level)
33+
34+
# Create formatter
35+
formatter = logging.Formatter(
36+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
37+
)
38+
39+
# Add formatter to ch
40+
ch.setFormatter(formatter)
41+
42+
# Add ch to logger
43+
logger.addHandler(ch)
44+
45+
# Optional: Add file handler
46+
if log_file:
47+
fh = logging.FileHandler(log_file)
48+
fh.setLevel(level)
49+
fh.setFormatter(formatter)
50+
logger.addHandler(fh)
51+
52+
return logger

0 commit comments

Comments
 (0)