Fix set up issue AGAIN
This commit is contained in:
31
button.py
31
button.py
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user