Respect a disabled Bluetooth entry of a Ruuvi Gateway scanner (#195432)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Paulus Schoutsen
2026-11-30 09:17:01 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent e8ded26fca
commit 7ca69398dd
3 changed files with 43 additions and 6 deletions
@@ -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(
@@ -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
+30 -2
View File
@@ -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