Skip to content

Commit 324aa09

Browse files
bdracoballoob
andauthored
Update Improv BLE discovery notification when device name changes (home-assistant#154352)
Co-authored-by: Paulus Schoutsen <[email protected]>
1 parent 663431f commit 324aa09

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

homeassistant/components/improv_ble/config_flow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ def _async_update_ble(
170170
)
171171

172172
self._discovery_info = service_info
173+
174+
# Update title placeholders if name changed
175+
name = service_info.name or service_info.address
176+
if self.context.get("title_placeholders", {}).get("name") != name:
177+
self.async_update_title_placeholders({"name": name})
178+
173179
try:
174180
self._abort_if_provisioned()
175181
except AbortFlow:

tests/components/improv_ble/test_config_flow.py

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
import pytest
99

1010
from homeassistant import config_entries
11-
from homeassistant.components.bluetooth import BluetoothChange
11+
from homeassistant.components.bluetooth import (
12+
BluetoothChange,
13+
BluetoothServiceInfoBleak,
14+
)
1215
from homeassistant.components.improv_ble.const import DOMAIN
1316
from homeassistant.config_entries import SOURCE_IGNORE
1417
from homeassistant.const import CONF_ADDRESS
@@ -22,8 +25,12 @@
2225
PROVISIONED_IMPROV_BLE_DISCOVERY_INFO,
2326
)
2427

25-
from tests.common import MockConfigEntry
26-
from tests.components.bluetooth import inject_bluetooth_service_info_bleak
28+
from tests.common import MockConfigEntry, async_capture_events
29+
from tests.components.bluetooth import (
30+
generate_advertisement_data,
31+
generate_ble_device,
32+
inject_bluetooth_service_info_bleak,
33+
)
2734

2835
IMPROV_BLE = "homeassistant.components.improv_ble"
2936

@@ -803,3 +810,62 @@ async def test_provision_fails_invalid_data(
803810
"Received invalid improv via BLE data '000000000000' from device with bluetooth address 'AA:BB:CC:DD:EE:F0'"
804811
in caplog.text
805812
)
813+
814+
815+
async def test_bluetooth_name_update(hass: HomeAssistant) -> None:
816+
"""Test that discovery notification title updates when device name changes."""
817+
with patch(
818+
f"{IMPROV_BLE}.config_flow.bluetooth.async_register_callback",
819+
) as mock_async_register_callback:
820+
result = await hass.config_entries.flow.async_init(
821+
DOMAIN,
822+
context={"source": config_entries.SOURCE_BLUETOOTH},
823+
data=IMPROV_BLE_DISCOVERY_INFO,
824+
)
825+
assert result["type"] is FlowResultType.FORM
826+
assert result["step_id"] == "bluetooth_confirm"
827+
828+
# Get the flow to check initial title_placeholders
829+
flow = hass.config_entries.flow.async_get(result["flow_id"])
830+
assert flow["context"]["title_placeholders"] == {"name": "00123456"}
831+
832+
# Get the callback that was registered
833+
callback = mock_async_register_callback.call_args.args[1]
834+
835+
# Create updated discovery info with a new name
836+
updated_discovery_info = BluetoothServiceInfoBleak(
837+
name="improvtest",
838+
address="AA:BB:CC:DD:EE:F0",
839+
rssi=-60,
840+
manufacturer_data={},
841+
service_uuids=[IMPROV_BLE_DISCOVERY_INFO.service_uuids[0]],
842+
service_data=IMPROV_BLE_DISCOVERY_INFO.service_data,
843+
source="local",
844+
device=generate_ble_device(address="AA:BB:CC:DD:EE:F0", name="improvtest"),
845+
advertisement=generate_advertisement_data(
846+
service_uuids=IMPROV_BLE_DISCOVERY_INFO.service_uuids,
847+
service_data=IMPROV_BLE_DISCOVERY_INFO.service_data,
848+
),
849+
time=0,
850+
connectable=True,
851+
tx_power=-127,
852+
)
853+
854+
# Capture events to verify frontend notification
855+
events = async_capture_events(hass, "data_entry_flow_progressed")
856+
857+
# Simulate receiving updated advertisement with new name
858+
callback(updated_discovery_info, BluetoothChange.ADVERTISEMENT)
859+
await hass.async_block_till_done()
860+
861+
# Verify title_placeholders were updated
862+
flow = hass.config_entries.flow.async_get(result["flow_id"])
863+
assert flow["context"]["title_placeholders"] == {"name": "improvtest"}
864+
865+
# Verify frontend was notified
866+
assert len(events) == 1
867+
assert events[0].data == {
868+
"handler": DOMAIN,
869+
"flow_id": result["flow_id"],
870+
"refresh": True,
871+
}

0 commit comments

Comments
 (0)