Skip to content

Commit 647620d

Browse files
committed
More test switch, and don't call daikin when the requested value is already set
* custom_components/daikin_onecta/switch.py: * tests/test_init.py:
1 parent a82f315 commit 647620d

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

custom_components/daikin_onecta/switch.py

+31-13
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,42 @@ def device_info(self):
147147

148148
async def async_turn_on(self, **kwargs):
149149
"""Turn the zone on."""
150-
result = await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "on")
151-
if result is False:
152-
_LOGGER.warning("Device '%s' problem setting '%s' to on", self._device.name, self._value)
150+
result = True
151+
if not self.is_on:
152+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "on")
153+
if result is False:
154+
_LOGGER.warning("Device '%s' problem setting '%s' to on", self._device.name, self._value)
155+
else:
156+
self._switch_state = "on"
157+
self.async_write_ha_state()
153158
else:
154-
self._switch_state = "on"
155-
self.async_write_ha_state()
159+
_LOGGER.debug(
160+
"Device '%s' switch '%s' request to turn on ignored because is already on",
161+
self._device.name,
162+
self._value
163+
)
164+
156165
return result
157166

158167
async def async_turn_off(self, **kwargs):
159168
"""Turn the zone off."""
160-
result = await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "off")
161-
if result is False:
162-
_LOGGER.warning(
163-
"Device '%s' problem setting '%s' to off",
169+
result = True
170+
if self.is_on:
171+
result &= await self._device.set_path(self._device.getId(), self._embedded_id, self._value, "", "off")
172+
if result is False:
173+
_LOGGER.warning(
174+
"Device '%s' problem setting '%s' to off",
175+
self._device.name,
176+
self._value,
177+
)
178+
else:
179+
self._switch_state = "off"
180+
self.async_write_ha_state()
181+
else:
182+
_LOGGER.debug(
183+
"Device '%s' switch '%s' request to turn off ignored because is already off",
164184
self._device.name,
165-
self._value,
185+
self._value
166186
)
167-
else:
168-
self._switch_state = "off"
169-
self.async_write_ha_state()
187+
170188
return result

tests/test_init.py

+22
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,17 @@ async def test_climate(
493493
assert responses.calls[20].request.body == '{"value": "on"}'
494494
assert hass.states.get("switch.werkkamer_climatecontrol_streamer_mode").state == STATE_ON
495495

496+
# Set the streamer mode on a second time shouldn't result in a call to daikin
497+
await hass.services.async_call(
498+
SWITCH_DOMAIN,
499+
SERVICE_TURN_ON,
500+
{ATTR_ENTITY_ID: "switch.werkkamer_climatecontrol_streamer_mode"},
501+
blocking=True,
502+
)
503+
await hass.async_block_till_done()
504+
505+
assert len(responses.calls) == 21
506+
496507
# Set the streamer mode off
497508
await hass.services.async_call(
498509
SWITCH_DOMAIN,
@@ -505,3 +516,14 @@ async def test_climate(
505516
assert len(responses.calls) == 22
506517
assert responses.calls[21].request.body == '{"value": "off"}'
507518
assert hass.states.get("switch.werkkamer_climatecontrol_streamer_mode").state == STATE_OFF
519+
520+
# Set the streamer mode off a second time shouldn't result in a call to daikin
521+
await hass.services.async_call(
522+
SWITCH_DOMAIN,
523+
SERVICE_TURN_OFF,
524+
{ATTR_ENTITY_ID: "switch.werkkamer_climatecontrol_streamer_mode"},
525+
blocking=True,
526+
)
527+
await hass.async_block_till_done()
528+
529+
assert len(responses.calls) == 22

0 commit comments

Comments
 (0)