|
13 | 13 | Platform, |
14 | 14 | ) |
15 | 15 | from homeassistant.core import HomeAssistant |
| 16 | +from homeassistant.helpers.device_registry import DeviceEntry |
16 | 17 | from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed |
17 | 18 |
|
18 | 19 | from .const import ( |
@@ -101,6 +102,53 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: |
101 | 102 | await hass.config_entries.async_reload(entry.entry_id) |
102 | 103 |
|
103 | 104 |
|
| 105 | +async def async_remove_config_entry_device( |
| 106 | + hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry |
| 107 | +) -> bool: |
| 108 | + """Remove a config entry from a device.""" |
| 109 | + solaredge_hub = hass.data[DOMAIN][config_entry.entry_id]["hub"] |
| 110 | + |
| 111 | + known_devices = [] |
| 112 | + |
| 113 | + for inverter in solaredge_hub.inverters: |
| 114 | + inverter_device_ids = { |
| 115 | + dev_id[1] |
| 116 | + for dev_id in inverter.device_info["identifiers"] |
| 117 | + if dev_id[0] == DOMAIN |
| 118 | + } |
| 119 | + for dev_id in inverter_device_ids: |
| 120 | + known_devices.append(dev_id) |
| 121 | + |
| 122 | + for meter in solaredge_hub.meters: |
| 123 | + meter_device_ids = { |
| 124 | + dev_id[1] |
| 125 | + for dev_id in meter.device_info["identifiers"] |
| 126 | + if dev_id[0] == DOMAIN |
| 127 | + } |
| 128 | + for dev_id in meter_device_ids: |
| 129 | + known_devices.append(dev_id) |
| 130 | + |
| 131 | + for battery in solaredge_hub.batteries: |
| 132 | + battery_device_ids = { |
| 133 | + dev_id[1] |
| 134 | + for dev_id in battery.device_info["identifiers"] |
| 135 | + if dev_id[0] == DOMAIN |
| 136 | + } |
| 137 | + for dev_id in battery_device_ids: |
| 138 | + known_devices.append(dev_id) |
| 139 | + |
| 140 | + this_device_ids = { |
| 141 | + dev_id[1] for dev_id in device_entry.identifiers if dev_id[0] == DOMAIN |
| 142 | + } |
| 143 | + |
| 144 | + for device_id in this_device_ids: |
| 145 | + if device_id in known_devices: |
| 146 | + _LOGGER.error(f"Failed to remove device entry: device {device_id} in use") |
| 147 | + return False |
| 148 | + |
| 149 | + return True |
| 150 | + |
| 151 | + |
104 | 152 | class SolarEdgeCoordinator(DataUpdateCoordinator): |
105 | 153 | def __init__(self, hass, hub, scan_interval): |
106 | 154 | super().__init__( |
|
0 commit comments