|
19 | 19 | from .coordinator import KaleidescapeSensorCoordinator |
20 | 20 |
|
21 | 21 | type KaleidescapeConfigEntry = ConfigEntry |
| 22 | +type KaleidescapePlatform = str |
22 | 23 |
|
23 | 24 | _LOGGER = logging.getLogger(__name__) |
| 25 | +DATA_LOADED_PLATFORMS = "loaded_platforms" |
24 | 26 |
|
25 | 27 |
|
26 | 28 | async def async_setup_entry(hass: HomeAssistant, entry: KaleidescapeConfigEntry) -> bool: |
@@ -57,13 +59,32 @@ async def async_setup_entry(hass: HomeAssistant, entry: KaleidescapeConfigEntry) |
57 | 59 | DATA_DEVICE_TYPE: device_type, |
58 | 60 | } |
59 | 61 |
|
60 | | - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) |
| 62 | + loaded_platforms: list[KaleidescapePlatform] = [] |
| 63 | + for platform in PLATFORMS: |
| 64 | + try: |
| 65 | + await hass.config_entries.async_forward_entry_setups(entry, [platform]) |
| 66 | + loaded_platforms.append(platform) |
| 67 | + except Exception: |
| 68 | + _LOGGER.exception( |
| 69 | + "Failed to set up Kaleidescape platform '%s' for entry %s", |
| 70 | + platform, |
| 71 | + entry.entry_id, |
| 72 | + ) |
| 73 | + |
| 74 | + if not loaded_platforms: |
| 75 | + _LOGGER.error("No Kaleidescape platforms could be set up for entry %s", entry.entry_id) |
| 76 | + hass.data[DOMAIN].pop(entry.entry_id, None) |
| 77 | + return False |
| 78 | + |
| 79 | + hass.data[DOMAIN][entry.entry_id][DATA_LOADED_PLATFORMS] = loaded_platforms |
61 | 80 | entry.async_on_unload(entry.add_update_listener(async_reload_entry)) |
62 | 81 | return True |
63 | 82 |
|
64 | 83 |
|
65 | 84 | async def async_unload_entry(hass: HomeAssistant, entry: KaleidescapeConfigEntry) -> bool: |
66 | | - unloaded = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) |
| 85 | + entry_data = hass.data.get(DOMAIN, {}).get(entry.entry_id, {}) |
| 86 | + loaded_platforms = entry_data.get(DATA_LOADED_PLATFORMS, PLATFORMS) |
| 87 | + unloaded = await hass.config_entries.async_unload_platforms(entry, loaded_platforms) |
67 | 88 | if unloaded: |
68 | 89 | hass.data[DOMAIN].pop(entry.entry_id, None) |
69 | 90 | return unloaded |
|
0 commit comments