"""MedMate - Medicine Tracker Integration for Home Assistant.""" import asyncio import logging from datetime import datetime, timedelta import voluptuous as vol from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType import homeassistant.helpers.config_validation as cv from .const import DOMAIN _LOGGER = logging.getLogger(__name__) PLATFORMS = [Platform.SENSOR, Platform.BINARY_SENSOR, Platform.BUTTON] # Configuration schema - this tells HA what config keys are allowed CONFIG_SCHEMA = vol.Schema({ DOMAIN: vol.Schema({}) }, extra=vol.ALLOW_EXTRA) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up MedMate integration.""" # Initialize the domain data storage hass.data.setdefault(DOMAIN, {}) return True async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up MedMate from a config entry.""" try: # Import coordinator here to avoid circular imports from .coordinator import MedMateDataUpdateCoordinator 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) # Set up update listener for options changes entry.async_on_unload(entry.add_update_listener(async_update_options)) 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.""" try: unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: hass.data[DOMAIN].pop(entry.entry_id, None) 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: """Reload config entry.""" await async_unload_entry(hass, entry) await async_setup_entry(hass, entry) async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None: """Handle options update.""" await hass.config_entries.async_reload(config_entry.entry_id)