From 7ca69398dd70a7531d19eec89b35293cf7251e4b Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 30 Nov 2026 03:17:01 -0500 Subject: [PATCH] Respect a disabled Bluetooth entry of a Ruuvi Gateway scanner (#195432) Co-authored-by: Claude Opus 5.5 --- .../components/ruuvi_gateway/__init__.py | 14 ++++++-- .../components/ruuvi_gateway/models.py | 3 +- tests/components/ruuvi_gateway/test_init.py | 32 +++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/ruuvi_gateway/__init__.py b/homeassistant/components/ruuvi_gateway/__init__.py index 663a398b501c..9b6dc808fb86 100644 --- a/homeassistant/components/ruuvi_gateway/__init__.py +++ b/homeassistant/components/ruuvi_gateway/__init__.py @@ -1,8 +1,12 @@ """The Ruuvi Gateway integration.""" import logging +from typing import TYPE_CHECKING -from homeassistant.components.bluetooth import async_remove_scanner +from homeassistant.components.bluetooth import ( + async_remote_scanner_enabled, + async_remove_scanner, +) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -21,12 +25,16 @@ async def async_setup_entry( ) -> None: """Set up Ruuvi Gateway from a config entry.""" coordinator = RuuviGatewayUpdateCoordinator(hass, entry, _LOGGER) - scanner, unload_scanner = async_connect_scanner(hass, entry, coordinator) + if TYPE_CHECKING: + assert entry.unique_id is not None + scanner = None + if async_remote_scanner_enabled(hass, entry.unique_id.upper(), entry.entry_id): + scanner, unload_scanner = async_connect_scanner(hass, entry, coordinator) + entry.async_on_unload(unload_scanner) entry.runtime_data = RuuviGatewayRuntimeData( update_coordinator=coordinator, scanner=scanner, ) - entry.async_on_unload(unload_scanner) async def async_unload_entry( diff --git a/homeassistant/components/ruuvi_gateway/models.py b/homeassistant/components/ruuvi_gateway/models.py index 201668e9a6cf..bd0016776af9 100644 --- a/homeassistant/components/ruuvi_gateway/models.py +++ b/homeassistant/components/ruuvi_gateway/models.py @@ -11,4 +11,5 @@ class RuuviGatewayRuntimeData: """Runtime data for Ruuvi Gateway integration.""" update_coordinator: RuuviGatewayUpdateCoordinator - scanner: RuuviGatewayScanner + # None when the user disabled the Bluetooth entry of the scanner + scanner: RuuviGatewayScanner | None diff --git a/tests/components/ruuvi_gateway/test_init.py b/tests/components/ruuvi_gateway/test_init.py index 4d18baef31e9..3ee871634897 100644 --- a/tests/components/ruuvi_gateway/test_init.py +++ b/tests/components/ruuvi_gateway/test_init.py @@ -2,9 +2,12 @@ from pytest_homeassistant.common import MockConfigEntry -from homeassistant.components.bluetooth import DOMAIN as BLUETOOTH_DOMAIN +from homeassistant.components.bluetooth import ( + DOMAIN as BLUETOOTH_DOMAIN, + async_scanner_by_source, +) from homeassistant.components.ruuvi_gateway.const import DOMAIN -from homeassistant.config_entries import ConfigEntryState +from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr @@ -71,3 +74,28 @@ async def test_scanner_removed_with_entry( ) is None ) + + +async def test_scanner_skipped_when_bluetooth_entry_disabled( + hass: HomeAssistant, +) -> None: + """Test a disabled Bluetooth entry skips the scanner.""" + MockConfigEntry( + domain=BLUETOOTH_DOMAIN, + unique_id=GATEWAY_MAC, + disabled_by=ConfigEntryDisabler.USER, + ).add_to_hass(hass) + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=GATEWAY_MAC_LOWER, + title=EXPECTED_TITLE, + data=BASE_DATA, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.LOADED + assert entry.runtime_data.scanner is None + assert async_scanner_by_source(hass, GATEWAY_MAC) is None