Allow MS face detection to handle updating entities when no face is detected (#17593)

* Allow Microsoft face detection to handle updating entities when no face is detected

* Remove microsoft_face_detect_no_face_detected.json and hard code in simple empty list into the tests
This commit is contained in:
Neil Crosby 2018-11-02 09:50:43 +00:00 committed by Pascal Vizeli
parent a4c0c34028
commit cb7ae5cdf2
4 changed files with 47 additions and 10 deletions

View File

@ -102,7 +102,7 @@ class MicrosoftFaceDetectEntity(ImageProcessingFaceEntity):
return return
if not face_data: if not face_data:
return face_data = []
faces = [] faces = []
for face in face_data: for face in face_data:

View File

@ -83,18 +83,16 @@ class MicrosoftFaceIdentifyEntity(ImageProcessingFaceEntity):
This method is a coroutine. This method is a coroutine.
""" """
detect = None detect = []
try: try:
face_data = await self._api.call_api( face_data = await self._api.call_api(
'post', 'detect', image, binary=True) 'post', 'detect', image, binary=True)
if not face_data: if face_data:
return face_ids = [data['faceId'] for data in face_data]
detect = await self._api.call_api(
face_ids = [data['faceId'] for data in face_data] 'post', 'identify',
detect = await self._api.call_api( {'faceIds': face_ids, 'personGroupId': self._face_group})
'post', 'identify',
{'faceIds': face_ids, 'personGroupId': self._face_group})
except HomeAssistantError as err: except HomeAssistantError as err:
_LOGGER.error("Can't process image on Microsoft face: %s", err) _LOGGER.error("Can't process image on Microsoft face: %s", err)

View File

@ -160,3 +160,23 @@ class TestMicrosoftFaceDetect:
assert face_events[0].data['gender'] == 'male' assert face_events[0].data['gender'] == 'male'
assert face_events[0].data['entity_id'] == \ assert face_events[0].data['entity_id'] == \
'image_processing.test_local' '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'

View File

@ -2,7 +2,7 @@
from unittest.mock import patch, PropertyMock from unittest.mock import patch, PropertyMock
from homeassistant.core import callback 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 from homeassistant.setup import setup_component
import homeassistant.components.image_processing as ip import homeassistant.components.image_processing as ip
import homeassistant.components.microsoft_face as mf 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['confidence'] == float(92)
assert face_events[0].data['entity_id'] == \ assert face_events[0].data['entity_id'] == \
'image_processing.test_local' '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