Skip to content

Commit ca81aae

Browse files
Merge pull request #104 from WillCodeForCats/remove-device-support
Support removing devices from UI
2 parents e2bea60 + 0797e18 commit ca81aae

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

custom_components/solaredge_modbus_multi/__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Platform,
1414
)
1515
from homeassistant.core import HomeAssistant
16+
from homeassistant.helpers.device_registry import DeviceEntry
1617
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
1718

1819
from .const import (
@@ -101,6 +102,53 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
101102
await hass.config_entries.async_reload(entry.entry_id)
102103

103104

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+
104152
class SolarEdgeCoordinator(DataUpdateCoordinator):
105153
def __init__(self, hass, hub, scan_interval):
106154
super().__init__(

custom_components/solaredge_modbus_multi/hub.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import logging
32
import threading
43
from collections import OrderedDict
@@ -93,7 +92,7 @@ def __init__(
9392
self.inverter_common = {}
9493
self.mmppt_common = {}
9594

96-
self._client = ModbusTcpClient(host=self._host, port=self._port)
95+
self._client = None
9796

9897
self.initalized = False
9998
self.online = False
@@ -340,19 +339,24 @@ def disconnect(self) -> None:
340339
async def connect(self) -> None:
341340
"""Connect modbus client."""
342341
with self._lock:
342+
if self._client is None:
343+
self._client = ModbusTcpClient(host=self._host, port=self._port)
344+
343345
await self._hass.async_add_executor_job(self._client.connect)
344346

345347
def is_socket_open(self) -> bool:
346348
"""Check modbus client connection status."""
347349
with self._lock:
350+
if self._client is None:
351+
return False
352+
348353
return self._client.is_socket_open()
349354

350355
async def shutdown(self) -> None:
351356
"""Shut down the hub."""
352357
self.online = False
353358
self.disconnect()
354359
self._client = None
355-
await asyncio.sleep(3)
356360

357361
def read_holding_registers(self, unit, address, count):
358362
"""Read holding registers."""

0 commit comments

Comments
 (0)