|
8 | 8 | import pytest |
9 | 9 |
|
10 | 10 | from homeassistant import config_entries |
11 | | -from homeassistant.components.bluetooth import BluetoothChange |
| 11 | +from homeassistant.components.bluetooth import ( |
| 12 | + BluetoothChange, |
| 13 | + BluetoothServiceInfoBleak, |
| 14 | +) |
12 | 15 | from homeassistant.components.improv_ble.const import DOMAIN |
13 | 16 | from homeassistant.config_entries import SOURCE_IGNORE |
14 | 17 | from homeassistant.const import CONF_ADDRESS |
|
22 | 25 | PROVISIONED_IMPROV_BLE_DISCOVERY_INFO, |
23 | 26 | ) |
24 | 27 |
|
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 | +) |
27 | 34 |
|
28 | 35 | IMPROV_BLE = "homeassistant.components.improv_ble" |
29 | 36 |
|
@@ -803,3 +810,62 @@ async def test_provision_fails_invalid_data( |
803 | 810 | "Received invalid improv via BLE data '000000000000' from device with bluetooth address 'AA:BB:CC:DD:EE:F0'" |
804 | 811 | in caplog.text |
805 | 812 | ) |
| 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