Allow configuring ignored devices from improve_ble user flow (#140595)

pull/140600/head
Erik Montnemery 2025-03-14 15:05:58 +01:00 committed by GitHub
parent 96a6d88dca
commit 08fc6dcff6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 5 deletions

View File

@ -83,12 +83,9 @@ class ImprovBLEConfigFlow(ConfigFlow, domain=DOMAIN):
self._discovery_info = self._discovered_devices[address]
return await self.async_step_start_improv()
current_addresses = self._async_current_ids()
for discovery in bluetooth.async_discovered_service_info(self.hass):
if (
discovery.address in current_addresses
or discovery.address in self._discovered_devices
or not device_filter(discovery.advertisement)
if discovery.address in self._discovered_devices or not device_filter(
discovery.advertisement
):
continue
self._discovered_devices[discovery.address] = discovery
@ -364,6 +361,18 @@ class ImprovBLEConfigFlow(ConfigFlow, domain=DOMAIN):
assert self._provision_result is not None
result = self._provision_result
if result["type"] == "abort" and result["reason"] in (
"provision_successful",
"provision_successful_url",
):
# Delete ignored config entry, if it exists
address = self.context["unique_id"]
current_entries = self._async_current_entries(include_ignore=True)
for entry in current_entries:
if entry.unique_id == address:
_LOGGER.debug("Removing ignored entry: %s", entry)
await self.hass.config_entries.async_remove(entry.entry_id)
break
self._provision_result = None
return result

View File

@ -10,6 +10,7 @@ import pytest
from homeassistant import config_entries
from homeassistant.components.bluetooth import BluetoothChange
from homeassistant.components.improv_ble.const import DOMAIN
from homeassistant.config_entries import SOURCE_IGNORE
from homeassistant.const import CONF_ADDRESS
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult, FlowResultType
@ -21,6 +22,8 @@ from . import (
PROVISIONED_IMPROV_BLE_DISCOVERY_INFO,
)
from tests.common import MockConfigEntry
IMPROV_BLE = "homeassistant.components.improv_ble"
@ -118,6 +121,32 @@ async def test_async_step_user_takes_precedence_over_discovery(
assert not hass.config_entries.flow.async_progress(DOMAIN)
async def test_user_setup_removes_ignored_entry(hass: HomeAssistant) -> None:
"""Test the user initiated form can replace an ignored device."""
ignored_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=IMPROV_BLE_DISCOVERY_INFO.address,
source=SOURCE_IGNORE,
)
ignored_entry.add_to_hass(hass)
with patch(
f"{IMPROV_BLE}.config_flow.bluetooth.async_discovered_service_info",
return_value=[NOT_IMPROV_BLE_DISCOVERY_INFO, IMPROV_BLE_DISCOVERY_INFO],
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
await _test_common_success_wo_identify(
hass, result, IMPROV_BLE_DISCOVERY_INFO.address
)
# Check the ignored entry is removed
assert not hass.config_entries.async_entries(DOMAIN)
async def test_bluetooth_step_provisioned_device(hass: HomeAssistant) -> None:
"""Test bluetooth step when device is already provisioned."""
result = await hass.config_entries.flow.async_init(