Skip to content

Commit

Permalink
Merge branch 'main' into power-control-block
Browse files Browse the repository at this point in the history
  • Loading branch information
WillCodeForCats committed Aug 30, 2023
2 parents a56b165 + a23a11c commit c29efbb
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 183 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ After rebooting Home Assistant, this integration can be configured through the i
[WillCodeForCats/solaredge-modbus-multi/wiki](https://github.com/WillCodeForCats/solaredge-modbus-multi/wiki)

### Required Versions
* Home Assistant 2023.7.0 or newer
* Python 3.10 or newer
* Home Assistant 2023.8.0 or newer
* Python 3.11 or newer
* pymodbus 3.3.1 or newer

## Specifications
Expand Down
7 changes: 5 additions & 2 deletions custom_components/solaredge_modbus_multi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from datetime import timedelta
from typing import Any

import async_timeout
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_HOST,
Expand Down Expand Up @@ -188,6 +187,10 @@ def __init__(

async def _async_update_data(self):
try:
while self._hub.has_write:
_LOGGER.debug(f"Waiting for write {self._hub.has_write}")
await asyncio.sleep(1)

return await self._refresh_modbus_data_with_retry(
ex_type=DataUpdateFailed,
limit=RetrySettings.Limit,
Expand Down Expand Up @@ -222,7 +225,7 @@ async def _refresh_modbus_data_with_retry(
attempt = 1
while True:
try:
async with async_timeout.timeout(self._hub.coordinator_timeout):
async with asyncio.timeout(self._hub.coordinator_timeout):
return await self._hub.async_refresh_modbus_data()
except Exception as ex:
if not isinstance(ex, ex_type):
Expand Down
29 changes: 14 additions & 15 deletions custom_components/solaredge_modbus_multi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ async def async_step_init(self, user_input=None) -> FlowResult:
errors[CONF_SCAN_INTERVAL] = "invalid_scan_interval"
elif user_input[CONF_SCAN_INTERVAL] > 86400:
errors[CONF_SCAN_INTERVAL] = "invalid_scan_interval"
elif user_input[ConfName.SLEEP_AFTER_WRITE] < 0:
errors[ConfName.SLEEP_AFTER_WRITE] = "invalid_sleep_interval"
elif user_input[ConfName.SLEEP_AFTER_WRITE] > 60:
errors[ConfName.SLEEP_AFTER_WRITE] = "invalid_sleep_interval"
else:
if user_input[ConfName.DETECT_BATTERIES] is True:
self.init_info = user_input
Expand Down Expand Up @@ -146,6 +150,9 @@ async def async_step_init(self, user_input=None) -> FlowResult:
ConfName.ADV_PWR_CONTROL: self.config_entry.options.get(
ConfName.ADV_PWR_CONTROL, bool(ConfDefaultFlag.ADV_PWR_CONTROL)
),
ConfName.SLEEP_AFTER_WRITE: self.config_entry.options.get(
ConfName.SLEEP_AFTER_WRITE, ConfDefaultInt.SLEEP_AFTER_WRITE
),
}

return self.async_show_form(
Expand Down Expand Up @@ -176,6 +183,10 @@ async def async_step_init(self, user_input=None) -> FlowResult:
f"{ConfName.ADV_PWR_CONTROL}",
default=user_input[ConfName.ADV_PWR_CONTROL],
): cv.boolean,
vol.Optional(
f"{ConfName.SLEEP_AFTER_WRITE}",
default=user_input[ConfName.SLEEP_AFTER_WRITE],
): vol.Coerce(int),
},
),
errors=errors,
Expand Down Expand Up @@ -241,14 +252,9 @@ async def async_step_adv_pwr_ctl(self, user_input=None) -> FlowResult:
errors = {}

if user_input is not None:
if user_input[ConfName.SLEEP_AFTER_WRITE] < 0:
errors[ConfName.SLEEP_AFTER_WRITE] = "invalid_sleep_interval"
elif user_input[ConfName.SLEEP_AFTER_WRITE] > 60:
errors[ConfName.SLEEP_AFTER_WRITE] = "invalid_sleep_interval"
else:
return self.async_create_entry(
title="", data={**self.init_info, **user_input}
)
return self.async_create_entry(
title="", data={**self.init_info, **user_input}
)

else:
user_input = {
Expand All @@ -260,9 +266,6 @@ async def async_step_adv_pwr_ctl(self, user_input=None) -> FlowResult:
ConfName.ADV_SITE_LIMIT_CONTROL,
bool(ConfDefaultFlag.ADV_SITE_LIMIT_CONTROL),
),
ConfName.SLEEP_AFTER_WRITE: self.config_entry.options.get(
ConfName.SLEEP_AFTER_WRITE, ConfDefaultInt.SLEEP_AFTER_WRITE
),
}

return self.async_show_form(
Expand All @@ -277,10 +280,6 @@ async def async_step_adv_pwr_ctl(self, user_input=None) -> FlowResult:
f"{ConfName.ADV_SITE_LIMIT_CONTROL}",
default=user_input[ConfName.ADV_SITE_LIMIT_CONTROL],
): cv.boolean,
vol.Optional(
f"{ConfName.SLEEP_AFTER_WRITE}",
default=user_input[ConfName.SLEEP_AFTER_WRITE],
): vol.Coerce(int),
}
),
errors=errors,
Expand Down
Loading

0 comments on commit c29efbb

Please sign in to comment.