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

@@ -1,4 +1,4 @@
"""Config flow for MedMate integration."""
"""Config flow for MedMate integration - Bulletproof version."""
from __future__ import annotations
import logging
@@ -11,7 +11,6 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.storage import Store
from homeassistant.helpers import selector
from .const import (
DOMAIN,
@@ -82,13 +81,19 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
if medicine_name in medicines and self._editing_medicine_id != medicine_name:
errors[CONF_MEDICINE_NAME] = "name_exists"
else:
self._medicine_data = user_input
# Store ONLY the expected keys - CRITICAL FILTERING
self._medicine_data = {
CONF_MEDICINE_NAME: user_input[CONF_MEDICINE_NAME],
CONF_ACTIVE_INGREDIENT: user_input[CONF_ACTIVE_INGREDIENT],
CONF_STRENGTH: user_input.get(CONF_STRENGTH, ""),
CONF_PACK_SIZE: user_input.get(CONF_PACK_SIZE, DEFAULT_PACK_SIZE),
}
return await self.async_step_schedule()
schema = vol.Schema({
vol.Required(CONF_MEDICINE_NAME): str,
vol.Required(CONF_ACTIVE_INGREDIENT): str,
vol.Optional(CONF_STRENGTH, default=""): str,
vol.Required(CONF_MEDICINE_NAME): cv.string,
vol.Required(CONF_ACTIVE_INGREDIENT): cv.string,
vol.Optional(CONF_STRENGTH, default=""): cv.string,
vol.Optional(CONF_PACK_SIZE, default=DEFAULT_PACK_SIZE): cv.positive_int,
})
@@ -103,39 +108,45 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) -> FlowResult:
"""Handle schedule configuration."""
if user_input is not None:
# Process the checkbox inputs
# CRITICAL: Extract only the actual values, ignore ALL form keys
selected_days = []
selected_times = []
# Process days
# Process days - extract values from checkbox form keys
for day in DAYS_OF_WEEK:
if user_input.get(f"day_{day}", False):
form_key = f"day_{day}"
if user_input.get(form_key, False):
selected_days.append(day)
# Process times
# Process times - extract values from checkbox form keys
for time_key in TIME_SLOTS.keys():
if user_input.get(f"time_{time_key}", False):
form_key = f"time_{time_key}"
if user_input.get(form_key, False):
selected_times.append(time_key)
# Store ONLY the clean, expected data structure
self._schedule_data = {
CONF_DAYS: selected_days,
CONF_TIMES: selected_times
}
_LOGGER.debug("Processed schedule data: %s", self._schedule_data)
return await self.async_step_prescription()
# Create schema with individual checkboxes
schema_dict = {}
# Create form schema with temporary checkbox keys
# These keys are ONLY for the form, never stored permanently
schema_fields = {}
# Add day checkboxes (default all days selected)
# Add day checkboxes (all selected by default)
for day in DAYS_OF_WEEK:
schema_dict[vol.Optional(f"day_{day}", default=True)] = bool
schema_fields[vol.Optional(f"day_{day}", default=True)] = cv.boolean
# Add time checkboxes (default morning selected)
for time_key, time_label in TIME_SLOTS.items():
default_val = time_key == "morning" # Only morning selected by default
schema_dict[vol.Optional(f"time_{time_key}", default=default_val)] = bool
# Add time checkboxes (only morning selected by default)
for time_key in TIME_SLOTS.keys():
default_val = (time_key == "morning")
schema_fields[vol.Optional(f"time_{time_key}", default=default_val)] = cv.boolean
schema = vol.Schema(schema_dict)
schema = vol.Schema(schema_fields)
return self.async_show_form(
step_id="schedule",
@@ -147,7 +158,14 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) -> FlowResult:
"""Handle prescription information."""
if user_input is not None:
self._prescription_data = user_input
# Store ONLY expected prescription keys
self._prescription_data = {
CONF_ISSUE_DATE: user_input.get(CONF_ISSUE_DATE),
CONF_EXPIRY_DATE: user_input.get(CONF_EXPIRY_DATE),
CONF_DOCTOR: user_input.get(CONF_DOCTOR, ""),
CONF_TOTAL_REPEATS: user_input.get(CONF_TOTAL_REPEATS, DEFAULT_REPEATS),
CONF_REPEATS_LEFT: user_input.get(CONF_REPEATS_LEFT),
}
return await self.async_create_entry_data()
today = date.today()
@@ -156,7 +174,7 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
schema = vol.Schema({
vol.Optional(CONF_ISSUE_DATE, default=today): cv.date,
vol.Optional(CONF_EXPIRY_DATE, default=expiry_date): cv.date,
vol.Optional(CONF_DOCTOR, default=""): str,
vol.Optional(CONF_DOCTOR, default=""): cv.string,
vol.Optional(CONF_TOTAL_REPEATS, default=DEFAULT_REPEATS): cv.positive_int,
vol.Optional(CONF_REPEATS_LEFT): cv.positive_int,
})
@@ -169,18 +187,22 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_create_entry_data(self) -> FlowResult:
"""Create the config entry."""
# Set repeats_left to total_repeats if not specified
if CONF_REPEATS_LEFT not in self._prescription_data:
if not self._prescription_data.get(CONF_REPEATS_LEFT):
self._prescription_data[CONF_REPEATS_LEFT] = self._prescription_data[CONF_TOTAL_REPEATS]
# Build the complete medicine data with ONLY expected keys
medicine_data = {
**self._medicine_data,
CONF_SCHEDULE: self._schedule_data,
CONF_MEDICINE_NAME: self._medicine_data[CONF_MEDICINE_NAME],
CONF_ACTIVE_INGREDIENT: self._medicine_data[CONF_ACTIVE_INGREDIENT],
CONF_STRENGTH: self._medicine_data[CONF_STRENGTH],
CONF_PACK_SIZE: self._medicine_data[CONF_PACK_SIZE],
CONF_SCHEDULE: self._schedule_data, # Contains only CONF_DAYS and CONF_TIMES
CONF_PRESCRIPTION: self._prescription_data,
"inventory": 0,
"last_taken": {},
}
# Store the medicine data
# Store the medicine data in the persistent storage
store = Store(self.hass, STORAGE_VERSION, STORAGE_KEY)
existing_data = await store.async_load() or {}
medicines = existing_data.get("medicines", {})
@@ -190,9 +212,17 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
await store.async_save({"medicines": medicines})
# Create config entry with MINIMAL data - only what Home Assistant needs
# CRITICAL: Only store the medicine_id, nothing else that could cause validation issues
config_entry_data = {
"medicine_id": medicine_id
}
_LOGGER.debug("Creating config entry with data: %s", config_entry_data)
return self.async_create_entry(
title=f"MedMate - {self._medicine_data[CONF_MEDICINE_NAME]}",
data={"medicine_id": medicine_id},
data=config_entry_data, # Only the minimal required data
)
@staticmethod
@@ -234,22 +264,28 @@ class MedMateOptionsFlow(config_entries.OptionsFlow):
current_medicine = medicines.get(medicine_id, {})
if user_input is not None:
self._medicine_data = user_input
# Filter to only expected keys
self._medicine_data = {
CONF_MEDICINE_NAME: user_input[CONF_MEDICINE_NAME],
CONF_ACTIVE_INGREDIENT: user_input[CONF_ACTIVE_INGREDIENT],
CONF_STRENGTH: user_input.get(CONF_STRENGTH, ""),
CONF_PACK_SIZE: user_input.get(CONF_PACK_SIZE, DEFAULT_PACK_SIZE),
}
return await self.async_step_schedule()
schema = vol.Schema({
vol.Required(
CONF_MEDICINE_NAME,
default=current_medicine.get(CONF_MEDICINE_NAME, "")
): str,
): cv.string,
vol.Required(
CONF_ACTIVE_INGREDIENT,
default=current_medicine.get(CONF_ACTIVE_INGREDIENT, "")
): str,
): cv.string,
vol.Optional(
CONF_STRENGTH,
default=current_medicine.get(CONF_STRENGTH, "")
): str,
): cv.string,
vol.Optional(
CONF_PACK_SIZE,
default=current_medicine.get(CONF_PACK_SIZE, DEFAULT_PACK_SIZE)
@@ -277,7 +313,7 @@ class MedMateOptionsFlow(config_entries.OptionsFlow):
current_times = current_schedule.get(CONF_TIMES, ["morning"])
if user_input is not None:
# Process the checkbox inputs
# Extract clean values from form checkboxes
selected_days = []
selected_times = []
@@ -291,26 +327,27 @@ class MedMateOptionsFlow(config_entries.OptionsFlow):
if user_input.get(f"time_{time_key}", False):
selected_times.append(time_key)
# Store only clean data
self._schedule_data = {
CONF_DAYS: selected_days,
CONF_TIMES: selected_times
}
return await self.async_step_prescription()
# Create schema with individual checkboxes
schema_dict = {}
# Create form schema with checkboxes
schema_fields = {}
# Add day checkboxes with current values as defaults
for day in DAYS_OF_WEEK:
default_val = day in current_days
schema_dict[vol.Optional(f"day_{day}", default=default_val)] = bool
schema_fields[vol.Optional(f"day_{day}", default=default_val)] = cv.boolean
# Add time checkboxes with current values as defaults
for time_key, time_label in TIME_SLOTS.items():
for time_key in TIME_SLOTS.keys():
default_val = time_key in current_times
schema_dict[vol.Optional(f"time_{time_key}", default=default_val)] = bool
schema_fields[vol.Optional(f"time_{time_key}", default=default_val)] = cv.boolean
schema = vol.Schema(schema_dict)
schema = vol.Schema(schema_fields)
return self.async_show_form(
step_id="schedule",
@@ -330,7 +367,14 @@ class MedMateOptionsFlow(config_entries.OptionsFlow):
current_prescription = current_medicine.get(CONF_PRESCRIPTION, {})
if user_input is not None:
self._prescription_data = user_input
# Filter to expected prescription keys only
self._prescription_data = {
CONF_ISSUE_DATE: user_input.get(CONF_ISSUE_DATE),
CONF_EXPIRY_DATE: user_input.get(CONF_EXPIRY_DATE),
CONF_DOCTOR: user_input.get(CONF_DOCTOR, ""),
CONF_TOTAL_REPEATS: user_input.get(CONF_TOTAL_REPEATS, DEFAULT_REPEATS),
CONF_REPEATS_LEFT: user_input.get(CONF_REPEATS_LEFT, DEFAULT_REPEATS),
}
return await self.async_update_entry_data()
today = date.today()
@@ -347,7 +391,7 @@ class MedMateOptionsFlow(config_entries.OptionsFlow):
vol.Optional(
CONF_DOCTOR,
default=current_prescription.get(CONF_DOCTOR, "")
): str,
): cv.string,
vol.Optional(
CONF_TOTAL_REPEATS,
default=current_prescription.get(CONF_TOTAL_REPEATS, DEFAULT_REPEATS)
@@ -373,10 +417,13 @@ class MedMateOptionsFlow(config_entries.OptionsFlow):
medicine_id = self.config_entry.data.get("medicine_id")
current_medicine = medicines.get(medicine_id, {})
# Update medicine data
# Update with only expected keys
updated_medicine = {
**current_medicine,
**self._medicine_data,
CONF_MEDICINE_NAME: self._medicine_data[CONF_MEDICINE_NAME],
CONF_ACTIVE_INGREDIENT: self._medicine_data[CONF_ACTIVE_INGREDIENT],
CONF_STRENGTH: self._medicine_data[CONF_STRENGTH],
CONF_PACK_SIZE: self._medicine_data[CONF_PACK_SIZE],
CONF_SCHEDULE: self._schedule_data,
CONF_PRESCRIPTION: self._prescription_data,
}