From 188831180085d51401e5f80fa76aee62dead7b0a Mon Sep 17 00:00:00 2001
From: David Knowles <dknowles2@gmail.com>
Date: Sun, 12 Nov 2023 17:09:08 -0500
Subject: [PATCH] Hydrawise: Explicitly set switch state on toggle (#103827)

Explicitly set switch state on toggle
---
 homeassistant/components/hydrawise/switch.py |  4 ++++
 tests/components/hydrawise/test_switch.py    | 12 ++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/homeassistant/components/hydrawise/switch.py b/homeassistant/components/hydrawise/switch.py
index caaefd7aa26..2aa4ecc085b 100644
--- a/homeassistant/components/hydrawise/switch.py
+++ b/homeassistant/components/hydrawise/switch.py
@@ -114,6 +114,8 @@ class HydrawiseSwitch(HydrawiseEntity, SwitchEntity):
             self.coordinator.api.run_zone(self._default_watering_timer, zone_number)
         elif self.entity_description.key == "auto_watering":
             self.coordinator.api.suspend_zone(0, zone_number)
+        self._attr_is_on = True
+        self.async_write_ha_state()
 
     def turn_off(self, **kwargs: Any) -> None:
         """Turn the device off."""
@@ -122,6 +124,8 @@ class HydrawiseSwitch(HydrawiseEntity, SwitchEntity):
             self.coordinator.api.run_zone(0, zone_number)
         elif self.entity_description.key == "auto_watering":
             self.coordinator.api.suspend_zone(365, zone_number)
+        self._attr_is_on = False
+        self.async_write_ha_state()
 
     def _update_attrs(self) -> None:
         """Update state attributes."""
diff --git a/tests/components/hydrawise/test_switch.py b/tests/components/hydrawise/test_switch.py
index 39d789f4cf9..1d2de7f8332 100644
--- a/tests/components/hydrawise/test_switch.py
+++ b/tests/components/hydrawise/test_switch.py
@@ -45,6 +45,9 @@ async def test_manual_watering_services(
         blocking=True,
     )
     mock_pydrawise.run_zone.assert_called_once_with(15, 1)
+    state = hass.states.get("switch.zone_one_manual_watering")
+    assert state is not None
+    assert state.state == "on"
     mock_pydrawise.reset_mock()
 
     await hass.services.async_call(
@@ -54,6 +57,9 @@ async def test_manual_watering_services(
         blocking=True,
     )
     mock_pydrawise.run_zone.assert_called_once_with(0, 1)
+    state = hass.states.get("switch.zone_one_manual_watering")
+    assert state is not None
+    assert state.state == "off"
 
 
 async def test_auto_watering_services(
@@ -67,6 +73,9 @@ async def test_auto_watering_services(
         blocking=True,
     )
     mock_pydrawise.suspend_zone.assert_called_once_with(365, 1)
+    state = hass.states.get("switch.zone_one_automatic_watering")
+    assert state is not None
+    assert state.state == "off"
     mock_pydrawise.reset_mock()
 
     await hass.services.async_call(
@@ -76,3 +85,6 @@ async def test_auto_watering_services(
         blocking=True,
     )
     mock_pydrawise.suspend_zone.assert_called_once_with(0, 1)
+    state = hass.states.get("switch.zone_one_automatic_watering")
+    assert state is not None
+    assert state.state == "on"
-- 
GitLab