Skip to content

Commit

Permalink
Merge pull request #180 from jwillemsen/jwi-waterheateronoff
Browse files Browse the repository at this point in the history
Implement turn_on/turn_off on the water_heater
  • Loading branch information
jwillemsen authored May 6, 2024
2 parents 158050e + 9a7b5e4 commit 4119d51
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
52 changes: 52 additions & 0 deletions custom_components/daikin_onecta/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -268,3 +278,45 @@ async def async_set_operation_mode(self, operation_mode):
self.async_write_ha_state()

return result

async def async_turn_on(self):
"""Turn water heater 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):
"""Turn water heater off."""
_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
48 changes: 48 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 4119d51

Please sign in to comment.