diff --git a/homeassistant/components/overkiz/executor.py b/homeassistant/components/overkiz/executor.py index 94b2c1b25fa..02829eaf1a3 100644 --- a/homeassistant/components/overkiz/executor.py +++ b/homeassistant/components/overkiz/executor.py @@ -81,8 +81,14 @@ class OverkizExecutor: 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 @@ -107,8 +113,8 @@ class OverkizExecutor: "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] diff --git a/homeassistant/components/overkiz/water_heater/atlantic_domestic_hot_water_production_mlb_component.py b/homeassistant/components/overkiz/water_heater/atlantic_domestic_hot_water_production_mlb_component.py index 0f57d13433b..1b2a1e218d4 100644 --- a/homeassistant/components/overkiz/water_heater/atlantic_domestic_hot_water_production_mlb_component.py +++ b/homeassistant/components/overkiz/water_heater/atlantic_domestic_hot_water_production_mlb_component.py @@ -97,9 +97,9 @@ class AtlanticDomesticHotWaterProductionMBLComponent(OverkizEntity, WaterHeaterE @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 @@ -151,10 +151,40 @@ class AtlanticDomesticHotWaterProductionMBLComponent(OverkizEntity, WaterHeaterE await self.async_turn_away_mode_on() async def async_turn_away_mode_on(self) -> None: - """Turn away mode on.""" - await self.executor.async_execute_command( - OverkizCommand.SET_ABSENCE_MODE, OverkizCommandParam.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.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."""