diff --git a/homeassistant/components/image_processing/microsoft_face_detect.py b/homeassistant/components/image_processing/microsoft_face_detect.py index 69bd8a8f931..ae6b9c260cd 100644 --- a/homeassistant/components/image_processing/microsoft_face_detect.py +++ b/homeassistant/components/image_processing/microsoft_face_detect.py @@ -102,7 +102,7 @@ class MicrosoftFaceDetectEntity(ImageProcessingFaceEntity): return if not face_data: - return + face_data = [] faces = [] for face in face_data: diff --git a/homeassistant/components/image_processing/microsoft_face_identify.py b/homeassistant/components/image_processing/microsoft_face_identify.py index 0a5b7725260..7a427d9b046 100644 --- a/homeassistant/components/image_processing/microsoft_face_identify.py +++ b/homeassistant/components/image_processing/microsoft_face_identify.py @@ -83,18 +83,16 @@ class MicrosoftFaceIdentifyEntity(ImageProcessingFaceEntity): This method is a coroutine. """ - detect = None + detect = [] try: face_data = await self._api.call_api( 'post', 'detect', image, binary=True) - if not face_data: - return - - face_ids = [data['faceId'] for data in face_data] - detect = await self._api.call_api( - 'post', 'identify', - {'faceIds': face_ids, 'personGroupId': self._face_group}) + if face_data: + face_ids = [data['faceId'] for data in face_data] + detect = await self._api.call_api( + 'post', 'identify', + {'faceIds': face_ids, 'personGroupId': self._face_group}) except HomeAssistantError as err: _LOGGER.error("Can't process image on Microsoft face: %s", err) diff --git a/tests/components/image_processing/test_microsoft_face_detect.py b/tests/components/image_processing/test_microsoft_face_detect.py index c7528c346ee..9e65386a3c6 100644 --- a/tests/components/image_processing/test_microsoft_face_detect.py +++ b/tests/components/image_processing/test_microsoft_face_detect.py @@ -160,3 +160,23 @@ class TestMicrosoftFaceDetect: assert face_events[0].data['gender'] == 'male' assert face_events[0].data['entity_id'] == \ 'image_processing.test_local' + + # Test that later, if a request is made that results in no face + # being detected, that this is reflected in the state object + aioclient_mock.clear_requests() + aioclient_mock.post( + self.endpoint_url.format("detect"), + text="[]", + params={'returnFaceAttributes': "age,gender"} + ) + + common.scan(self.hass, entity_id='image_processing.test_local') + self.hass.block_till_done() + + state = self.hass.states.get('image_processing.test_local') + + # No more face events were fired + assert len(face_events) == 1 + # Total faces and actual qualified number of faces reset to zero + assert state.attributes.get('total_faces') == 0 + assert state.state == '0' diff --git a/tests/components/image_processing/test_microsoft_face_identify.py b/tests/components/image_processing/test_microsoft_face_identify.py index 892326e5bff..c24e758da97 100644 --- a/tests/components/image_processing/test_microsoft_face_identify.py +++ b/tests/components/image_processing/test_microsoft_face_identify.py @@ -2,7 +2,7 @@ from unittest.mock import patch, PropertyMock from homeassistant.core import callback -from homeassistant.const import ATTR_ENTITY_PICTURE +from homeassistant.const import ATTR_ENTITY_PICTURE, STATE_UNKNOWN from homeassistant.setup import setup_component import homeassistant.components.image_processing as ip import homeassistant.components.microsoft_face as mf @@ -164,3 +164,22 @@ class TestMicrosoftFaceIdentify: assert face_events[0].data['confidence'] == float(92) assert face_events[0].data['entity_id'] == \ 'image_processing.test_local' + + # Test that later, if a request is made that results in no face + # being detected, that this is reflected in the state object + aioclient_mock.clear_requests() + aioclient_mock.post( + self.endpoint_url.format("detect"), + text="[]" + ) + + common.scan(self.hass, entity_id='image_processing.test_local') + self.hass.block_till_done() + + state = self.hass.states.get('image_processing.test_local') + + # No more face events were fired + assert len(face_events) == 1 + # Total faces and actual qualified number of faces reset to zero + assert state.attributes.get('total_faces') == 0 + assert state.state == STATE_UNKNOWN