From 3d89c4225ee0ebf7c07618589d5052e19a3a83ab Mon Sep 17 00:00:00 2001 From: dematheson Date: Sun, 17 Aug 2025 20:26:04 +1000 Subject: [PATCH] Fix config flow issue --- __init__.py | 38 +++++++++++++++++++++++--------------- config_flow.py | 30 ++++++++++++++++++++++++------ const.py | 2 -- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/__init__.py b/__init__.py index c14628d..d951d07 100644 --- a/__init__.py +++ b/__init__.py @@ -8,7 +8,7 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType -from .const import DOMAIN, PLATFORMS +from .const import DOMAIN from .coordinator import MedMateDataUpdateCoordinator _LOGGER = logging.getLogger(__name__) @@ -23,24 +23,32 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up MedMate from a config entry.""" - coordinator = MedMateDataUpdateCoordinator(hass, entry) - - await coordinator.async_config_entry_first_refresh() - - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][entry.entry_id] = coordinator - - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - - return True + try: + coordinator = MedMateDataUpdateCoordinator(hass, entry) + + await coordinator.async_config_entry_first_refresh() + + hass.data.setdefault(DOMAIN, {}) + hass.data[DOMAIN][entry.entry_id] = coordinator + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True + except Exception as err: + _LOGGER.error("Error setting up MedMate integration: %s", err) + return False async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + try: + if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): + hass.data[DOMAIN].pop(entry.entry_id) + + return unload_ok + except Exception as err: + _LOGGER.error("Error unloading MedMate integration: %s", err) + return False async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: diff --git a/config_flow.py b/config_flow.py index dfe4397..6ec35e2 100644 --- a/config_flow.py +++ b/config_flow.py @@ -108,11 +108,19 @@ class MedMateConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): } return await self.async_step_prescription() + # Create day options dict + day_options = {} + for day in DAYS_OF_WEEK: + day_options[day] = day.title() + + # Create time options dict + time_options = {} + for key, value in TIME_SLOTS.items(): + time_options[key] = value + schema = vol.Schema({ - vol.Required(CONF_DAYS, default=DAYS_OF_WEEK): cv.multi_select( - {day: day.title() for day in DAYS_OF_WEEK} - ), - vol.Required(CONF_TIMES, default=["morning"]): cv.multi_select(TIME_SLOTS), + vol.Required(CONF_DAYS, default=DAYS_OF_WEEK): cv.multi_select(day_options), + vol.Required(CONF_TIMES, default=["morning"]): cv.multi_select(time_options), }) return self.async_show_form( @@ -259,15 +267,25 @@ class MedMateOptionsFlow(config_entries.OptionsFlow): } return await self.async_step_prescription() + # Create day options dict + day_options = {} + for day in DAYS_OF_WEEK: + day_options[day] = day.title() + + # Create time options dict + time_options = {} + for key, value in TIME_SLOTS.items(): + time_options[key] = value + schema = vol.Schema({ vol.Required( CONF_DAYS, default=current_schedule.get(CONF_DAYS, DAYS_OF_WEEK) - ): cv.multi_select({day: day.title() for day in DAYS_OF_WEEK}), + ): cv.multi_select(day_options), vol.Required( CONF_TIMES, default=current_schedule.get(CONF_TIMES, ["morning"]) - ): cv.multi_select(TIME_SLOTS), + ): cv.multi_select(time_options), }) return self.async_show_form( diff --git a/const.py b/const.py index 18e100b..8de389d 100644 --- a/const.py +++ b/const.py @@ -1,8 +1,6 @@ """Constants for the MedMate integration.""" -from homeassistant.const import Platform DOMAIN = "medmate" -PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.BUTTON] # Configuration keys CONF_MEDICINES = "medicines"