From b8e18627465581955736e5c015205309944ad569 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Sun, 17 Mar 2024 15:59:29 +0100 Subject: [PATCH] Remove deprecated `hass.components` from image_processing platform (#113613) * Remove deprecated `hass.components` from image_processing platform * Add test and change log * D'oh.. use updated error text --- .../components/image_processing/__init__.py | 13 ++++++----- .../components/image_processing/test_init.py | 22 +++++++++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/image_processing/__init__.py b/homeassistant/components/image_processing/__init__.py index b75cb445895..ed366c0bdfc 100644 --- a/homeassistant/components/image_processing/__init__.py +++ b/homeassistant/components/image_processing/__init__.py @@ -10,7 +10,7 @@ from typing import Any, Final, TypedDict, final import voluptuous as vol -from homeassistant.components.camera import Image +from homeassistant.components.camera import async_get_image from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_NAME, @@ -176,13 +176,16 @@ class ImageProcessingEntity(Entity): This method is a coroutine. """ - camera = self.hass.components.camera + if self.camera_entity is None: + _LOGGER.error( + "No camera entity id was set by the image processing entity", + ) + return try: - image: Image = await camera.async_get_image( - self.camera_entity, timeout=self.timeout + image = await async_get_image( + self.hass, self.camera_entity, timeout=self.timeout ) - except HomeAssistantError as err: _LOGGER.error("Error on receive image from entity: %s", err) return diff --git a/tests/components/image_processing/test_init.py b/tests/components/image_processing/test_init.py index 88aedc548ce..cf92047b49a 100644 --- a/tests/components/image_processing/test_init.py +++ b/tests/components/image_processing/test_init.py @@ -1,5 +1,4 @@ """The tests for the image_processing component.""" - from unittest.mock import PropertyMock, patch import pytest @@ -103,7 +102,7 @@ async def test_get_image_from_camera( @patch( - "homeassistant.components.camera.async_get_image", + "homeassistant.components.image_processing.async_get_image", side_effect=HomeAssistantError(), ) async def test_get_image_without_exists_camera( @@ -180,3 +179,22 @@ async def test_face_event_call_no_confidence( assert event_data[0]["confidence"] == 98.34 assert event_data[0]["gender"] == "male" assert event_data[0]["entity_id"] == "image_processing.demo_face" + + +async def test_update_missing_camera( + hass: HomeAssistant, + aiohttp_unused_port_factory, + enable_custom_integrations: None, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test when entity does not set camera.""" + await setup_image_processing(hass, aiohttp_unused_port_factory) + + with patch( + "custom_components.test.image_processing.TestImageProcessing.camera_entity", + new_callable=PropertyMock(return_value=None), + ): + common.async_scan(hass, entity_id="image_processing.test") + await hass.async_block_till_done() + + assert "No camera entity id was set by the image processing entity" in caplog.text