Skip to content

Commit

Permalink
Fix entity rename for unusually named entities (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed Jun 8, 2022
1 parent a4690de commit 5102e6b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
14 changes: 9 additions & 5 deletions custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_MODEL, CONF_HOST, CONF_URL
from homeassistant.core import Config, HomeAssistant, callback
from homeassistant.core import Config, HomeAssistant, callback, valid_entity_id
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand Down Expand Up @@ -265,10 +265,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
f"{cam_name}_{obj_name}",
)
entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
new_id = f"sensor.{cam_name}_{obj_name}_count"
# Verify the new entity_id doesn't already exist.
entry_for_new_id = entity_registry.async_get(new_id)
if entity_id and entity_id != new_id and not entry_for_new_id:
new_id = f"sensor.{slugify(cam_name)}_{slugify(obj_name)}_count"

if (
entity_id
and entity_id != new_id
and valid_entity_id(new_id)
and not entity_registry.async_get(new_id)
):
new_name = f"{get_friendly_name(cam_name)} {obj_name} Count".title()
entity_registry.async_update_entity(
entity_id=entity_id,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/frigate/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"domain": "frigate",
"documentation": "https://github.com/blakeblackshear/frigate",
"name": "Frigate",
"version": "3.0.0-rc.1",
"version": "3.0.0-rc.2",
"issue_tracker": "https://github.com/blakeblackshear/frigate-hass-integration/issues",
"dependencies": [
"http",
Expand Down
46 changes: 46 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test the frigate binary sensor."""
from __future__ import annotations

import copy
import logging
from typing import Any
from unittest.mock import AsyncMock, patch
Expand All @@ -20,6 +21,7 @@
from homeassistant.loader import async_get_integration

from . import (
TEST_CONFIG,
TEST_CONFIG_ENTRY_ID,
create_mock_frigate_client,
create_mock_frigate_config_entry,
Expand Down Expand Up @@ -439,3 +441,47 @@ async def test_entry_remove_old_devices(hass: HomeAssistant) -> None:
config_entry.entry_id, "sensor_object_count", "steps_person"
),
)


async def test_entry_rename_entities_with_unusual_names(hass: HomeAssistant) -> None:
"""Test that non-simple names work."""
# Test for: https://github.com/blakeblackshear/frigate-hass-integration/issues/275

config: dict[str, Any] = copy.deepcopy(TEST_CONFIG)

# Rename one camera.
config["cameras"]["Front-door"] = config["cameras"]["front_door"]
del config["cameras"]["front_door"]

client = create_mock_frigate_client()
client.async_get_config = AsyncMock(return_value=config)

config_entry = create_mock_frigate_config_entry(hass)
unique_id = get_frigate_entity_unique_id(
config_entry.entry_id,
"sensor_object_count",
"Front-door_person",
)

entity_registry = er.async_get(hass)
entity_registry.async_get_or_create(
domain="sensor",
platform=DOMAIN,
unique_id=unique_id,
config_entry=config_entry,
suggested_object_id="front_door_person",
)

# Verify the entity name before we load the config entry.
entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
assert entity_id == "sensor.front_door_person"

# Load the config entry.
config_entry = await setup_mock_frigate_config_entry(
hass, config_entry=config_entry, client=client
)
await hass.async_block_till_done()

# Verify the rename has correctly occurred.
entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
assert entity_id == "sensor.front_door_person_count"

0 comments on commit 5102e6b

Please sign in to comment.