From 6e7fe13d51b7a00791fa054b69eb0f60bd3cca48 Mon Sep 17 00:00:00 2001
From: jjlawren <jjlawren@users.noreply.github.com>
Date: Fri, 29 Oct 2021 14:43:59 -0500
Subject: [PATCH] Disable polling Sonos switches by default (#58705)

---
 homeassistant/components/sonos/switch.py |  1 +
 tests/components/sonos/test_switch.py    | 38 ++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/homeassistant/components/sonos/switch.py b/homeassistant/components/sonos/switch.py
index 9af6c1eebec..830f3b09481 100644
--- a/homeassistant/components/sonos/switch.py
+++ b/homeassistant/components/sonos/switch.py
@@ -136,6 +136,7 @@ class SonosSwitchEntity(SonosEntity, SwitchEntity):
         self._attr_icon = FEATURE_ICONS.get(feature_type)
 
         if feature_type in POLL_REQUIRED:
+            self._attr_entity_registry_enabled_default = False
             self._attr_should_poll = True
 
     async def _async_poll(self) -> None:
diff --git a/tests/components/sonos/test_switch.py b/tests/components/sonos/test_switch.py
index 906695bdbaf..c2f997dbcb6 100644
--- a/tests/components/sonos/test_switch.py
+++ b/tests/components/sonos/test_switch.py
@@ -1,7 +1,10 @@
 """Tests for the Sonos Alarm switch platform."""
 from copy import copy
+from datetime import timedelta
+from unittest.mock import patch
 
 from homeassistant.components.sonos import DOMAIN
+from homeassistant.components.sonos.const import DATA_SONOS_DISCOVERY_MANAGER
 from homeassistant.components.sonos.switch import (
     ATTR_DURATION,
     ATTR_ID,
@@ -10,9 +13,15 @@ from homeassistant.components.sonos.switch import (
     ATTR_RECURRENCE,
     ATTR_VOLUME,
 )
+from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
 from homeassistant.const import ATTR_TIME, STATE_ON
 from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry
 from homeassistant.setup import async_setup_component
+from homeassistant.util import dt
+
+from .conftest import SonosMockEvent
+
+from tests.common import async_fire_time_changed
 
 
 async def setup_platform(hass, config_entry, config):
@@ -67,7 +76,36 @@ async def test_switch_attributes(hass, config_entry, config, soco):
     crossfade_state = hass.states.get(crossfade.entity_id)
     assert crossfade_state.state == STATE_ON
 
+    # Ensure switches are disabled
     status_light = entity_registry.entities["switch.sonos_zone_a_status_light"]
+    assert hass.states.get(status_light.entity_id) is None
+
+    touch_controls = entity_registry.entities["switch.sonos_zone_a_touch_controls"]
+    assert hass.states.get(touch_controls.entity_id) is None
+
+    # Enable disabled switches
+    for entity in (status_light, touch_controls):
+        entity_registry.async_update_entity(
+            entity_id=entity.entity_id, disabled_by=None
+        )
+    await hass.async_block_till_done()
+
+    # Fire event to cancel poll timer and avoid triggering errors during time jump
+    service = soco.contentDirectory
+    empty_event = SonosMockEvent(soco, service, {})
+    subscription = service.subscribe.return_value
+    subscription.callback(event=empty_event)
+    await hass.async_block_till_done()
+
+    # Mock shutdown calls during config entry reload
+    with patch.object(hass.data[DATA_SONOS_DISCOVERY_MANAGER], "async_shutdown") as m:
+        async_fire_time_changed(
+            hass,
+            dt.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
+        )
+        await hass.async_block_till_done()
+        assert m.called
+
     status_light_state = hass.states.get(status_light.entity_id)
     assert status_light_state.state == STATE_ON
 
-- 
GitLab