Skip to content

Commit

Permalink
Merge pull request #591 from canton7/bugfix/charge-window
Browse files Browse the repository at this point in the history
Only disable remote control when the user disables force charge/discharge
  • Loading branch information
canton7 authored Apr 13, 2024
2 parents 1cd307b + 1d276f3 commit 284caff
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions custom_components/foxess_modbus/remote_control_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(

self._mode = RemoteControlMode.DISABLE
self._prev_mode = RemoteControlMode.DISABLE
self._remote_control_enabled: bool | None = None # None = we don't know
self._remote_control_enabled = False
self._current_import_power = 0 # Set the first time that we enable force charge
self._discharge_power: int | None = None
self._charge_power: int | None = None
Expand Down Expand Up @@ -297,22 +297,27 @@ async def _update_discharge(self) -> None:
await self._write_active_power(export_power)

async def _enable_remote_control(self, fallback_work_mode: WorkMode) -> None:
if self._remote_control_enabled in (None, False):
# We set a fallback work mode so that the inverter still does "roughly" the right thing if we disconnect
# (This might not be available, e.g. on H1 LAN)
current_work_mode = self._read(self._addresses.work_mode, signed=False)
if current_work_mode != fallback_work_mode and self._addresses.work_mode is not None:
await self._controller.write_register(self._addresses.work_mode, int(fallback_work_mode))

if not self._remote_control_enabled:
self._remote_control_enabled = True
timeout = self._poll_rate * 2

# We set a fallback work mode so that the inverter still does "roughly" the right thing if we disconnect
# (This might not be available, e.g. on H1 LAN)
current_work_mode = self._read(self._addresses.work_mode, signed=False)
if current_work_mode != fallback_work_mode and self._addresses.work_mode is not None:
await self._controller.write_register(self._addresses.work_mode, int(fallback_work_mode))

# We can't do multi-register writes to these registers
await self._controller.write_register(self._addresses.timeout_set, timeout)
await self._controller.write_register(self._addresses.remote_enable, 1)

async def _disable_remote_control(self, work_mode: WorkMode | None = None) -> None:
if self._remote_control_enabled in (None, True):
# The strategy periods feature of the foxess app use the remote control register internally. If we disable
# remote control when a strategy period is active, we'll end up disabling it.
# We therefore need to be a bit careful, and only disable remote control if we previously enabled it.
# If we did have it enabled, but then restarted, then we just need to let the watchdog catch it.

if self._remote_control_enabled:
self._remote_control_enabled = False
await self._controller.write_register(self._addresses.remote_enable, 0)

Expand All @@ -335,7 +340,7 @@ async def poll_complete_callback(self) -> None:
await self._update()

async def became_connected_callback(self) -> None:
self._remote_control_enabled = None # Don't know whether it's enabled or not
self._remote_control_enabled = False
await self._update()

def update_callback(self, changed_addresses: set[int]) -> None:
Expand Down

0 comments on commit 284caff

Please sign in to comment.