Skip to content

Commit ad18110

Browse files
author
lexorius
committed
working v2.5
1 parent 66c52e4 commit ad18110

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

custom_components/alternative_time/sensor.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,27 @@ async def async_setup_entry(
4949
_LOGGER.warning("No calendars selected")
5050
return
5151

52+
_LOGGER.info(f"Setting up sensors for calendars: {selected_calendars}")
53+
54+
# Clear cache to force re-discovery
55+
global _DISCOVERED_CALENDARS_CACHE
56+
_DISCOVERED_CALENDARS_CACHE = None
57+
5258
# Discover all available calendars
5359
discovered_calendars = await async_discover_all_calendars(hass)
5460

61+
if not discovered_calendars:
62+
_LOGGER.error("No calendars could be discovered!")
63+
return
64+
65+
_LOGGER.info(f"Discovered calendars: {list(discovered_calendars.keys())}")
66+
5567
# Create sensors for selected calendars
5668
sensors = []
5769
for calendar_id in selected_calendars:
5870
if calendar_id not in discovered_calendars:
59-
_LOGGER.warning(f"Calendar {calendar_id} not found")
71+
_LOGGER.error(f"Calendar '{calendar_id}' is enabled but not found in registry")
72+
_LOGGER.debug(f"Available calendars: {list(discovered_calendars.keys())}")
6073
continue
6174

6275
calendar_info = discovered_calendars[calendar_id]
@@ -93,11 +106,12 @@ async def async_setup_entry(
93106
except Exception as e:
94107
_LOGGER.error(f"Failed to create sensor for calendar {calendar_id}: {e}")
95108
import traceback
96-
traceback.print_exc()
109+
_LOGGER.debug(traceback.format_exc())
97110
continue
98111

99112
if sensors:
100113
async_add_entities(sensors)
114+
_LOGGER.info(f"Added {len(sensors)} sensors to Home Assistant")
101115

102116

103117
async def async_discover_all_calendars(hass: HomeAssistant) -> Dict[str, Dict[str, Any]]:
@@ -165,19 +179,40 @@ async def async_import_calendar_module(hass: HomeAssistant, module_name: str):
165179
"""Import a calendar module asynchronously."""
166180
def _import():
167181
try:
182+
# Add parent directory to path for imports
183+
import sys
184+
current_dir = os.path.dirname(os.path.abspath(__file__))
185+
parent_dir = os.path.dirname(current_dir)
186+
if parent_dir not in sys.path:
187+
sys.path.insert(0, parent_dir)
188+
168189
# Try different import methods
169190
try:
170-
return import_module(f'.calendars.{module_name}',
191+
module = import_module(f'.calendars.{module_name}',
171192
package='custom_components.alternative_time')
172-
except ImportError:
193+
_LOGGER.debug(f"Successfully imported {module_name} via method 1")
194+
return module
195+
except ImportError as e1:
196+
_LOGGER.debug(f"Method 1 failed for {module_name}: {e1}")
173197
try:
174-
return import_module(
198+
module = import_module(
175199
f'custom_components.alternative_time.calendars.{module_name}'
176200
)
177-
except ImportError:
178-
return import_module(module_name)
201+
_LOGGER.debug(f"Successfully imported {module_name} via method 2")
202+
return module
203+
except ImportError as e2:
204+
_LOGGER.debug(f"Method 2 failed for {module_name}: {e2}")
205+
try:
206+
module = import_module(module_name)
207+
_LOGGER.debug(f"Successfully imported {module_name} via method 3")
208+
return module
209+
except ImportError as e3:
210+
_LOGGER.debug(f"Method 3 failed for {module_name}: {e3}")
211+
raise e3
179212
except Exception as e:
180213
_LOGGER.error(f"Failed to import calendar module {module_name}: {e}")
214+
import traceback
215+
_LOGGER.debug(traceback.format_exc())
181216
return None
182217

183218
return await hass.async_add_executor_job(_import)

0 commit comments

Comments
 (0)