From 2c95c0b3a11c5098e23427d99d99932fd2f2a52c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 9 Jan 2023 12:16:14 -1000 Subject: [PATCH] Do not check ble scanner state for sleepy shelly devices (#85566) fixes #85563 --- homeassistant/components/shelly/coordinator.py | 3 ++- tests/components/shelly/test_init.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index 18d5ba1e152..18857a731cb 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -509,7 +509,8 @@ class ShellyRpcCoordinator(DataUpdateCoordinator[None]): This will be executed on connect or when the config entry is updated. """ - await self._async_connect_ble_scanner() + if not self.entry.data.get(CONF_SLEEP_PERIOD): + await self._async_connect_ble_scanner() async def _async_connect_ble_scanner(self) -> None: """Connect BLE scanner.""" diff --git a/tests/components/shelly/test_init.py b/tests/components/shelly/test_init.py index 0697c6fb613..3675186b9ba 100644 --- a/tests/components/shelly/test_init.py +++ b/tests/components/shelly/test_init.py @@ -213,7 +213,7 @@ async def test_entry_unload_not_connected(hass, mock_rpc_device, monkeypatch): assert entry.state is ConfigEntryState.LOADED -async def test_entry_unload_not_connected_but_we_with_we_are( +async def test_entry_unload_not_connected_but_we_think_we_are( hass, mock_rpc_device, monkeypatch ): """Test entry unload when not connected but we think we are still connected.""" @@ -238,3 +238,17 @@ async def test_entry_unload_not_connected_but_we_with_we_are( assert not mock_stop_scanner.call_count assert entry.state is ConfigEntryState.LOADED + + +async def test_no_attempt_to_stop_scanner_with_sleepy_devices(hass, mock_rpc_device): + """Test we do not try to stop the scanner if its disabled with a sleepy device.""" + with patch( + "homeassistant.components.shelly.coordinator.async_stop_scanner", + ) as mock_stop_scanner: + entry = await init_integration(hass, 2, sleep_period=7200) + assert entry.state is ConfigEntryState.LOADED + assert not mock_stop_scanner.call_count + + mock_rpc_device.mock_update() + await hass.async_block_till_done() + assert not mock_stop_scanner.call_count