Skip to content

Commit 5063378

Browse files
committed
Improve logging messages
1 parent 4426161 commit 5063378

3 files changed

Lines changed: 24 additions & 28 deletions

File tree

custom_components/carbon_intensity_uk/__init__.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77
import asyncio
88
import logging
9-
import traceback
109
from datetime import timedelta
1110

1211
from homeassistant.config_entries import ConfigEntry
@@ -42,25 +41,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
4241
_LOGGER.info(STARTUP_MESSAGE)
4342

4443
postcode = entry.data.get(CONF_POSTCODE)
45-
_LOGGER.error("[carbon_intensity_uk] Setting up entry for postcode: %s", postcode)
44+
_LOGGER.debug("Setting up Carbon Intensity UK for postcode: %s", postcode)
4645

4746
coordinator = CarbonIntensityDataUpdateCoordinator(hass, postcode=postcode)
48-
_LOGGER.error("[carbon_intensity_uk] Triggering initial coordinator refresh")
47+
_LOGGER.debug("Performing initial data fetch for postcode: %s", postcode)
4948
await coordinator.async_refresh()
50-
_LOGGER.error(
51-
"[carbon_intensity_uk] Refresh done. last_update_success=%s data=%s",
52-
coordinator.last_update_success,
53-
coordinator.data,
54-
)
5549

5650
if not coordinator.last_update_success:
57-
_LOGGER.error("[carbon_intensity_uk] Coordinator refresh failed — raising ConfigEntryNotReady")
51+
_LOGGER.warning(
52+
"Initial data fetch failed for postcode %s — will retry on next poll", postcode
53+
)
5854
raise ConfigEntryNotReady
5955

6056
hass.data[DOMAIN][entry.entry_id] = coordinator
6157

6258
platforms = [p for p in PLATFORMS if entry.options.get(p, True)]
63-
_LOGGER.error("[carbon_intensity_uk] Forwarding setup to platforms: %s", platforms)
59+
_LOGGER.debug("Forwarding entry setup to platforms: %s", platforms)
6460
coordinator.platforms.extend(platforms)
6561
await hass.config_entries.async_forward_entry_setups(entry, platforms)
6662

@@ -83,23 +79,24 @@ def __init__(self, hass, postcode):
8379
async def _async_update_data(self):
8480
"""Update data via library."""
8581
try:
86-
_LOGGER.error("[carbon_intensity_uk] Fetching data from API")
82+
_LOGGER.debug("Fetching data from Carbon Intensity API")
8783
data = await self.api.async_get_data()
88-
_LOGGER.error("[carbon_intensity_uk] Raw API response: %s", data)
8984
result = data.get("data", {})
90-
_LOGGER.error("[carbon_intensity_uk] Parsed data keys: %s", list(result.keys()) if isinstance(result, dict) else result)
85+
_LOGGER.debug(
86+
"Data fetch succeeded: index=%s, forecast=%s gCO2/kWh",
87+
result.get("current_period_index"),
88+
result.get("current_period_forecast"),
89+
)
9190
return result
9291
except Exception as exception:
93-
_LOGGER.error(
94-
"[carbon_intensity_uk] Exception fetching data: %s\n%s",
95-
exception,
96-
traceback.format_exc(),
97-
)
92+
_LOGGER.warning("Failed to fetch data from Carbon Intensity API: %s", exception)
9893
raise UpdateFailed(exception)
9994

10095

10196
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
10297
"""Handle removal of an entry."""
98+
postcode = entry.data.get(CONF_POSTCODE)
99+
_LOGGER.debug("Unloading Carbon Intensity UK entry for postcode: %s", postcode)
103100
coordinator = hass.data[DOMAIN][entry.entry_id]
104101
unloaded = all(
105102
await asyncio.gather(
@@ -112,11 +109,15 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
112109
)
113110
if unloaded:
114111
hass.data[DOMAIN].pop(entry.entry_id)
112+
_LOGGER.debug("Successfully unloaded Carbon Intensity UK entry for postcode: %s", postcode)
113+
else:
114+
_LOGGER.warning("Failed to unload one or more platforms for postcode: %s", postcode)
115115

116116
return unloaded
117117

118118

119119
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry):
120120
"""Reload config entry."""
121+
_LOGGER.debug("Reloading Carbon Intensity UK entry for postcode: %s", entry.data.get(CONF_POSTCODE))
121122
await async_unload_entry(hass, entry)
122123
await async_setup_entry(hass, entry)

custom_components/carbon_intensity_uk/config_flow.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ async def async_step_user(
3737
if user_input is not None:
3838
valid = await self._test_credentials(user_input[CONF_POSTCODE])
3939
if valid:
40-
_LOGGER.debug("Input is valid")
40+
_LOGGER.debug("Postcode %s validated, creating config entry", user_input[CONF_POSTCODE])
4141
return self.async_create_entry(
4242
title=user_input[CONF_POSTCODE], data=user_input
4343
)
4444
else:
45-
_LOGGER.debug("Input not valid")
45+
_LOGGER.warning("Postcode %s failed validation — check it is a valid UK postcode area", user_input[CONF_POSTCODE])
4646
self._errors["base"] = "auth"
4747

4848
return await self._show_config_form(user_input)
@@ -67,11 +67,10 @@ async def _test_credentials(self, postcode):
6767
try:
6868
client = CarbonIntentisityApi(postcode)
6969
await client.async_get_data()
70-
_LOGGER.debug("Input successfully")
70+
_LOGGER.debug("API connectivity test for postcode %s succeeded", postcode)
7171
return True
7272
except Exception as exception: # pylint: disable=broad-except
73-
_LOGGER.debug(exception)
74-
_LOGGER.debug("Oops! Input failed!")
73+
_LOGGER.warning("API connectivity test for postcode %s failed: %s", postcode, exception)
7574
return False
7675

7776

custom_components/carbon_intensity_uk/sensor.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,8 @@
9090
async def async_setup_entry(hass, entry, async_add_devices):
9191
"""Setup sensor platform."""
9292
coordinator = hass.data[DOMAIN][entry.entry_id]
93-
_LOGGER.error(
94-
"[carbon_intensity_uk] sensor async_setup_entry — coordinator data keys: %s",
95-
list(coordinator.data.keys()) if coordinator.data else None,
96-
)
9793
sensors = [CarbonIntensitySensor(coordinator, entry, sensor) for sensor in SENSOR_TYPES]
98-
_LOGGER.error("[carbon_intensity_uk] Creating %d sensors: %s", len(sensors), [s.name for s in sensors])
94+
_LOGGER.debug("Registering %d Carbon Intensity UK sensors", len(sensors))
9995
async_add_devices(sensors)
10096

10197

0 commit comments

Comments
 (0)