1 """UniFi Protect data migrations."""
3 from __future__
import annotations
5 from itertools
import chain
7 from typing
import TypedDict
9 from uiprotect
import ProtectApiClient
10 from uiprotect.data
import Bootstrap
19 from .const
import DOMAIN
20 from .data
import UFPConfigEntry
22 _LOGGER = logging.getLogger(__name__)
26 """Entity ref parameter variable."""
33 """Entity usages response variable."""
35 automations: dict[str, list[str]]
36 scripts: dict[str, list[str]]
41 hass: HomeAssistant, entry: UFPConfigEntry, entities: dict[str, EntityRef]
42 ) -> dict[str, EntityUsage]:
43 """Check for usages of entities and return them."""
45 entity_registry = er.async_get(hass)
46 refs: dict[str, EntityUsage] = {
47 ref: {
"automations": {},
"scripts": {}}
for ref
in entities
50 for entity
in er.async_entries_for_config_entry(entity_registry, entry.entry_id):
51 for ref_id, ref
in entities.items():
53 entity.domain == ref[
"platform"]
54 and entity.disabled_by
is None
55 and ref[
"id"]
in entity.unique_id
59 if entity_automations:
60 refs[ref_id][
"automations"][entity.entity_id] = entity_automations
62 refs[ref_id][
"scripts"][entity.entity_id] = entity_scripts
70 entry: UFPConfigEntry,
72 entities: dict[str, EntityRef],
74 """Create repairs for used entities that are deprecated."""
77 for ref_id, refs
in usages.items():
78 issue_id = f
"deprecate_{ref_id}"
79 automations = refs[
"automations"]
80 scripts = refs[
"scripts"]
81 if automations
or scripts:
83 set(chain.from_iterable(chain(automations.values(), scripts.values())))
85 ir.async_create_issue(
90 breaks_in_ha_version=breaks_in,
91 severity=IssueSeverity.WARNING,
92 translation_key=issue_id,
93 translation_placeholders={
94 "items":
"* `" +
"`\n* `".join(items) +
"`\n"
98 _LOGGER.debug(
"No found usages of %s", ref_id)
99 ir.async_delete_issue(hass, DOMAIN, issue_id)
104 entry: UFPConfigEntry,
105 protect: ProtectApiClient,
106 bootstrap: Bootstrap,
108 """Run all valid UniFi Protect data migrations."""
110 _LOGGER.debug(
"Start Migrate: async_deprecate_hdr")
112 _LOGGER.debug(
"Completed Migrate: async_deprecate_hdr")
117 """Check for usages of hdr_mode switch and raise repair if it is used.
119 UniFi Protect v3.0.22 changed how HDR works so it is no longer a simple on/off toggle. There is
120 Always On, Always Off and Auto. So it has been migrated to a select. The old switch is now deprecated.
129 {
"hdr_switch": {
"id":
"hdr_mode",
"platform": Platform.SWITCH}},
list[str] automations_with_entity(HomeAssistant hass, str entity_id)
list[str] scripts_with_entity(HomeAssistant hass, str entity_id)
None async_deprecate_hdr(HomeAssistant hass, UFPConfigEntry entry)
dict[str, EntityUsage] check_if_used(HomeAssistant hass, UFPConfigEntry entry, dict[str, EntityRef] entities)
None create_repair_if_used(HomeAssistant hass, UFPConfigEntry entry, str breaks_in, dict[str, EntityRef] entities)
None async_migrate_data(HomeAssistant hass, UFPConfigEntry entry, ProtectApiClient protect, Bootstrap bootstrap)