Skip to content

Commit a6f2735

Browse files
Merge pull request #54 from WillCodeForCats/47-add-config-option-to-hold-modbus-connection-open
New config option: keep connection open
2 parents 4403680 + 1b2151a commit a6f2735

File tree

12 files changed

+38
-11
lines changed

12 files changed

+38
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It is designed to communicate locally using Modbus/TCP where you have a single L
1010
* Meter support for 1 to 3 meters per inverter.
1111
* Battery support for 1 or 2 batteries per inverter.
1212
* Automatically detects meters and batteries.
13-
* Polling frequency configuration option (10 to 86400 seconds).
13+
* Polling frequency configuration option (1 to 86400 seconds).
1414
* Configurable starting inverter device ID.
1515
* Connects using Modbus/TCP - no cloud dependencies.
1616
* Informational sensor for device and its attributes

custom_components/solaredge_modbus_multi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
CONF_DETECT_METERS, DEFAULT_DETECT_METERS,
1818
CONF_DETECT_BATTERIES, DEFAULT_DETECT_BATTERIES,
1919
CONF_SINGLE_DEVICE_ENTITY, DEFAULT_SINGLE_DEVICE_ENTITY,
20+
CONF_KEEP_MODBUS_OPEN, DEFAULT_KEEP_MODBUS_OPEN,
2021
)
2122

2223
PLATFORMS: list[str] = ["sensor"]
@@ -46,6 +47,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
4647
entry.options.get(CONF_DETECT_METERS, DEFAULT_DETECT_METERS),
4748
entry.options.get(CONF_DETECT_BATTERIES, DEFAULT_DETECT_BATTERIES),
4849
entry.options.get(CONF_SINGLE_DEVICE_ENTITY, DEFAULT_SINGLE_DEVICE_ENTITY),
50+
entry.options.get(CONF_KEEP_MODBUS_OPEN, DEFAULT_KEEP_MODBUS_OPEN),
4951
)
5052

5153
await solaredge_hub.async_init_solaredge()

custom_components/solaredge_modbus_multi/config_flow.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
DEFAULT_DETECT_METERS,
1616
DEFAULT_DETECT_BATTERIES,
1717
DEFAULT_SINGLE_DEVICE_ENTITY,
18+
DEFAULT_KEEP_MODBUS_OPEN,
1819
CONF_DEVICE_ID,
1920
CONF_NUMBER_INVERTERS,
2021
CONF_DETECT_METERS,
2122
CONF_DETECT_BATTERIES,
2223
CONF_SINGLE_DEVICE_ENTITY,
24+
CONF_KEEP_MODBUS_OPEN,
2325
)
2426
from homeassistant.core import HomeAssistant, callback
2527
from homeassistant.data_entry_flow import FlowResult
@@ -133,7 +135,7 @@ async def async_step_init(self, user_input = None) -> FlowResult:
133135
"""Manage the options."""
134136
if user_input is not None:
135137

136-
if user_input[CONF_SCAN_INTERVAL] < 10:
138+
if user_input[CONF_SCAN_INTERVAL] < 1:
137139
errors[CONF_SCAN_INTERVAL] = "invalid_scan_interval"
138140
elif user_input[CONF_SCAN_INTERVAL] > 86400:
139141
errors[CONF_SCAN_INTERVAL] = "invalid_scan_interval"
@@ -146,6 +148,7 @@ async def async_step_init(self, user_input = None) -> FlowResult:
146148
user_input = {
147149
CONF_SCAN_INTERVAL: self.config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL),
148150
CONF_SINGLE_DEVICE_ENTITY: self.config_entry.options.get(CONF_SINGLE_DEVICE_ENTITY, DEFAULT_SINGLE_DEVICE_ENTITY),
151+
CONF_KEEP_MODBUS_OPEN: self.config_entry.options.get(CONF_KEEP_MODBUS_OPEN, DEFAULT_KEEP_MODBUS_OPEN),
149152
CONF_DETECT_METERS: self.config_entry.options.get(CONF_DETECT_METERS, DEFAULT_DETECT_METERS),
150153
CONF_DETECT_BATTERIES: self.config_entry.options.get(CONF_DETECT_BATTERIES, DEFAULT_DETECT_BATTERIES),
151154
}
@@ -160,6 +163,9 @@ async def async_step_init(self, user_input = None) -> FlowResult:
160163
vol.Optional(
161164
CONF_SINGLE_DEVICE_ENTITY, default=user_input[CONF_SINGLE_DEVICE_ENTITY]
162165
): cv.boolean,
166+
vol.Optional(
167+
CONF_KEEP_MODBUS_OPEN, default=user_input[CONF_KEEP_MODBUS_OPEN]
168+
): cv.boolean,
163169
vol.Optional(
164170
CONF_DETECT_METERS, default=user_input[CONF_DETECT_METERS]
165171
): cv.boolean,

custom_components/solaredge_modbus_multi/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
DEFAULT_DETECT_METERS = True
88
DEFAULT_DETECT_BATTERIES = False
99
DEFAULT_SINGLE_DEVICE_ENTITY = True
10+
DEFAULT_KEEP_MODBUS_OPEN = False
1011
CONF_NUMBER_INVERTERS = "number_of_inverters"
1112
CONF_DEVICE_ID = "device_id"
1213
CONF_DETECT_METERS = "detect_meters"
1314
CONF_DETECT_BATTERIES = "detect_batteries"
1415
CONF_SINGLE_DEVICE_ENTITY = "single_device_entity"
16+
CONF_KEEP_MODBUS_OPEN = "keep_modbus_open"
1517

