From 684a74c0fce32814e7866e83efb8d77aebda6d76 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Mon, 6 May 2024 09:33:17 +0200 Subject: [PATCH 1/2] Implement turn_on/turn_off on the water_heater * custom_components/daikin_onecta/water_heater.py: * tests/test_init.py: --- .../daikin_onecta/water_heater.py | 51 +++++++++++++++++++ tests/test_init.py | 48 +++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/custom_components/daikin_onecta/water_heater.py b/custom_components/daikin_onecta/water_heater.py index 1660f46..ce33b7d 100644 --- a/custom_components/daikin_onecta/water_heater.py +++ b/custom_components/daikin_onecta/water_heater.py @@ -250,6 +250,10 @@ async def async_set_operation_mode(self, operation_mode): # Only set the on/off to Daikin when we need to change it if on_off_mode != "": result &= await self._device.patch(self._device.id, self._embedded_id, "onOffMode", "", on_off_mode) + if result is True: + hwtd = self.hotwatertank_data + hwtd["onOffMode"]["value"] = on_off_mode + # Only set powerfulMode when it is set and supported by the device if (powerful_mode != "") and (STATE_PERFORMANCE in self.operation_list): result &= await self._device.patch( @@ -259,6 +263,12 @@ async def async_set_operation_mode(self, operation_mode): "", powerful_mode, ) + if result is True: + hwtd = self.hotwatertank_data + pwf = hwtd.get("powerfulMode") + if pwf is not None: + if pwf["settable"] is True: + pwf["value"] = powerful_mode if result is False: _LOGGER.warning("Device '%s' invalid tank state: %s", self._device.name, operation_mode) @@ -268,3 +278,44 @@ async def async_set_operation_mode(self, operation_mode): self.async_write_ha_state() return result + + async def async_turn_on(self): + """Turn device CLIMATE on.""" + _LOGGER.debug("Device '%s' request to turn on", self._device.name) + result = True + if self.current_operation == STATE_OFF: + result &= await self._device.patch(self._device.id, self._embedded_id, "onOffMode", "", "on") + if result is False: + _LOGGER.error("Device '%s' problem setting onOffMode to on", self._device.name) + else: + hwtd = self.hotwatertank_data + hwtd["onOffMode"]["value"] = "on" + self._attr_current_operation = self.get_current_operation() + self.async_write_ha_state() + else: + _LOGGER.debug( + "Device '%s' request to turn on ignored because device is already on", + self._device.name, + ) + + return result + + async def async_turn_off(self): + _LOGGER.debug("Device '%s' request to turn off", self._device.name) + result = True + if self.current_operation != STATE_OFF: + result &= await self._device.patch(self._device.id, self._embedded_id, "onOffMode", "", "off") + if result is False: + _LOGGER.error("Device '%s' problem setting onOffMode to off", self._device.name) + else: + hwtd = self.hotwatertank_data + hwtd["onOffMode"]["value"] = "off" + self._attr_current_operation = self.get_current_operation() + self.async_write_ha_state() + else: + _LOGGER.debug( + "Device '%s' request to turn off ignored because device is already off", + self._device.name, + ) + + return result diff --git a/tests/test_init.py b/tests/test_init.py index 4de57fc..5961cfa 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -364,6 +364,54 @@ async def test_water_heater( assert responses.calls[6].request.body == '{"value": "on"}' assert hass.states.get("water_heater.altherma").attributes["operation_mode"] == STATE_HEAT_PUMP + # Turn the tank again off using turn_off + await hass.services.async_call( + WATER_HEATER_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "water_heater.altherma"}, + blocking=True, + ) + await hass.async_block_till_done() + + assert len(responses.calls) == 8 + assert responses.calls[7].request.body == '{"value": "off"}' + assert hass.states.get("water_heater.altherma").attributes["operation_mode"] == STATE_OFF + + # Turn the tank again off using turn_off, will be a noop + await hass.services.async_call( + WATER_HEATER_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "water_heater.altherma"}, + blocking=True, + ) + await hass.async_block_till_done() + + assert len(responses.calls) == 8 + + # Turn the tank again on using turn_on + await hass.services.async_call( + WATER_HEATER_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "water_heater.altherma"}, + blocking=True, + ) + await hass.async_block_till_done() + + assert len(responses.calls) == 9 + assert responses.calls[8].request.body == '{"value": "on"}' + assert hass.states.get("water_heater.altherma").attributes["operation_mode"] == STATE_HEAT_PUMP + + # Turn the tank again on using turn_on, will be a noop + await hass.services.async_call( + WATER_HEATER_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "water_heater.altherma"}, + blocking=True, + ) + await hass.async_block_till_done() + + assert len(responses.calls) == 9 + @responses.activate async def test_climate( From 9a7b5e4447652e2b879369ac67a7c93e6cb87138 Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Mon, 6 May 2024 09:34:03 +0200 Subject: [PATCH 2/2] Updated comment * custom_components/daikin_onecta/water_heater.py: --- custom_components/daikin_onecta/water_heater.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/daikin_onecta/water_heater.py b/custom_components/daikin_onecta/water_heater.py index ce33b7d..2acb58f 100644 --- a/custom_components/daikin_onecta/water_heater.py +++ b/custom_components/daikin_onecta/water_heater.py @@ -280,7 +280,7 @@ async def async_set_operation_mode(self, operation_mode): return result async def async_turn_on(self): - """Turn device CLIMATE on.""" + """Turn water heater on.""" _LOGGER.debug("Device '%s' request to turn on", self._device.name) result = True if self.current_operation == STATE_OFF: @@ -301,6 +301,7 @@ async def async_turn_on(self): return result async def async_turn_off(self): + """Turn water heater off.""" _LOGGER.debug("Device '%s' request to turn off", self._device.name) result = True if self.current_operation != STATE_OFF: