mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
Raise the UniFi Protect migration repairs once startup is done (#186171)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""UniFi Protect data migrations."""
|
||||
|
||||
import logging
|
||||
from typing import NamedTuple
|
||||
|
||||
from uiprotect.data import Bootstrap
|
||||
|
||||
@@ -14,6 +15,7 @@ from homeassistant.helpers import (
|
||||
issue_registry as ir,
|
||||
)
|
||||
from homeassistant.helpers.issue_registry import IssueSeverity
|
||||
from homeassistant.helpers.start import async_at_started
|
||||
|
||||
from .const import DOMAIN
|
||||
from .data import UFPConfigEntry
|
||||
@@ -87,6 +89,8 @@ def async_migrate_insecure_cameras(hass: HomeAssistant, entry: UFPConfigEntry) -
|
||||
Added in 2026.7.0
|
||||
"""
|
||||
registry = er.async_get(hass)
|
||||
# The rename must land before the camera platform sets up. Only the removal waits.
|
||||
removals: list[_Removal] = []
|
||||
for entity in er.async_entries_for_config_entry(registry, entry.entry_id):
|
||||
if entity.domain != Platform.CAMERA or not entity.unique_id.endswith(
|
||||
"_insecure"
|
||||
@@ -101,14 +105,16 @@ def async_migrate_insecure_cameras(hass: HomeAssistant, entry: UFPConfigEntry) -
|
||||
entity.entity_id, new_unique_id=secure_unique_id
|
||||
)
|
||||
continue
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"insecure_camera_removed_{entity.unique_id}",
|
||||
"insecure_camera_removed",
|
||||
{"replacement": secure_entity_id},
|
||||
removals.append(
|
||||
_Removal(
|
||||
entity.domain,
|
||||
entity.unique_id,
|
||||
f"insecure_camera_removed_{entity.unique_id}",
|
||||
"insecure_camera_removed",
|
||||
{"replacement": secure_entity_id},
|
||||
)
|
||||
)
|
||||
registry.async_remove(entity.entity_id)
|
||||
_async_remove_when_started(hass, entry, removals)
|
||||
|
||||
|
||||
@callback
|
||||
@@ -156,6 +162,51 @@ def _async_repair_if_used(
|
||||
)
|
||||
|
||||
|
||||
class _Removal(NamedTuple):
|
||||
"""An entity to remove once startup is done, with the repair if it is used."""
|
||||
|
||||
domain: str
|
||||
unique_id: str
|
||||
issue_id: str
|
||||
translation_key: str
|
||||
placeholders: dict[str, str] | None = None
|
||||
|
||||
|
||||
@callback
|
||||
def _async_remove_when_started(
|
||||
hass: HomeAssistant,
|
||||
entry: UFPConfigEntry,
|
||||
removals: list[_Removal],
|
||||
) -> None:
|
||||
"""Remove entities once startup is done, raising a repair for used ones.
|
||||
|
||||
Before the automations are loaded every entity looks unused, so removing
|
||||
earlier would drop the repair for good.
|
||||
"""
|
||||
if not removals:
|
||||
return
|
||||
|
||||
@callback
|
||||
def _async_remove(_hass: HomeAssistant) -> None:
|
||||
registry = er.async_get(hass)
|
||||
for removal in removals:
|
||||
entity_id = registry.async_get_entity_id(
|
||||
removal.domain, DOMAIN, removal.unique_id
|
||||
)
|
||||
if entity_id is None or (entity := registry.async_get(entity_id)) is None:
|
||||
continue
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
removal.issue_id,
|
||||
removal.translation_key,
|
||||
removal.placeholders,
|
||||
)
|
||||
registry.async_remove(entity_id)
|
||||
|
||||
entry.async_on_unload(async_at_started(hass, _async_remove))
|
||||
|
||||
|
||||
@callback
|
||||
def async_remove_package_binary_sensor(
|
||||
hass: HomeAssistant, entry: UFPConfigEntry
|
||||
@@ -171,18 +222,21 @@ def async_remove_package_binary_sensor(
|
||||
Added in 2026.7.0
|
||||
"""
|
||||
registry = er.async_get(hass)
|
||||
removals: list[_Removal] = []
|
||||
for entity in er.async_entries_for_config_entry(registry, entry.entry_id):
|
||||
if entity.domain != Platform.BINARY_SENSOR or not entity.unique_id.endswith(
|
||||
"_smart_obj_package"
|
||||
):
|
||||
continue
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"package_binary_sensor_removed_{entity.unique_id}",
|
||||
"package_binary_sensor_removed",
|
||||
removals.append(
|
||||
_Removal(
|
||||
entity.domain,
|
||||
entity.unique_id,
|
||||
f"package_binary_sensor_removed_{entity.unique_id}",
|
||||
"package_binary_sensor_removed",
|
||||
)
|
||||
)
|
||||
registry.async_remove(entity.entity_id)
|
||||
_async_remove_when_started(hass, entry, removals)
|
||||
|
||||
|
||||
@callback
|
||||
@@ -199,18 +253,21 @@ def async_remove_hdr_switch(hass: HomeAssistant, entry: UFPConfigEntry) -> None:
|
||||
# Drop the stored record of the old deprecation repair.
|
||||
ir.async_delete_issue(hass, DOMAIN, "deprecate_hdr_switch")
|
||||
registry = er.async_get(hass)
|
||||
removals: list[_Removal] = []
|
||||
for entity in er.async_entries_for_config_entry(registry, entry.entry_id):
|
||||
if entity.domain != Platform.SWITCH or not entity.unique_id.endswith(
|
||||
"_hdr_mode"
|
||||
):
|
||||
continue
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"hdr_switch_removed_{entity.unique_id}",
|
||||
"hdr_switch_removed",
|
||||
removals.append(
|
||||
_Removal(
|
||||
entity.domain,
|
||||
entity.unique_id,
|
||||
f"hdr_switch_removed_{entity.unique_id}",
|
||||
"hdr_switch_removed",
|
||||
)
|
||||
)
|
||||
registry.async_remove(entity.entity_id)
|
||||
_async_remove_when_started(hass, entry, removals)
|
||||
|
||||
|
||||
@callback
|
||||
@@ -272,6 +329,7 @@ def async_remove_sense_setting_mirrors(
|
||||
if not sensor_macs:
|
||||
return
|
||||
registry = er.async_get(hass)
|
||||
removals: list[_Removal] = []
|
||||
for entity in er.async_entries_for_config_entry(registry, entry.entry_id):
|
||||
mac, _, key = entity.unique_id.partition("_")
|
||||
replacement = _SENSE_SETTING_MIRRORS.get((entity.domain, key))
|
||||
@@ -281,24 +339,29 @@ def async_remove_sense_setting_mirrors(
|
||||
hass, DOMAIN, f"sense_setting_mirror_deprecated_{entity.unique_id}"
|
||||
)
|
||||
replacement_platform, replacement_key = replacement
|
||||
issue_id = f"sense_setting_mirror_removed_{entity.unique_id}"
|
||||
if replacement_entity_id := registry.async_get_entity_id(
|
||||
replacement_platform, DOMAIN, f"{mac}_{replacement_key}"
|
||||
):
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"sense_setting_mirror_removed_{entity.unique_id}",
|
||||
"sense_setting_mirror_removed",
|
||||
{"replacement": replacement_entity_id},
|
||||
removals.append(
|
||||
_Removal(
|
||||
entity.domain,
|
||||
entity.unique_id,
|
||||
issue_id,
|
||||
"sense_setting_mirror_removed",
|
||||
{"replacement": replacement_entity_id},
|
||||
)
|
||||
)
|
||||
else:
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"sense_setting_mirror_removed_{entity.unique_id}",
|
||||
"sense_setting_mirror_removed_no_replacement",
|
||||
removals.append(
|
||||
_Removal(
|
||||
entity.domain,
|
||||
entity.unique_id,
|
||||
issue_id,
|
||||
"sense_setting_mirror_removed_no_replacement",
|
||||
)
|
||||
)
|
||||
registry.async_remove(entity.entity_id)
|
||||
_async_remove_when_started(hass, entry, removals)
|
||||
|
||||
|
||||
# Release that removes the deprecated light mirrors.
|
||||
@@ -366,30 +429,35 @@ def _async_deprecate_setting_mirrors(
|
||||
"""Raise a repair for each used mirror, pointing at its replacement.
|
||||
|
||||
The mirror keys are shared across device types, so the match is scoped to
|
||||
``macs``.
|
||||
``macs``. Waits for startup to finish, like ``_async_remove_when_started``.
|
||||
"""
|
||||
if not macs:
|
||||
return
|
||||
registry = er.async_get(hass)
|
||||
for entity in er.async_entries_for_config_entry(registry, entry.entry_id):
|
||||
mac, _, key = entity.unique_id.partition("_")
|
||||
mirror = (entity.domain, key)
|
||||
if mirror not in replacements or mac not in macs:
|
||||
continue
|
||||
platform, replacement_key, translation_key = replacements[mirror]
|
||||
replacement_id = registry.async_get_entity_id(
|
||||
platform,
|
||||
DOMAIN,
|
||||
mac if replacement_key is None else f"{mac}_{replacement_key}",
|
||||
)
|
||||
# Nothing to point the repair at before the replacement exists.
|
||||
if replacement_id is None:
|
||||
continue
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"setting_mirror_deprecated_{entity.unique_id}",
|
||||
translation_key,
|
||||
{"replacement": replacement_id},
|
||||
breaks_in=breaks_in,
|
||||
)
|
||||
|
||||
@callback
|
||||
def _async_deprecate(_hass: HomeAssistant) -> None:
|
||||
registry = er.async_get(hass)
|
||||
for entity in er.async_entries_for_config_entry(registry, entry.entry_id):
|
||||
mac, _, key = entity.unique_id.partition("_")
|
||||
mirror = (entity.domain, key)
|
||||
if mirror not in replacements or mac not in macs:
|
||||
continue
|
||||
platform, replacement_key, translation_key = replacements[mirror]
|
||||
replacement_id = registry.async_get_entity_id(
|
||||
platform,
|
||||
DOMAIN,
|
||||
mac if replacement_key is None else f"{mac}_{replacement_key}",
|
||||
)
|
||||
# Nothing to point the repair at before the replacement exists.
|
||||
if replacement_id is None:
|
||||
continue
|
||||
_async_repair_if_used(
|
||||
hass,
|
||||
entity,
|
||||
f"setting_mirror_deprecated_{entity.unique_id}",
|
||||
translation_key,
|
||||
{"replacement": replacement_id},
|
||||
breaks_in=breaks_in,
|
||||
)
|
||||
|
||||
entry.async_on_unload(async_at_started(hass, _async_deprecate))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Test the UniFi Protect setup flow."""
|
||||
|
||||
from collections.abc import Callable, Coroutine
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
@@ -16,8 +17,8 @@ from homeassistant.components.unifiprotect.migrate import (
|
||||
async_remove_hdr_switch,
|
||||
async_remove_sense_setting_mirrors,
|
||||
)
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, Platform
|
||||
from homeassistant.core import CoreState, HomeAssistant
|
||||
from homeassistant.helpers import (
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
@@ -684,6 +685,204 @@ async def test_migrate_light_setting_mirror_repair_clears_when_unused(
|
||||
assert issue_registry.async_get_issue(DOMAIN, issue_id) is None
|
||||
|
||||
|
||||
async def test_migrate_light_setting_mirror_waits_for_start(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ufp: MockUFPFixture,
|
||||
light: Light,
|
||||
) -> None:
|
||||
"""Automations loaded after the entry still get the repair."""
|
||||
setup_public_light(ufp)
|
||||
mirror = entity_registry.async_get_or_create(
|
||||
Platform.BINARY_SENSOR, DOMAIN, f"{light.mac}_light", config_entry=ufp.entry
|
||||
)
|
||||
hass.set_state(CoreState.starting)
|
||||
|
||||
await init_entry(hass, ufp, [light], regenerate_ids=False)
|
||||
await _load_automation(hass, mirror.entity_id)
|
||||
assert not issue_registry.issues
|
||||
|
||||
hass.set_state(CoreState.running)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert issue_registry.async_get_issue(
|
||||
DOMAIN, f"setting_mirror_deprecated_{light.mac}_light"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("platform", "key", "issue"),
|
||||
[
|
||||
*REMOVED_ENTITIES,
|
||||
pytest.param(
|
||||
Platform.CAMERA,
|
||||
"0_insecure",
|
||||
"insecure_camera_removed",
|
||||
id="insecure_camera",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_migrate_removed_entity_waits_for_start(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ufp: MockUFPFixture,
|
||||
doorbell: Camera,
|
||||
platform: Platform,
|
||||
key: str,
|
||||
issue: str,
|
||||
) -> None:
|
||||
"""A used entity is only removed, with its repair, once startup is done."""
|
||||
# The insecure camera is only removed next to its secure counterpart. The
|
||||
# other entities ignore it.
|
||||
entity_registry.async_get_or_create(
|
||||
Platform.CAMERA, DOMAIN, f"{doorbell.mac}_0", config_entry=ufp.entry
|
||||
)
|
||||
entity = entity_registry.async_get_or_create(
|
||||
platform, DOMAIN, f"{doorbell.mac}_{key}", config_entry=ufp.entry
|
||||
)
|
||||
hass.set_state(CoreState.starting)
|
||||
|
||||
await init_entry(hass, ufp, [doorbell], regenerate_ids=False)
|
||||
await _load_automation(hass, entity.entity_id)
|
||||
assert entity_registry.async_get(entity.entity_id) is not None
|
||||
assert not issue_registry.issues
|
||||
|
||||
hass.set_state(CoreState.running)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entity_registry.async_get(entity.entity_id) is None
|
||||
assert issue_registry.async_get_issue(DOMAIN, f"{issue}_{doorbell.mac}_{key}")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("platform", "device", "key"),
|
||||
[
|
||||
pytest.param(Platform.SWITCH, "doorbell", "hdr_mode", id="removal"),
|
||||
pytest.param(Platform.BINARY_SENSOR, "light", "light", id="deprecation"),
|
||||
],
|
||||
)
|
||||
async def test_migrate_unload_before_start(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ufp: MockUFPFixture,
|
||||
doorbell: Camera,
|
||||
light: Light,
|
||||
request: pytest.FixtureRequest,
|
||||
platform: Platform,
|
||||
device: str,
|
||||
key: str,
|
||||
) -> None:
|
||||
"""An entry unloaded before startup finishes leaves its entities alone."""
|
||||
setup_public_light(ufp)
|
||||
mac = request.getfixturevalue(device).mac
|
||||
entity = entity_registry.async_get_or_create(
|
||||
platform, DOMAIN, f"{mac}_{key}", config_entry=ufp.entry
|
||||
)
|
||||
await _load_automation(hass, entity.entity_id)
|
||||
hass.set_state(CoreState.starting)
|
||||
await init_entry(hass, ufp, [doorbell, light], regenerate_ids=False)
|
||||
|
||||
await hass.config_entries.async_unload(ufp.entry.entry_id)
|
||||
hass.set_state(CoreState.running)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entity_registry.async_get(entity.entity_id) is not None
|
||||
assert not issue_registry.issues
|
||||
|
||||
|
||||
async def test_migrate_removed_entity_deleted_before_start(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ufp: MockUFPFixture,
|
||||
doorbell: Camera,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""An entity the user deletes during startup is skipped."""
|
||||
entity = entity_registry.async_get_or_create(
|
||||
Platform.SWITCH, DOMAIN, f"{doorbell.mac}_hdr_mode", config_entry=ufp.entry
|
||||
)
|
||||
await _load_automation(hass, entity.entity_id)
|
||||
hass.set_state(CoreState.starting)
|
||||
await init_entry(hass, ufp, [doorbell], regenerate_ids=False)
|
||||
|
||||
entity_registry.async_remove(entity.entity_id)
|
||||
hass.set_state(CoreState.running)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not issue_registry.issues
|
||||
# A failing STARTED listener is only logged, not raised.
|
||||
assert not [r for r in caplog.records if r.levelno >= logging.ERROR]
|
||||
|
||||
|
||||
async def test_migrate_removed_entity_renamed_before_start(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ufp: MockUFPFixture,
|
||||
doorbell: Camera,
|
||||
) -> None:
|
||||
"""An entity renamed during startup is still removed with its repair."""
|
||||
entity = entity_registry.async_get_or_create(
|
||||
Platform.SWITCH, DOMAIN, f"{doorbell.mac}_hdr_mode", config_entry=ufp.entry
|
||||
)
|
||||
hass.set_state(CoreState.starting)
|
||||
await init_entry(hass, ufp, [doorbell], regenerate_ids=False)
|
||||
|
||||
renamed = entity_registry.async_update_entity(
|
||||
entity.entity_id, new_entity_id="switch.renamed_hdr"
|
||||
)
|
||||
await _load_automation(hass, renamed.entity_id)
|
||||
hass.set_state(CoreState.running)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entity_registry.async_get(renamed.entity_id) is None
|
||||
issue = issue_registry.async_get_issue(
|
||||
DOMAIN, f"hdr_switch_removed_{doorbell.mac}_hdr_mode"
|
||||
)
|
||||
assert issue is not None
|
||||
assert issue.translation_placeholders["entity_id"] == renamed.entity_id
|
||||
|
||||
|
||||
async def test_migrate_sense_setting_mirror_waits_for_start(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
issue_registry: ir.IssueRegistry,
|
||||
ufp: MockUFPFixture,
|
||||
sensor_all: Sensor,
|
||||
) -> None:
|
||||
"""A used sense mirror is only removed, with its repair, once startup is done."""
|
||||
setup_public_sensor(ufp)
|
||||
mirror = entity_registry.async_get_or_create(
|
||||
Platform.BINARY_SENSOR,
|
||||
DOMAIN,
|
||||
f"{sensor_all.mac}_alarm",
|
||||
config_entry=ufp.entry,
|
||||
)
|
||||
hass.set_state(CoreState.starting)
|
||||
|
||||
await init_entry(hass, ufp, [sensor_all], regenerate_ids=False)
|
||||
await _load_automation(hass, mirror.entity_id)
|
||||
assert entity_registry.async_get(mirror.entity_id) is not None
|
||||
|
||||
hass.set_state(CoreState.running)
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entity_registry.async_get(mirror.entity_id) is None
|
||||
assert issue_registry.async_get_issue(
|
||||
DOMAIN, f"sense_setting_mirror_removed_{mirror.unique_id}"
|
||||
)
|
||||
|
||||
|
||||
async def test_migrate_light_setting_mirror_without_replacement(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
|
||||
Reference in New Issue
Block a user