1618
# units missing in homeassistant core
1719
ENERGY_VOLT_AMPERE_HOUR = "VAh"

custom_components/solaredge_modbus_multi/hub.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(
4141
detect_meters: bool = True,
4242
detect_batteries: bool = False,
4343
single_device_entity: bool = True,
44+
keep_modbus_open: bool = False,
4445
):
4546
"""Initialize the Modbus hub."""
4647
self._hass = hass
@@ -55,9 +56,17 @@ def __init__(
5556
self._detect_meters = detect_meters
5657
self._detect_batteries = detect_batteries
5758
self._single_device_entity = single_device_entity
59+
self._keep_modbus_open = keep_modbus_open
5860
self._sensors = []
5961
self.data = {}
6062

63+
if (
64+
scan_interval < 10 and
65+
not self._keep_modbus_open
66+
):
67+
_LOGGER.warning("Polling frequency < 10, enabling keep modbus open option.")
68+
self._keep_modbus_open = True
69+
6170
self._client = ModbusTcpClient(host=self._host, port=self._port)
6271

6372
self._id = name.lower()
@@ -151,7 +160,8 @@ async def async_init_solaredge(self) -> None:
151160
except:
152161
raise ConfigEntryNotReady(f"Devices not ready.")
153162

154-
self.close()
163+
if not self._keep_modbus_open:
164+
self.close()
155165

156166
self._polling_interval = async_track_time_interval(
157167
self._hass, self.async_refresh_modbus_data, self._scan_interval
@@ -197,7 +207,8 @@ async def async_refresh_modbus_data(self, _now: Optional[int] = None) -> None:
197207
for battery in self.batteries:
198208
await battery.publish_updates()
199209

200-
self.close()
210+
if not self._keep_modbus_open:
211+
self.close()
201212

202213
@property
203214
def name(self):

custom_components/solaredge_modbus_multi/strings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
"data": {
3737
"scan_interval": "Polling Frequency (seconds)",
3838
"single_device_entity": "Single Device Entity",
39+
"keep_modbus_open": "Keep Modbus Connection Open",
3940
"detect_meters": "Auto-Detect Meters",
4041
"detect_batteries": "Auto-Detect Batteries"
4142
}
4243
}
4344
},
4445
"error": {
45-
"invalid_scan_interval": "Valid interval is 10 to 86400 seconds."
46+
"invalid_scan_interval": "Valid interval is 1 to 86400 seconds."
4647
}
4748
}
4849
}

custom_components/solaredge_modbus_multi/translations/de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
"data": {
3737
"scan_interval": "Abfragehäufigkeit (Sekunden)",
3838
"single_device_entity": "Einzelne Geräteeinheit",
39+
"keep_modbus_open": "Modbus-Verbindung geöffnet lassen",
3940
"detect_meters": "Messgeräte automatisch erkennen",
4041
"detect_batteries": "Batterien automatisch erkennen"
4142
}
4243
}
4344
},
4445
"error": {
45-
"invalid_scan_interval": "Gültiges Intervall ist 10 bis 86400 Sekunden."
46+
"invalid_scan_interval": "Gültiges Intervall ist 1 bis 86400 Sekunden."
4647
}
4748
}
4849
}

custom_components/solaredge_modbus_multi/translations/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
"data": {
3737
"scan_interval": "Polling Frequency (seconds)",
3838
"single_device_entity": "Single Device Entity",
39+
"keep_modbus_open": "Keep Modbus Connection Open",
3940
"detect_meters": "Auto-Detect Meters",
4041
"detect_batteries": "Auto-Detect Batteries"
4142
}
4243
}
4344
},
4445
"error": {
45-
"invalid_scan_interval": "Valid interval is 10 to 86400 seconds."
46+
"invalid_scan_interval": "Valid interval is 1 to 86400 seconds."
4647
}
4748
}
4849
}

custom_components/solaredge_modbus_multi/translations/nb.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
"data": {
3737
"scan_interval": "Avstemningsfrekvens (sekunder)",
3838
"single_device_entity": "Enkelt enhetsenhet",
39+
"keep_modbus_open": "Hold Modbus-tilkoblingen åpen",
3940
"detect_meters": "Automatisk oppdagelse av målere",
4041
"detect_batteries": "Automatisk gjenkjenning av batterier"
4142
}
4243
}
4344
},
4445
"error": {
45-
"invalid_scan_interval": "Gyldig intervall er 10 til 86400 sekunder."
46+
"invalid_scan_interval": "Gyldig intervall er 1 til 86400 sekunder."
4647
}
4748
}
4849
}

custom_components/solaredge_modbus_multi/translations/nl.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
"data": {
3737
"scan_interval": "Oproepfrequentie (seconden)",
3838
"single_device_entity": "Entiteit met één apparaat",
39+
"keep_modbus_open": "Houd Modbus-verbinding open",
3940
"detect_meters": "Meters automatisch detecteren",
4041
"detect_batteries": "Batterijen automatisch detecteren"
4142
}
4243
}
4344
},
4445
"error": {
45-
"invalid_scan_interval": "Geldig interval is 10 tot 86400 seconden."
46+
"invalid_scan_interval": "Geldig interval is 1 tot 86400 seconden."
4647
}
4748
}
4849
}

0 commit comments

Comments
 (0)