Skip to content

Commit

Permalink
More test switch, and don't call daikin when the requested value is a…
Browse files Browse the repository at this point in the history
…lready set

    * custom_components/daikin_onecta/switch.py:
    * tests/test_init.py:
  • Loading branch information
jwillemsen committed Mar 19, 2024
1 parent a82f315 commit 647620d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
44 changes: 31 additions & 13 deletions custom_components/daikin_onecta/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,42 @@ def device_info(self):

async def async_turn_on(self, **kwargs):
"""Turn the zone on."""
result = await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "on")
if result is False:
_LOGGER.warning("Device '%s' problem setting '%s' to on", self._device.name, self._value)
result = True
if not self.is_on:
result &= await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "on")
if result is False:
_LOGGER.warning("Device '%s' problem setting '%s' to on", self._device.name, self._value)
else:
self._switch_state = "on"
self.async_write_ha_state()
else:
self._switch_state = "on"
self.async_write_ha_state()
_LOGGER.debug(
"Device '%s' switch '%s' request to turn on ignored because is already on",
self._device.name,
self._value
)

return result

async def async_turn_off(self, **kwargs):
"""Turn the zone off."""
result = await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "off")
if result is False:
_LOGGER.warning(
"Device '%s' problem setting '%s' to off",
result = True
if self.is_on:
result &= await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "off")
if result is False:
_LOGGER.warning(
"Device '%s' problem setting '%s' to off",
self._device.name,
self._value,
)
else:
self._switch_state = "off"
self.async_write_ha_state()
else:
_LOGGER.debug(
"Device '%s' switch '%s' request to turn off ignored because is already off",
self._device.name,
self._value,
self._value
)
else:
self._switch_state = "off"
self.async_write_ha_state()

return result
22 changes: 22 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,17 @@ async def test_climate(
assert responses.calls[20].request.body == '{"value": "on"}'
assert hass.states.get("switch.werkkamer_climatecontrol_streamer_mode").state == STATE_ON

# Set the streamer mode on a second time shouldn't result in a call to daikin
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.werkkamer_climatecontrol_streamer_mode"},
blocking=True,
)
await hass.async_block_till_done()

assert len(responses.calls) == 21

# Set the streamer mode off
await hass.services.async_call(
SWITCH_DOMAIN,
Expand All @@ -505,3 +516,14 @@ async def test_climate(
assert len(responses.calls) == 22
assert responses.calls[21].request.body == '{"value": "off"}'
assert hass.states.get("switch.werkkamer_climatecontrol_streamer_mode").state == STATE_OFF

# Set the streamer mode off a second time shouldn't result in a call to daikin
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.werkkamer_climatecontrol_streamer_mode"},
blocking=True,
)
await hass.async_block_till_done()

assert len(responses.calls) == 22

0 comments on commit 647620d

Please sign in to comment.