|
23 | 23 | ) |
24 | 24 |
|
25 | 25 | from tests.common import MockConfigEntry |
| 26 | +from tests.components.bluetooth import inject_bluetooth_service_info_bleak |
26 | 27 |
|
27 | 28 | IMPROV_BLE = "homeassistant.components.improv_ble" |
28 | 29 |
|
@@ -179,6 +180,112 @@ async def test_bluetooth_step_provisioned_device_2(hass: HomeAssistant) -> None: |
179 | 180 | assert len(hass.config_entries.flow.async_progress_by_handler("improv_ble")) == 0 |
180 | 181 |
|
181 | 182 |
|
| 183 | +async def test_bluetooth_step_provisioned_no_rediscovery(hass: HomeAssistant) -> None: |
| 184 | + """Test that provisioned device is not rediscovered while it stays provisioned.""" |
| 185 | + # Step 1: Inject provisioned device advertisement (triggers discovery, aborts) |
| 186 | + inject_bluetooth_service_info_bleak(hass, PROVISIONED_IMPROV_BLE_DISCOVERY_INFO) |
| 187 | + await hass.async_block_till_done() |
| 188 | + |
| 189 | + # Verify flow was aborted |
| 190 | + flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN) |
| 191 | + assert len(flows) == 0 |
| 192 | + |
| 193 | + # Step 2: Inject same provisioned advertisement again |
| 194 | + # This should NOT trigger a new discovery because the content hasn't changed |
| 195 | + # even though we cleared the match history |
| 196 | + inject_bluetooth_service_info_bleak(hass, PROVISIONED_IMPROV_BLE_DISCOVERY_INFO) |
| 197 | + await hass.async_block_till_done() |
| 198 | + |
| 199 | + # Verify no new flow was started |
| 200 | + flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN) |
| 201 | + assert len(flows) == 0 |
| 202 | + |
| 203 | + |
| 204 | +async def test_bluetooth_step_factory_reset_rediscovery(hass: HomeAssistant) -> None: |
| 205 | + """Test that factory reset device can be rediscovered.""" |
| 206 | + # Start a flow manually with provisioned device to ensure improv_ble is loaded |
| 207 | + result = await hass.config_entries.flow.async_init( |
| 208 | + DOMAIN, |
| 209 | + context={"source": config_entries.SOURCE_BLUETOOTH}, |
| 210 | + data=PROVISIONED_IMPROV_BLE_DISCOVERY_INFO, |
| 211 | + ) |
| 212 | + assert result["type"] is FlowResultType.ABORT |
| 213 | + assert result["reason"] == "already_provisioned" |
| 214 | + |
| 215 | + # Now the match history has been cleared by the config flow |
| 216 | + # Inject authorized device advertisement - should trigger new discovery |
| 217 | + inject_bluetooth_service_info_bleak(hass, IMPROV_BLE_DISCOVERY_INFO) |
| 218 | + await hass.async_block_till_done() |
| 219 | + |
| 220 | + # Verify discovery proceeds (new flow started) |
| 221 | + flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN) |
| 222 | + assert len(flows) == 1 |
| 223 | + assert flows[0]["step_id"] == "bluetooth_confirm" |
| 224 | + |
| 225 | + |
| 226 | +async def test_bluetooth_rediscovery_after_successful_provision( |
| 227 | + hass: HomeAssistant, |
| 228 | +) -> None: |
| 229 | + """Test that device can be rediscovered after successful provisioning.""" |
| 230 | + # Start provisioning flow |
| 231 | + result = await hass.config_entries.flow.async_init( |
| 232 | + DOMAIN, |
| 233 | + context={"source": config_entries.SOURCE_BLUETOOTH}, |
| 234 | + data=IMPROV_BLE_DISCOVERY_INFO, |
| 235 | + ) |
| 236 | + assert result["type"] is FlowResultType.FORM |
| 237 | + assert result["step_id"] == "bluetooth_confirm" |
| 238 | + |
| 239 | + # Confirm bluetooth setup |
| 240 | + result = await hass.config_entries.flow.async_configure(result["flow_id"]) |
| 241 | + assert result["type"] is FlowResultType.FORM |
| 242 | + assert result["step_id"] == "bluetooth_confirm" |
| 243 | + |
| 244 | + # Start provisioning |
| 245 | + with patch( |
| 246 | + f"{IMPROV_BLE}.config_flow.ImprovBLEClient.can_identify", return_value=False |
| 247 | + ): |
| 248 | + result = await hass.config_entries.flow.async_configure( |
| 249 | + result["flow_id"], |
| 250 | + {CONF_ADDRESS: IMPROV_BLE_DISCOVERY_INFO.address}, |
| 251 | + ) |
| 252 | + assert result["type"] is FlowResultType.FORM |
| 253 | + assert result["step_id"] == "provision" |
| 254 | + |
| 255 | + # Complete provisioning successfully |
| 256 | + with ( |
| 257 | + patch( |
| 258 | + f"{IMPROV_BLE}.config_flow.ImprovBLEClient.need_authorization", |
| 259 | + return_value=False, |
| 260 | + ), |
| 261 | + patch( |
| 262 | + f"{IMPROV_BLE}.config_flow.ImprovBLEClient.provision", |
| 263 | + return_value=None, |
| 264 | + ), |
| 265 | + ): |
| 266 | + result = await hass.config_entries.flow.async_configure( |
| 267 | + result["flow_id"], {"ssid": "TestNetwork", "password": "secret"} |
| 268 | + ) |
| 269 | + assert result["type"] is FlowResultType.SHOW_PROGRESS |
| 270 | + assert result["progress_action"] == "provisioning" |
| 271 | + assert result["step_id"] == "do_provision" |
| 272 | + await hass.async_block_till_done() |
| 273 | + |
| 274 | + result = await hass.config_entries.flow.async_configure(result["flow_id"]) |
| 275 | + assert result["type"] is FlowResultType.ABORT |
| 276 | + assert result["reason"] == "provision_successful" |
| 277 | + |
| 278 | + # Now inject the same device again (simulating factory reset) |
| 279 | + # The match history was cleared after successful provision, so it should be rediscovered |
| 280 | + inject_bluetooth_service_info_bleak(hass, IMPROV_BLE_DISCOVERY_INFO) |
| 281 | + await hass.async_block_till_done() |
| 282 | + |
| 283 | + # Verify new discovery flow was created |
| 284 | + flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN) |
| 285 | + assert len(flows) == 1 |
| 286 | + assert flows[0]["step_id"] == "bluetooth_confirm" |
| 287 | + |
| 288 | + |
182 | 289 | async def test_bluetooth_step_success(hass: HomeAssistant) -> None: |
183 | 290 | """Test bluetooth step success path.""" |
184 | 291 | result = await hass.config_entries.flow.async_init( |
|
0 commit comments