Fix set up issue AGAIN

This commit is contained in:
2025-08-17 23:02:10 +10:00
parent 0621606782
commit a7259bca08
7 changed files with 270 additions and 87 deletions

View File

@@ -30,19 +30,32 @@ async def async_setup_entry(
"""Set up MedMate buttons."""
coordinator = hass.data[DOMAIN][config_entry.entry_id]
# Wait for first update to ensure we have clean data
if not coordinator.data:
await coordinator.async_request_refresh()
buttons = [
MedMateFillPrescriptionButton(coordinator, config_entry),
]
# Add take dose buttons for each scheduled time
schedule = coordinator.data.get(CONF_SCHEDULE, {})
scheduled_times = schedule.get(CONF_TIMES, [])
for time_slot in scheduled_times:
if time_slot in TIME_SLOTS:
buttons.append(
MedMateTakeDoseButton(coordinator, config_entry, time_slot)
)
# Safely get scheduled times with error handling
try:
schedule = coordinator.data.get(CONF_SCHEDULE, {})
scheduled_times = schedule.get(CONF_TIMES, []) if isinstance(schedule, dict) else []
# Validate that scheduled_times is a list and contains expected values
if isinstance(scheduled_times, list):
for time_slot in scheduled_times:
if isinstance(time_slot, str) and time_slot in TIME_SLOTS:
buttons.append(
MedMateTakeDoseButton(coordinator, config_entry, time_slot)
)
else:
_LOGGER.warning("Invalid scheduled_times format: %s", type(scheduled_times))
except Exception as err:
_LOGGER.error("Error setting up dose buttons: %s", err)
# Continue with just the fill prescription button
async_add_entities(buttons)