66"""
77import asyncio
88import logging
9- import traceback
109from datetime import timedelta
1110
1211from 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
10196async 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
119119async 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 )
0 commit comments