|
| 1 | +import logging |
| 2 | + |
| 3 | +from homeassistant.core import HomeAssistant |
| 4 | +from homeassistant.config_entries import ConfigEntry |
| 5 | +from homeassistant.loader import async_get_integration |
| 6 | + |
| 7 | +from .const import DOMAIN, PLATFORMS |
| 8 | +from .controller import BoilerController |
| 9 | + |
| 10 | +_LOGGER = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +# Set up the component |
| 14 | +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 15 | + """Set up Boiler Controller from a config entry.""" |
| 16 | + _LOGGER.info("Setting up Boiler Controller") |
| 17 | + |
| 18 | + integration = await async_get_integration(hass, DOMAIN) |
| 19 | + integration_version = integration.version |
| 20 | + |
| 21 | + # Create the controller |
| 22 | + controller = BoilerController(hass, entry, integration_version) |
| 23 | + |
| 24 | + # Start the controller (now handles missing entities gracefully) |
| 25 | + success = await controller.async_start() |
| 26 | + if not success: |
| 27 | + _LOGGER.error("Failed to start Boiler Controller") |
| 28 | + # Don't raise ConfigEntryNotReady anymore - let it start and wait for entities |
| 29 | + _LOGGER.warning("Boiler Controller will continue running and wait for entities to become available") |
| 30 | + |
| 31 | + # Store the controller |
| 32 | + hass.data.setdefault(DOMAIN, {})[entry.entry_id] = { |
| 33 | + "controller": controller, |
| 34 | + } |
| 35 | + |
| 36 | + # Set up platforms |
| 37 | + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) |
| 38 | + |
| 39 | + _LOGGER.info("Boiler Controller setup completed") |
| 40 | + return True |
| 41 | + |
| 42 | +# Implement unloading and reloading of the config entry |
| 43 | +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
| 44 | + """Unload a config entry.""" |
| 45 | + _LOGGER.info("Unloading Boiler Controller") |
| 46 | + |
| 47 | + # Unload platforms |
| 48 | + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
| 49 | + |
| 50 | + # Stop the controller |
| 51 | + controller_data = hass.data.get(DOMAIN, {}).get(entry.entry_id) |
| 52 | + if controller_data: |
| 53 | + controller = controller_data.get("controller") |
| 54 | + if controller: |
| 55 | + await controller.async_stop() |
| 56 | + |
| 57 | + # Remove from hass.data |
| 58 | + if DOMAIN in hass.data and entry.entry_id in hass.data[DOMAIN]: |
| 59 | + hass.data[DOMAIN].pop(entry.entry_id) |
| 60 | + |
| 61 | + return unload_ok |
| 62 | + |
| 63 | + |
| 64 | +async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: |
| 65 | + """Reload config entry.""" |
| 66 | + await async_unload_entry(hass, entry) |
| 67 | + await async_setup_entry(hass, entry) |
0 commit comments