Do not try to cleanup invalid config entries without an AccessoryPairingID (#108830)
This commit is contained in:
parent
da7d2ef228
commit
82d21136bd
2 changed files with 48 additions and 4 deletions
|
@ -272,8 +272,14 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
# Device isn't paired with us or anyone else.
|
# Device isn't paired with us or anyone else.
|
||||||
# But we have a 'complete' config entry for it - that is probably
|
# But we have a 'complete' config entry for it - that is probably
|
||||||
# invalid. Remove it automatically.
|
# invalid. Remove it automatically if it has an accessory pairing id
|
||||||
if not paired and existing_entry:
|
# (which means it was paired with us at some point) and was not
|
||||||
|
# ignored by the user.
|
||||||
|
if (
|
||||||
|
not paired
|
||||||
|
and existing_entry
|
||||||
|
and (accessory_pairing_id := existing_entry.data.get("AccessoryPairingID"))
|
||||||
|
):
|
||||||
if self.controller is None:
|
if self.controller is None:
|
||||||
await self._async_setup_controller()
|
await self._async_setup_controller()
|
||||||
|
|
||||||
|
@ -282,7 +288,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
assert self.controller
|
assert self.controller
|
||||||
|
|
||||||
pairing = self.controller.load_pairing(
|
pairing = self.controller.load_pairing(
|
||||||
existing_entry.data["AccessoryPairingID"], dict(existing_entry.data)
|
accessory_pairing_id, dict(existing_entry.data)
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -12,7 +12,7 @@ from aiohomekit.model.services import ServicesTypes
|
||||||
from bleak.exc import BleakError
|
from bleak.exc import BleakError
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries, data_entry_flow
|
||||||
from homeassistant.components import zeroconf
|
from homeassistant.components import zeroconf
|
||||||
from homeassistant.components.homekit_controller import config_flow
|
from homeassistant.components.homekit_controller import config_flow
|
||||||
from homeassistant.components.homekit_controller.const import KNOWN_DEVICES
|
from homeassistant.components.homekit_controller.const import KNOWN_DEVICES
|
||||||
|
@ -485,6 +485,44 @@ async def test_discovery_invalid_config_entry(hass: HomeAssistant, controller) -
|
||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_discovery_ignored_config_entry(hass: HomeAssistant, controller) -> None:
|
||||||
|
"""There is already a config entry but it is ignored."""
|
||||||
|
pairing = await controller.add_paired_device(Accessories(), "00:00:00:00:00:00")
|
||||||
|
|
||||||
|
MockConfigEntry(
|
||||||
|
domain="homekit_controller",
|
||||||
|
data={},
|
||||||
|
unique_id="00:00:00:00:00:00",
|
||||||
|
source=config_entries.SOURCE_IGNORE,
|
||||||
|
).add_to_hass(hass)
|
||||||
|
|
||||||
|
# We just added a mock config entry so it must be visible in hass
|
||||||
|
assert len(hass.config_entries.async_entries()) == 1
|
||||||
|
|
||||||
|
device = setup_mock_accessory(controller)
|
||||||
|
discovery_info = get_device_discovery_info(device)
|
||||||
|
|
||||||
|
# Device is discovered
|
||||||
|
with patch.object(
|
||||||
|
pairing,
|
||||||
|
"list_accessories_and_characteristics",
|
||||||
|
side_effect=AuthenticationError("Invalid pairing keys"),
|
||||||
|
):
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
"homekit_controller",
|
||||||
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
|
data=discovery_info,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Entry is still ignored
|
||||||
|
config_entry_count = len(hass.config_entries.async_entries())
|
||||||
|
assert config_entry_count == 1
|
||||||
|
|
||||||
|
# We should abort since there is no accessory id in the data
|
||||||
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
async def test_discovery_already_configured(hass: HomeAssistant, controller) -> None:
|
async def test_discovery_already_configured(hass: HomeAssistant, controller) -> None:
|
||||||
"""Already configured."""
|
"""Already configured."""
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
|
|
Loading…
Add table
Reference in a new issue