Skip to content

Commit

Permalink
Fix interaction between v1_v2 migration and the motion sensor rename.
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed May 19, 2022
1 parent 6a727bd commit 9f1bbb1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
new_options.pop(CONF_CAMERA_STATIC_IMAGE_HEIGHT)
hass.config_entries.async_update_entry(entry, options=new_options)

# cleanup object_motion sensors.
# Cleanup object_motion sensors (replaced with occupancy sensors).
for cam_name, obj_name in get_cameras_zones_and_objects(config):
unique_id = get_frigate_entity_unique_id(
entry.entry_id,
Expand Down Expand Up @@ -312,7 +312,7 @@ def update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] | None:

converters: Final[dict[re.Pattern, Callable[[re.Match], list[str]]]] = {
re.compile(rf"^{DOMAIN}_(?P<cam_obj>\S+)_binary_sensor$"): lambda m: [
"motion_sensor",
"occupancy_sensor",
m.group("cam_obj"),
],
re.compile(rf"^{DOMAIN}_(?P<cam>\S+)_camera$"): lambda m: [
Expand Down
39 changes: 17 additions & 22 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async def test_entry_migration_v1_to_v2(hass: HomeAssistant) -> None:

# Ensure all the new transformed entity unique ids are present.
new_unique_ids = [
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:front_door_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:occupancy_sensor:front_door_person"),
("camera", f"{TEST_CONFIG_ENTRY_ID}:camera:front_door"),
("camera", f"{TEST_CONFIG_ENTRY_ID}:camera_snapshots:front_door_person"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_camera"),
Expand All @@ -166,7 +166,7 @@ async def test_entry_migration_v1_to_v2(hass: HomeAssistant) -> None:
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_detector_speed:cpu2"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_detection"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_object_count:steps_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:steps_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:occupancy_sensor:steps_person"),
]
for platform, unique_id in new_unique_ids:
assert (
Expand All @@ -189,7 +189,7 @@ async def test_entry_cleanup_old_clips_switch(hass: HomeAssistant) -> None:
config_entry.add_to_hass(hass)

old_unique_ids = [
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:front_door_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:occupancy_sensor:front_door_person"),
("camera", f"{TEST_CONFIG_ENTRY_ID}:camera:front_door"),
("camera", f"{TEST_CONFIG_ENTRY_ID}:camera_snapshots:front_door_person"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_camera"),
Expand All @@ -204,7 +204,7 @@ async def test_entry_cleanup_old_clips_switch(hass: HomeAssistant) -> None:
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_detector_speed:cpu2"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_detection"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_object_count:steps_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:steps_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:occupancy_sensor:steps_person"),
]

# Create fake entries with the old unique_ids.
Expand Down Expand Up @@ -251,15 +251,16 @@ async def test_entry_cleanup_old_motion_sensor(hass: HomeAssistant) -> None:

config_entry.add_to_hass(hass)

old_unique_ids = [
old_unique_ids = {
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:front_door_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:steps_person"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_camera"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_object_count:front_door_person"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:detection"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_process"),
("sensor", f"{TEST_CONFIG_ENTRY_ID}:sensor_fps:front_door_skipped"),
]
("switch", f"{TEST_CONFIG_ENTRY_ID}:switch:front_door_recordings"),
}

# Create fake entries with the old unique_ids.
for platform, unique_id in old_unique_ids:
Expand All @@ -272,24 +273,18 @@ async def test_entry_cleanup_old_motion_sensor(hass: HomeAssistant) -> None:
hass, config_entry=config_entry
)

for platform, unique_id in old_unique_ids:
if platform == "binary_sensor" and unique_id.endswith("_person"):
assert (
entity_registry.async_get_entity_id("motion_sensor", DOMAIN, unique_id)
is None
)
else:
assert (
entity_registry.async_get_entity_id(platform, DOMAIN, unique_id)
is not None
)
removed_unique_ids = {
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:front_door_person"),
("binary_sensor", f"{TEST_CONFIG_ENTRY_ID}:motion_sensor:steps_person"),
}

assert (
entity_registry.async_get_entity_id(
"switch", DOMAIN, f"{TEST_CONFIG_ENTRY_ID}:switch:front_door_recordings"
for platform, unique_id in removed_unique_ids:
assert entity_registry.async_get_entity_id(platform, DOMAIN, unique_id) is None

for platform, unique_id in old_unique_ids - removed_unique_ids:
assert (
entity_registry.async_get_entity_id(platform, DOMAIN, unique_id) is not None
)
is not None
)


async def test_startup_message(caplog: Any, hass: HomeAssistant) -> None:
Expand Down

0 comments on commit 9f1bbb1

Please sign in to comment.