Skip to content

Commit

Permalink
Update overkiz Atlantic Water Heater away mode switching (#121801)
Browse files Browse the repository at this point in the history
  • Loading branch information
ALERTua authored Sep 27, 2024
1 parent 76858f0 commit 27f3715
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
14 changes: 10 additions & 4 deletions homeassistant/components/overkiz/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ def select_attribute(self, *attributes: str) -> OverkizStateType:

return None

async def async_execute_command(self, command_name: str, *args: Any) -> None:
"""Execute device command in async context."""
async def async_execute_command(
self, command_name: str, *args: Any, refresh_afterwards: bool = True
) -> None:
"""Execute device command in async context.
:param refresh_afterwards: Whether to refresh the device state after the command is executed.
If several commands are executed, it will be refreshed only once.
"""
parameters = [arg for arg in args if arg is not None]
# Set the execution duration to 0 seconds for RTS devices on supported commands
# Default execution duration is 30 seconds and will block consecutive commands
Expand All @@ -107,8 +113,8 @@ async def async_execute_command(self, command_name: str, *args: Any) -> None:
"device_url": self.device.device_url,
"command_name": command_name,
}

await self.coordinator.async_refresh()
if refresh_afterwards:
await self.coordinator.async_refresh()

async def async_cancel_command(
self, commands_to_cancel: list[OverkizCommand]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def is_eco_mode_on(self) -> bool:
@property
def is_away_mode_on(self) -> bool:
"""Return true if away mode is on."""
return (
self.executor.select_state(OverkizState.MODBUSLINK_DHW_ABSENCE_MODE)
== OverkizCommandParam.ON
return self.executor.select_state(OverkizState.MODBUSLINK_DHW_ABSENCE_MODE) in (
OverkizCommandParam.ON,
OverkizCommandParam.PROG,
)

@property
Expand Down Expand Up @@ -151,10 +151,40 @@ async def async_set_operation_mode(self, operation_mode: str) -> None:
await self.async_turn_away_mode_on()

async def async_turn_away_mode_on(self) -> None:
"""Turn away mode on."""
"""Turn away mode on.
This requires the start date and the end date to be also set.
The API accepts setting dates in the format of the core:DateTimeState state for the DHW
{'day': 11, 'hour': 21, 'minute': 12, 'month': 7, 'second': 53, 'weekday': 3, 'year': 2024})
The dict is then passed as an away mode start date, and then as an end date, but with the year incremented by 1,
so the away mode is getting turned on for the next year.
The weekday number seems to have no effect so the calculation of the future date's weekday number is redundant,
but possible via homeassistant dt_util to form both start and end dates dictionaries from scratch
based on datetime.now() and datetime.timedelta into the future.
If you execute `setAbsenceStartDate`, `setAbsenceEndDate` and `setAbsenceMode`,
the API answers with "too many requests", as there's a polling update after each command execution,
and the device becomes unavailable until the API is available again.
With `refresh_afterwards=False` on the first commands, and `refresh_afterwards=True` only the last command,
the API is not choking and the transition is smooth without the unavailability state.
"""
now_date = cast(
dict,
self.executor.select_state(OverkizState.CORE_DATETIME),
)
await self.executor.async_execute_command(
OverkizCommand.SET_ABSENCE_MODE, OverkizCommandParam.ON
OverkizCommand.SET_ABSENCE_MODE,
OverkizCommandParam.PROG,
refresh_afterwards=False,
)
await self.executor.async_execute_command(
OverkizCommand.SET_ABSENCE_START_DATE, now_date, refresh_afterwards=False
)
now_date["year"] = now_date["year"] + 1
await self.executor.async_execute_command(
OverkizCommand.SET_ABSENCE_END_DATE, now_date, refresh_afterwards=False
)

await self.coordinator.async_refresh()

async def async_turn_away_mode_off(self) -> None:
"""Turn away mode off."""
Expand Down

0 comments on commit 27f3715

Please sign in to comment.