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

@@ -14,12 +14,19 @@ from .const import (
DOMAIN,
STORAGE_KEY,
STORAGE_VERSION,
CONF_MEDICINE_NAME,
CONF_ACTIVE_INGREDIENT,
CONF_STRENGTH,
CONF_PACK_SIZE,
CONF_SCHEDULE,
CONF_PRESCRIPTION,
CONF_DAYS,
CONF_TIMES,
CONF_EXPIRY_DATE,
CONF_REPEATS_LEFT,
CONF_ISSUE_DATE,
CONF_DOCTOR,
CONF_TOTAL_REPEATS,
TIME_SLOTS,
)
@@ -62,10 +69,59 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
medicine_data = medicines[self.medicine_id]
# Calculate derived values
medicine_data = self._calculate_derived_data(medicine_data)
# Clean the data to ensure no unwanted keys are present
cleaned_data = self._clean_medicine_data(medicine_data)
return medicine_data
# Calculate derived values
cleaned_data = self._calculate_derived_data(cleaned_data)
return cleaned_data
def _clean_medicine_data(self, medicine_data: dict[str, Any]) -> dict[str, Any]:
"""Clean medicine data to remove any unwanted keys from config flow."""
if not medicine_data:
return {}
# Define the expected structure
cleaned = {}
# Basic medicine info
for key in [CONF_MEDICINE_NAME, CONF_ACTIVE_INGREDIENT, CONF_STRENGTH, CONF_PACK_SIZE]:
if key in medicine_data:
cleaned[key] = medicine_data[key]
# Clean schedule data
schedule = medicine_data.get(CONF_SCHEDULE, {})
if isinstance(schedule, dict):
cleaned_schedule = {}
# Only include expected schedule keys
if CONF_DAYS in schedule and isinstance(schedule[CONF_DAYS], list):
cleaned_schedule[CONF_DAYS] = schedule[CONF_DAYS]
if CONF_TIMES in schedule and isinstance(schedule[CONF_TIMES], list):
cleaned_schedule[CONF_TIMES] = schedule[CONF_TIMES]
cleaned[CONF_SCHEDULE] = cleaned_schedule
# Clean prescription data
prescription = medicine_data.get(CONF_PRESCRIPTION, {})
if isinstance(prescription, dict):
cleaned_prescription = {}
# Only include expected prescription keys
for key in [CONF_ISSUE_DATE, CONF_EXPIRY_DATE, CONF_DOCTOR, CONF_TOTAL_REPEATS, CONF_REPEATS_LEFT]:
if key in prescription:
cleaned_prescription[key] = prescription[key]
cleaned[CONF_PRESCRIPTION] = cleaned_prescription
# Include other expected keys
for key in ["inventory", "last_taken"]:
if key in medicine_data:
cleaned[key] = medicine_data[key]
_LOGGER.debug("Cleaned medicine data: %s", cleaned)
return cleaned
def _calculate_derived_data(self, medicine_data: dict[str, Any]) -> dict[str, Any]:
"""Calculate derived data for the medicine."""
@@ -100,7 +156,7 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
days = schedule.get(CONF_DAYS, [])
times = schedule.get(CONF_TIMES, [])
if not days or not times:
if not days or not times or not isinstance(days, list) or not isinstance(times, list):
return None
# Get current day of week (0=Monday, 6=Sunday)
@@ -114,7 +170,10 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
"friday": 4, "saturday": 5, "sunday": 6
}
scheduled_weekdays = [weekday_map[day] for day in days if day in weekday_map]
scheduled_weekdays = []
for day in days:
if isinstance(day, str) and day in weekday_map:
scheduled_weekdays.append(weekday_map[day])
if not scheduled_weekdays:
return None
@@ -122,7 +181,7 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
# Get dose times for today and future
dose_times = []
for time_slot in times:
if time_slot in DEFAULT_TIMES:
if isinstance(time_slot, str) and time_slot in DEFAULT_TIMES:
dose_times.append(DEFAULT_TIMES[time_slot])
if not dose_times:
@@ -156,7 +215,7 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
times = schedule.get(CONF_TIMES, [])
last_taken = medicine_data.get("last_taken", {})
if not days or not times:
if not days or not times or not isinstance(days, list) or not isinstance(times, list):
return False
current_weekday = now.strftime("%A").lower()
@@ -168,7 +227,7 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
# Check each scheduled time slot
for time_slot in times:
if time_slot not in DEFAULT_TIMES:
if not isinstance(time_slot, str) or time_slot not in DEFAULT_TIMES:
continue
slot_time = DEFAULT_TIMES[time_slot]
@@ -243,7 +302,7 @@ class MedMateDataUpdateCoordinator(DataUpdateCoordinator):
prescription[CONF_REPEATS_LEFT] = repeats_left - 1
# Add to inventory
pack_size = medicine_data.get("pack_size", 30)
pack_size = medicine_data.get(CONF_PACK_SIZE, 30)
current_inventory = medicine_data.get("inventory", 0)
medicine_data["inventory"] = current_inventory + pack_size