From 5102e6bceaee46ad842063be9dfc1eb3b267df59 Mon Sep 17 00:00:00 2001 From: Dermot Duffy Date: Tue, 7 Jun 2022 19:42:34 -0700 Subject: [PATCH] Fix entity rename for unusually named entities (#276) --- custom_components/frigate/__init__.py | 14 +++++--- custom_components/frigate/manifest.json | 2 +- tests/test_init.py | 46 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/custom_components/frigate/__init__.py b/custom_components/frigate/__init__.py index 11c2c0b1..6f8b8611 100644 --- a/custom_components/frigate/__init__.py +++ b/custom_components/frigate/__init__.py @@ -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 @@ -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, diff --git a/custom_components/frigate/manifest.json b/custom_components/frigate/manifest.json index 823f76f0..8bfc5335 100644 --- a/custom_components/frigate/manifest.json +++ b/custom_components/frigate/manifest.json @@ -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", diff --git a/tests/test_init.py b/tests/test_init.py index 83738a2b..243659dd 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -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 @@ -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, @@ -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"