From f75249b18ec320d27cbd78cb3f84e2f372d24533 Mon Sep 17 00:00:00 2001 From: Mattias Arrelid Date: Fri, 23 Oct 2026 14:52:58 +0200 Subject: [PATCH] Deprecate the DoorBird last ring and last motion cameras (#187630) Co-authored-by: Claude Fable 5.1 --- homeassistant/components/doorbird/camera.py | 62 +++-- .../components/doorbird/deprecation.py | 95 +++++++ homeassistant/components/doorbird/device.py | 16 ++ homeassistant/components/doorbird/image.py | 27 +- .../components/doorbird/manifest.json | 1 + .../components/doorbird/strings.json | 16 ++ tests/components/doorbird/test_camera.py | 251 +++++++++++++++++- tests/components/doorbird/test_view.py | 12 +- 8 files changed, 418 insertions(+), 62 deletions(-) create mode 100644 homeassistant/components/doorbird/deprecation.py diff --git a/homeassistant/components/doorbird/camera.py b/homeassistant/components/doorbird/camera.py index faa275eef5a6..236b6796f8fb 100644 --- a/homeassistant/components/doorbird/camera.py +++ b/homeassistant/components/doorbird/camera.py @@ -7,12 +7,16 @@ from typing import override import aiohttp from homeassistant.components.camera import Camera, CameraEntityFeature +from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util import dt as dt_util +from .deprecation import async_should_create_deprecated_entity from .entity import DoorBirdEntity from .models import DoorBirdConfigEntry, DoorBirdData +from .util import get_mac_address_from_door_station_info _LAST_VISITOR_INTERVAL = dt.timedelta(minutes=2) _LAST_MOTION_INTERVAL = dt.timedelta(seconds=30) @@ -29,30 +33,42 @@ async def async_setup_entry( """Set up the DoorBird camera platform.""" door_bird_data = config_entry.runtime_data device = door_bird_data.door_station.device + mac_addr = get_mac_address_from_door_station_info(door_bird_data.door_station_info) + entity_registry = er.async_get(hass) - async_add_entities( - [ - DoorBirdCamera( - door_bird_data, - device.live_image_url, - "live", - _LIVE_INTERVAL, - device.rtsp_live_video_url, - ), - DoorBirdCamera( - door_bird_data, - device.history_image_url(1, "doorbell"), - "last_ring", - _LAST_VISITOR_INTERVAL, - ), - DoorBirdCamera( - door_bird_data, - device.history_image_url(1, "motionsensor"), - "last_motion", - _LAST_MOTION_INTERVAL, - ), - ] - ) + entities = [ + DoorBirdCamera( + door_bird_data, + device.live_image_url, + "live", + _LIVE_INTERVAL, + device.rtsp_live_video_url, + ) + ] + + for camera_id, history_type, interval in ( + ("last_ring", "doorbell", _LAST_VISITOR_INTERVAL), + ("last_motion", "motionsensor", _LAST_MOTION_INTERVAL), + ): + if async_should_create_deprecated_entity( + hass, + entity_registry, + config_entry_id=config_entry.entry_id, + platform_domain=Platform.CAMERA, + entity_unique_id=f"{mac_addr}_{camera_id}", + issue_id=f"deprecated_camera_{mac_addr}_{camera_id}", + translation_key=f"deprecated_camera_{camera_id}", + ): + entities.append( + DoorBirdCamera( + door_bird_data, + device.history_image_url(1, history_type), + camera_id, + interval, + ) + ) + + async_add_entities(entities) class DoorBirdCamera(DoorBirdEntity, Camera): diff --git a/homeassistant/components/doorbird/deprecation.py b/homeassistant/components/doorbird/deprecation.py new file mode 100644 index 000000000000..f629a5e5e227 --- /dev/null +++ b/homeassistant/components/doorbird/deprecation.py @@ -0,0 +1,95 @@ +"""Deprecation helpers for the DoorBird integration.""" + +from homeassistant.components.automation import automations_with_entity +from homeassistant.components.script import scripts_with_entity +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import entity_registry as er +from homeassistant.helpers.issue_registry import ( + IssueSeverity, + async_create_issue, + async_delete_issue, +) + +from .const import DOMAIN + +DEPRECATED_REMOVAL_VERSION = "2027.5.0" +EVENTS_DOCS_URL = "https://www.home-assistant.io/integrations/doorbird/#events" + + +@callback +def async_should_create_deprecated_entity( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + *, + config_entry_id: str, + platform_domain: str, + entity_unique_id: str, + issue_id: str, + translation_key: str, +) -> bool: + """Return whether a deprecated entity should still be created. + + Raises the repair issue while the user has the entity and removes it once + the user disables it. Only runs during platform setup, so that takes a reload. + """ + entity_id = entity_registry.async_get_entity_id( + platform_domain, DOMAIN, entity_unique_id + ) + if ( + entity_id is None + or (entity_entry := entity_registry.async_get(entity_id)) is None + ): + async_delete_issue(hass, DOMAIN, issue_id) + return False + + if entity_entry.disabled: + # Disabling the device also disables the entity, which is not a request + # to remove it. + if entity_entry.disabled_by is er.RegistryEntryDisabler.USER: + entity_registry.async_remove(entity_id) + async_delete_issue(hass, DOMAIN, issue_id) + return False + + placeholders = { + "entity_id": entity_id, + "entity_name": entity_entry.name or entity_entry.original_name or entity_id, + "docs_url": EVENTS_DOCS_URL, + } + if items := _automations_and_scripts_using_entity(hass, entity_registry, entity_id): + translation_key = f"{translation_key}_scripts" + placeholders["items"] = "\n".join(items) + + async_create_issue( + hass, + DOMAIN, + issue_id, + breaks_in_ha_version=DEPRECATED_REMOVAL_VERSION, + config_entry_id=config_entry_id, + is_fixable=False, + severity=IssueSeverity.WARNING, + translation_key=translation_key, + translation_placeholders=placeholders, + ) + return True + + +def _automations_and_scripts_using_entity( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + entity_id: str, +) -> list[str]: + """Return markdown links to the automations and scripts using an entity.""" + items: list[str] = [] + for integration, entities in ( + ("automation", automations_with_entity(hass, entity_id)), + ("script", scripts_with_entity(hass, entity_id)), + ): + for used_entity_id in entities: + # YAML automations without an id have no registry entry to link to. + if item := entity_registry.async_get(used_entity_id): + items.append( + f"- [{item.original_name}](/config/{integration}/edit/{item.unique_id})" + ) + else: + items.append(f"- `{used_entity_id}`") + return items diff --git a/homeassistant/components/doorbird/device.py b/homeassistant/components/doorbird/device.py index ea435d788510..753c62edecfa 100644 --- a/homeassistant/components/doorbird/device.py +++ b/homeassistant/components/doorbird/device.py @@ -210,6 +210,22 @@ class ConfiguredDoorBird: """Get device slug.""" return slugify(self._name) + def event_names_of_type(self, event_type: str) -> list[str]: + """Return the configured event names of an event type. + + Models without the schedule API have no descriptions, so fall back to + the type a default event name implies. + """ + described = {event.event: event.event_type for event in self.event_descriptions} + default_types = dict(DEFAULT_EVENT_TYPES) + return [ + event_name + for event, event_name in zip( + self.events, self.door_station_events, strict=True + ) + if (described.get(event_name) or default_types.get(event)) == event_type + ] + def _get_event_name(self, event: str) -> str: return f"{self.slug}_{event}" diff --git a/homeassistant/components/doorbird/image.py b/homeassistant/components/doorbird/image.py index ea4846e91558..096c30dfe24b 100644 --- a/homeassistant/components/doorbird/image.py +++ b/homeassistant/components/doorbird/image.py @@ -1,10 +1,5 @@ """Last motion and last ring image entities for a DoorBird device.""" -# These replace the same-named camera entities, which exposed stills through the -# camera UI even though no live video is involved. The legacy camera entities are -# kept to avoid breaking existing dashboards and automations; a follow-up should -# deprecate them via a repair issue once users have had time to migrate. - from dataclasses import dataclass from typing import override @@ -22,7 +17,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util import dt as dt_util -from .const import DEFAULT_EVENT_TYPES, DOMAIN +from .const import DOMAIN from .entity import DoorBirdEntity from .models import DoorBirdConfigEntry, DoorBirdData @@ -83,23 +78,9 @@ class DoorBirdLastEventImage(ImageEntity, DoorBirdEntity): else "motionsensor" ) self._image_url = self._door_station.device.history_image_url(1, history_type) - # Models without the schedule API have no descriptions, so fall back to - # the type a default event name implies. - described = { - event.event: event.event_type - for event in self._door_station.event_descriptions - } - default_types = dict(DEFAULT_EVENT_TYPES) - self._matching_event_names = [ - event_name - for event, event_name in zip( - self._door_station.events, - self._door_station.door_station_events, - strict=True, - ) - if (described.get(event_name) or default_types.get(event)) - == description.doorbird_event_type - ] + self._matching_event_names = self._door_station.event_names_of_type( + description.doorbird_event_type + ) @override async def async_image(self) -> bytes | None: diff --git a/homeassistant/components/doorbird/manifest.json b/homeassistant/components/doorbird/manifest.json index cac29cd0a4f6..cde4007a0276 100644 --- a/homeassistant/components/doorbird/manifest.json +++ b/homeassistant/components/doorbird/manifest.json @@ -1,6 +1,7 @@ { "domain": "doorbird", "name": "DoorBird", + "after_dependencies": ["automation", "script"], "categories": ["security_safety.doorbells_intercoms"], "codeowners": ["@oblogic7", "@bdraco", "@flacjacket"], "config_flow": true, diff --git a/homeassistant/components/doorbird/strings.json b/homeassistant/components/doorbird/strings.json index 5e84ccac608a..a76b41e33fe3 100644 --- a/homeassistant/components/doorbird/strings.json +++ b/homeassistant/components/doorbird/strings.json @@ -84,6 +84,22 @@ } }, "issues": { + "deprecated_camera_last_motion": { + "description": "The `{entity_id}` ({entity_name}) camera entity is deprecated and has been replaced by the **Last motion** image entity on the same device, which shows the same still image and updates the moment a motion event fires. The image entity relies on the DoorBird calling Home Assistant, so make sure the events are set up as described in the [integration documentation]({docs_url}).\n\nUpdate any dashboards, templates, automations or scripts to use the image entity, then disable `{entity_id}` and it is removed automatically on the next restart or reload of the integration.", + "title": "The DoorBird last motion camera is deprecated" + }, + "deprecated_camera_last_motion_scripts": { + "description": "The `{entity_id}` ({entity_name}) camera entity is deprecated and has been replaced by the **Last motion** image entity on the same device, which shows the same still image and updates the moment a motion event fires. The image entity relies on the DoorBird calling Home Assistant, so make sure the events are set up as described in the [integration documentation]({docs_url}).\n\nIt is still used in the following automations or scripts:\n{items}\n\nUpdate them to use the image entity, then disable `{entity_id}` and it is removed automatically on the next restart or reload of the integration.", + "title": "[%key:component::doorbird::issues::deprecated_camera_last_motion::title%]" + }, + "deprecated_camera_last_ring": { + "description": "The `{entity_id}` ({entity_name}) camera entity is deprecated and has been replaced by the **Last ring** image entity on the same device, which shows the same still image and updates the moment a doorbell ring event fires. The image entity relies on the DoorBird calling Home Assistant, so make sure the events are set up as described in the [integration documentation]({docs_url}).\n\nUpdate any dashboards, templates, automations or scripts to use the image entity, then disable `{entity_id}` and it is removed automatically on the next restart or reload of the integration.", + "title": "The DoorBird last ring camera is deprecated" + }, + "deprecated_camera_last_ring_scripts": { + "description": "The `{entity_id}` ({entity_name}) camera entity is deprecated and has been replaced by the **Last ring** image entity on the same device, which shows the same still image and updates the moment a doorbell ring event fires. The image entity relies on the DoorBird calling Home Assistant, so make sure the events are set up as described in the [integration documentation]({docs_url}).\n\nIt is still used in the following automations or scripts:\n{items}\n\nUpdate them to use the image entity, then disable `{entity_id}` and it is removed automatically on the next restart or reload of the integration.", + "title": "[%key:component::doorbird::issues::deprecated_camera_last_ring::title%]" + }, "error_registering_events": { "fix_flow": { "step": { diff --git a/tests/components/doorbird/test_camera.py b/tests/components/doorbird/test_camera.py index a310bcb88cce..b31741e830c7 100644 --- a/tests/components/doorbird/test_camera.py +++ b/tests/components/doorbird/test_camera.py @@ -3,44 +3,267 @@ from freezegun.api import FrozenDateTimeFactory import pytest +from homeassistant.components.automation import DOMAIN as AUTOMATION_DOMAIN from homeassistant.components.camera import ( CameraState, async_get_image, async_get_stream_source, ) +from homeassistant.components.doorbird.const import DOMAIN +from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import entity_registry as er, issue_registry as ir +from homeassistant.setup import async_setup_component from . import mock_not_found_exception from .conftest import DoorbirdMockerType +LIVE_CAMERA_ENTITY_ID = "camera.mydoorbird_live" +LAST_RING_CAMERA_ENTITY_ID = "camera.mydoorbird_last_ring" +LAST_MOTION_CAMERA_ENTITY_ID = "camera.mydoorbird_last_motion" +LAST_RING_ISSUE_ID = "deprecated_camera_1234ABCD_last_ring" -async def test_doorbird_cameras( + +def create_stale_issue(hass: HomeAssistant, issue_id: str) -> None: + """Create a repair issue left over from an earlier setup.""" + ir.async_create_issue( + hass, + DOMAIN, + issue_id, + is_fixable=False, + severity=ir.IssueSeverity.WARNING, + translation_key="deprecated_camera_last_ring", + translation_placeholders={ + "entity_id": LAST_RING_CAMERA_ENTITY_ID, + "entity_name": "Last ring", + "docs_url": "", + }, + ) + + +async def test_doorbird_live_camera( hass: HomeAssistant, doorbird_mocker: DoorbirdMockerType, freezer: FrozenDateTimeFactory, ) -> None: - """Test the doorbird cameras.""" + """Test the live camera, which has no image entity replacement.""" doorbird_entry = await doorbird_mocker() - live_camera_entity_id = "camera.mydoorbird_live" - assert hass.states.get(live_camera_entity_id).state == CameraState.IDLE - last_motion_camera_entity_id = "camera.mydoorbird_last_motion" - assert hass.states.get(last_motion_camera_entity_id).state == CameraState.IDLE - last_ring_camera_entity_id = "camera.mydoorbird_last_ring" - assert hass.states.get(last_ring_camera_entity_id).state == CameraState.IDLE - assert await async_get_stream_source(hass, live_camera_entity_id) is not None + assert hass.states.get(LIVE_CAMERA_ENTITY_ID).state == CameraState.IDLE + assert await async_get_stream_source(hass, LIVE_CAMERA_ENTITY_ID) is not None api = doorbird_entry.api api.get_image.side_effect = mock_not_found_exception() with pytest.raises(HomeAssistantError): - await async_get_image(hass, live_camera_entity_id) + await async_get_image(hass, LIVE_CAMERA_ENTITY_ID) api.get_image.side_effect = TimeoutError() with pytest.raises(HomeAssistantError): - await async_get_image(hass, live_camera_entity_id) + await async_get_image(hass, LIVE_CAMERA_ENTITY_ID) api.get_image.side_effect = None - assert (await async_get_image(hass, live_camera_entity_id)).content == b"image" + assert (await async_get_image(hass, LIVE_CAMERA_ENTITY_ID)).content == b"image" api.get_image.return_value = b"notyet" # Ensure rate limit works - assert (await async_get_image(hass, live_camera_entity_id)).content == b"image" + assert (await async_get_image(hass, LIVE_CAMERA_ENTITY_ID)).content == b"image" freezer.tick(60) - assert (await async_get_image(hass, live_camera_entity_id)).content == b"notyet" + assert (await async_get_image(hass, LIVE_CAMERA_ENTITY_ID)).content == b"notyet" + + +@pytest.mark.parametrize( + ("camera_id", "entity_id"), + [ + pytest.param("last_ring", LAST_RING_CAMERA_ENTITY_ID, id="last_ring"), + pytest.param("last_motion", LAST_MOTION_CAMERA_ENTITY_ID, id="last_motion"), + ], +) +async def test_deprecated_camera_not_created_for_new_installs( + hass: HomeAssistant, + doorbird_mocker: DoorbirdMockerType, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, + camera_id: str, + entity_id: str, +) -> None: + """Test the deprecated still cameras are not created on a fresh install.""" + create_stale_issue(hass, f"deprecated_camera_1234ABCD_{camera_id}") + + await doorbird_mocker() + + assert hass.states.get(entity_id) is None + assert ( + entity_registry.async_get_entity_id( + Platform.CAMERA, DOMAIN, f"1234ABCD_{camera_id}" + ) + is None + ) + assert ( + DOMAIN, + f"deprecated_camera_1234ABCD_{camera_id}", + ) not in issue_registry.issues + + +@pytest.mark.parametrize( + ("camera_id", "entity_id"), + [ + pytest.param("last_ring", LAST_RING_CAMERA_ENTITY_ID, id="last_ring"), + pytest.param("last_motion", LAST_MOTION_CAMERA_ENTITY_ID, id="last_motion"), + ], +) +async def test_deprecated_camera_kept_and_flagged( + hass: HomeAssistant, + doorbird_mocker: DoorbirdMockerType, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, + camera_id: str, + entity_id: str, +) -> None: + """Test an existing still camera is kept and raises a repair issue.""" + entity_registry.async_get_or_create( + Platform.CAMERA, + DOMAIN, + f"1234ABCD_{camera_id}", + suggested_object_id=f"mydoorbird_{camera_id}", + ) + + doorbird_entry = await doorbird_mocker() + + assert hass.states.get(entity_id).state == CameraState.IDLE + issue = issue_registry.async_get_issue( + DOMAIN, f"deprecated_camera_1234ABCD_{camera_id}" + ) + assert issue is not None + assert issue.translation_key == f"deprecated_camera_{camera_id}" + assert issue.translation_placeholders["entity_id"] == entity_id + assert issue.config_entry_id == doorbird_entry.entry.entry_id + + +async def test_deprecated_camera_issue_names_renamed_entity( + hass: HomeAssistant, + doorbird_mocker: DoorbirdMockerType, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, +) -> None: + """Test the issue names the entity id the user gave the camera.""" + entity_registry.async_get_or_create( + Platform.CAMERA, + DOMAIN, + "1234ABCD_last_ring", + suggested_object_id="front_door_ring_snapshot", + ) + + await doorbird_mocker() + + issue = issue_registry.async_get_issue(DOMAIN, LAST_RING_ISSUE_ID) + assert issue is not None + assert issue.translation_placeholders["entity_id"] == ( + "camera.front_door_ring_snapshot" + ) + + +@pytest.mark.parametrize( + "camera_id", + [ + pytest.param("last_ring", id="last_ring"), + pytest.param("last_motion", id="last_motion"), + ], +) +async def test_deprecated_camera_removed_when_disabled( + hass: HomeAssistant, + doorbird_mocker: DoorbirdMockerType, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, + camera_id: str, +) -> None: + """Test a disabled still camera is removed and the repair issue cleared.""" + entity_registry.async_get_or_create( + Platform.CAMERA, + DOMAIN, + f"1234ABCD_{camera_id}", + suggested_object_id=f"mydoorbird_{camera_id}", + disabled_by=er.RegistryEntryDisabler.USER, + ) + create_stale_issue(hass, f"deprecated_camera_1234ABCD_{camera_id}") + + await doorbird_mocker() + + assert ( + entity_registry.async_get_entity_id( + Platform.CAMERA, DOMAIN, f"1234ABCD_{camera_id}" + ) + is None + ) + assert ( + DOMAIN, + f"deprecated_camera_1234ABCD_{camera_id}", + ) not in issue_registry.issues + + +async def test_deprecated_camera_kept_when_disabled_by_device( + hass: HomeAssistant, + doorbird_mocker: DoorbirdMockerType, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, +) -> None: + """Test a camera disabled along with its device is neither removed nor flagged.""" + entity_registry.async_get_or_create( + Platform.CAMERA, + DOMAIN, + "1234ABCD_last_ring", + suggested_object_id="mydoorbird_last_ring", + disabled_by=er.RegistryEntryDisabler.DEVICE, + ) + create_stale_issue(hass, LAST_RING_ISSUE_ID) + + await doorbird_mocker() + + assert entity_registry.async_get(LAST_RING_CAMERA_ENTITY_ID) is not None + assert (DOMAIN, LAST_RING_ISSUE_ID) not in issue_registry.issues + + +@pytest.mark.parametrize( + ("automation_id", "expected_item"), + [ + pytest.param( + {"id": "test_automation"}, + "[test_automation](/config/automation/edit/test_automation)", + id="registered", + ), + pytest.param({}, "`automation.test_automation`", id="yaml_without_id"), + ], +) +async def test_deprecated_camera_issue_lists_automations( + hass: HomeAssistant, + doorbird_mocker: DoorbirdMockerType, + entity_registry: er.EntityRegistry, + issue_registry: ir.IssueRegistry, + automation_id: dict[str, str], + expected_item: str, +) -> None: + """Test the issue lists the automations still using the camera.""" + entity_registry.async_get_or_create( + Platform.CAMERA, + DOMAIN, + "1234ABCD_last_ring", + suggested_object_id="mydoorbird_last_ring", + ) + assert await async_setup_component( + hass, + AUTOMATION_DOMAIN, + { + AUTOMATION_DOMAIN: { + **automation_id, + "alias": "test_automation", + "triggers": { + "trigger": "state", + "entity_id": LAST_RING_CAMERA_ENTITY_ID, + }, + "actions": {"action": "notify.notify", "data": {}}, + } + }, + ) + + await doorbird_mocker() + + issue = issue_registry.async_get_issue(DOMAIN, LAST_RING_ISSUE_ID) + assert issue is not None + assert issue.translation_key == "deprecated_camera_last_ring_scripts" + assert issue.translation_placeholders["items"] == f"- {expected_item}" diff --git a/tests/components/doorbird/test_view.py b/tests/components/doorbird/test_view.py index c7bb0fab264b..9d5a35906d26 100644 --- a/tests/components/doorbird/test_view.py +++ b/tests/components/doorbird/test_view.py @@ -6,8 +6,8 @@ import pytest from pytest_homeassistant.common import async_capture_events from pytest_homeassistant.typing import ClientSessionGenerator -from homeassistant.components.doorbird.const import API_URL -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.components.doorbird.const import API_URL, DOMAIN +from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -87,7 +87,15 @@ async def test_webhook_event_entity_id_after_entity_removed( expected_entity_id: str | None, ) -> None: """Test removing an entity only drops the event mappings it owns.""" + # The deprecated camera is only created when it is already registered. + entity_registry.async_get_or_create( + Platform.CAMERA, + DOMAIN, + "1234ABCD_last_ring", + suggested_object_id="mydoorbird_last_ring", + ) doorbird_entry = await doorbird_mocker() + assert hass.states.get(removed_entity_id) is not None entity_registry.async_remove(removed_entity_id) await hass.async_block_till_done() assert hass.states.get(removed_entity_id) is None