mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Improve remote Bluetooth scanner manufacturer data (#135961)
Co-authored-by: Joostlek <joostlek@outlook.com>
This commit is contained in:
parent
85bea5b70e
commit
0d968267a2
@ -22,6 +22,7 @@ from bluetooth_adapters import (
|
|||||||
adapter_model,
|
adapter_model,
|
||||||
adapter_unique_name,
|
adapter_unique_name,
|
||||||
get_adapters,
|
get_adapters,
|
||||||
|
get_manufacturer_from_mac,
|
||||||
)
|
)
|
||||||
from bluetooth_data_tools import monotonic_time_coarse as MONOTONIC_TIME
|
from bluetooth_data_tools import monotonic_time_coarse as MONOTONIC_TIME
|
||||||
from habluetooth import (
|
from habluetooth import (
|
||||||
@ -333,15 +334,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
address = entry.unique_id
|
address = entry.unique_id
|
||||||
assert address is not None
|
assert address is not None
|
||||||
assert source_entry is not None
|
assert source_entry is not None
|
||||||
|
source_domain = entry.data[CONF_SOURCE_DOMAIN]
|
||||||
|
if mac_manufacturer := await get_manufacturer_from_mac(address):
|
||||||
|
manufacturer = f"{mac_manufacturer} ({source_domain})"
|
||||||
|
else:
|
||||||
|
manufacturer = source_domain
|
||||||
|
details = AdapterDetails(
|
||||||
|
address=address,
|
||||||
|
product=entry.data.get(CONF_SOURCE_MODEL),
|
||||||
|
manufacturer=manufacturer,
|
||||||
|
)
|
||||||
await async_update_device(
|
await async_update_device(
|
||||||
hass,
|
hass,
|
||||||
entry,
|
entry,
|
||||||
source_entry.title,
|
source_entry.title,
|
||||||
AdapterDetails(
|
details,
|
||||||
address=address,
|
|
||||||
product=entry.data.get(CONF_SOURCE_MODEL),
|
|
||||||
manufacturer=entry.data[CONF_SOURCE_DOMAIN],
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
manager = _get_manager(hass)
|
manager = _get_manager(hass)
|
||||||
|
@ -25,7 +25,9 @@ from homeassistant.components.bluetooth.const import (
|
|||||||
UNAVAILABLE_TRACK_SECONDS,
|
UNAVAILABLE_TRACK_SECONDS,
|
||||||
)
|
)
|
||||||
from homeassistant.components.bluetooth.manager import HomeAssistantBluetoothManager
|
from homeassistant.components.bluetooth.manager import HomeAssistantBluetoothManager
|
||||||
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.util.json import json_loads
|
from homeassistant.util.json import json_loads
|
||||||
@ -523,7 +525,19 @@ async def test_scanner_stops_responding(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("enable_bluetooth")
|
@pytest.mark.usefixtures("enable_bluetooth")
|
||||||
async def test_remote_scanner_bluetooth_config_entry(hass: HomeAssistant) -> None:
|
@pytest.mark.parametrize(
|
||||||
|
("manufacturer", "source"),
|
||||||
|
[
|
||||||
|
("test", "test"),
|
||||||
|
("Raspberry Pi Trading Ltd (test)", "28:CD:C1:11:23:45"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_remote_scanner_bluetooth_config_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
manufacturer: str,
|
||||||
|
source: str,
|
||||||
|
) -> None:
|
||||||
"""Test the remote scanner gets a bluetooth config entry."""
|
"""Test the remote scanner gets a bluetooth config entry."""
|
||||||
manager: HomeAssistantBluetoothManager = _get_manager()
|
manager: HomeAssistantBluetoothManager = _get_manager()
|
||||||
|
|
||||||
@ -543,8 +557,9 @@ async def test_remote_scanner_bluetooth_config_entry(hass: HomeAssistant) -> Non
|
|||||||
connector = (
|
connector = (
|
||||||
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
|
HaBluetoothConnector(MockBleakClient, "mock_bleak_client", lambda: False),
|
||||||
)
|
)
|
||||||
scanner = FakeScanner("esp32", "esp32", connector, True)
|
scanner = FakeScanner(source, source, connector, True)
|
||||||
unsetup = scanner.async_setup()
|
unsetup = scanner.async_setup()
|
||||||
|
assert scanner.source == source
|
||||||
entry = MockConfigEntry(domain="test")
|
entry = MockConfigEntry(domain="test")
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
cancel = manager.async_register_hass_scanner(
|
cancel = manager.async_register_hass_scanner(
|
||||||
@ -561,9 +576,18 @@ async def test_remote_scanner_bluetooth_config_entry(hass: HomeAssistant) -> Non
|
|||||||
cancel()
|
cancel()
|
||||||
unsetup()
|
unsetup()
|
||||||
|
|
||||||
assert hass.config_entries.async_entry_for_domain_unique_id(
|
adapter_entry = hass.config_entries.async_entry_for_domain_unique_id(
|
||||||
"bluetooth", scanner.source
|
"bluetooth", scanner.source
|
||||||
)
|
)
|
||||||
|
assert adapter_entry is not None
|
||||||
|
assert adapter_entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
dev = device_registry.async_get_device(
|
||||||
|
connections={(dr.CONNECTION_BLUETOOTH, scanner.source)}
|
||||||
|
)
|
||||||
|
assert dev is not None
|
||||||
|
assert dev.config_entries == {adapter_entry.entry_id}
|
||||||
|
assert dev.manufacturer == manufacturer
|
||||||
|
|
||||||
manager.async_remove_scanner(scanner.source)
|
manager.async_remove_scanner(scanner.source)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user