Register a device for the Ruuvi Gateway Bluetooth scanner (#181826)

Co-authored-by: Joostlek <joostlek@outlook.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martino Dell'Ambrogio
2026-10-14 19:49:08 +02:00
committed by GitHub
co-authored by Joostlek Claude Opus 5.5
parent 74713af50a
commit 4185a0aa74
3 changed files with 92 additions and 2 deletions
@@ -2,6 +2,7 @@
import logging
from homeassistant.components.bluetooth import async_remove_scanner
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@@ -32,3 +33,11 @@ async def async_unload_entry(
hass: HomeAssistant, entry: RuuviGatewayConfigEntry
) -> None:
"""Unload a config entry."""
async def async_remove_entry(
hass: HomeAssistant, entry: RuuviGatewayConfigEntry
) -> None:
"""Remove a config entry."""
if mac_address := entry.unique_id:
async_remove_scanner(hass, mac_address.upper())
@@ -12,6 +12,7 @@ from homeassistant.components.bluetooth import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from .const import DOMAIN
from .coordinator import RuuviGatewayUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
@@ -71,7 +72,9 @@ def async_connect_scanner(
) -> tuple[RuuviGatewayScanner, CALLBACK_TYPE]:
"""Connect scanner and start polling."""
assert entry.unique_id is not None
source = str(entry.unique_id)
# Scanner sources are upper case MAC addresses everywhere else in core, and
# the device registry does not normalise CONNECTION_BLUETOOTH values.
source = str(entry.unique_id).upper()
_LOGGER.debug(
"%s [%s]: Connecting scanner",
entry.title,
@@ -83,7 +86,12 @@ def async_connect_scanner(
coordinator=coordinator,
)
unload_callbacks = [
async_register_scanner(hass, scanner),
async_register_scanner(
hass,
scanner,
source_domain=DOMAIN,
source_config_entry_id=entry.entry_id,
),
scanner.async_setup(),
scanner.start_polling(),
]
@@ -0,0 +1,73 @@
"""Test the Ruuvi Gateway setup."""
from homeassistant.components.bluetooth import DOMAIN as BLUETOOTH_DOMAIN
from homeassistant.components.ruuvi_gateway.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from .consts import BASE_DATA, EXPECTED_TITLE, GATEWAY_MAC, GATEWAY_MAC_LOWER
from tests.common import MockConfigEntry
async def test_scanner_device(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test the scanner gets a device entry that can hold an area."""
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
scanner_entry = hass.config_entries.async_entry_for_domain_unique_id(
BLUETOOTH_DOMAIN, GATEWAY_MAC
)
assert scanner_entry is not None
device_entry = device_registry.async_get_device_by_connection(
(dr.CONNECTION_BLUETOOTH, GATEWAY_MAC), scanner_entry.entry_id
)
assert device_entry is not None
async def test_scanner_removed_with_entry(
hass: HomeAssistant,
) -> None:
"""Test removing the entry also removes the scanner's Bluetooth entry."""
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 (
hass.config_entries.async_entry_for_domain_unique_id(
BLUETOOTH_DOMAIN, GATEWAY_MAC
)
is not None
)
assert await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
assert (
hass.config_entries.async_entry_for_domain_unique_id(
BLUETOOTH_DOMAIN, GATEWAY_MAC
)
is None
)