def test_openalpr_process_image(self, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.post( OPENALPR_API_URL, params=self.params, text=load_fixture("alpr_cloud.json"), status=200, ) with patch( "homeassistant.components.camera.async_get_image", return_value=camera.Image("image/jpeg", b"image"), ): common.scan(self.hass, entity_id="image_processing.test_local") self.hass.block_till_done() state = self.hass.states.get("image_processing.test_local") assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 5 assert state.attributes.get("vehicles") == 1 assert state.state == "H786P0J" event_data = [ event.data for event in self.alpr_events if event.data.get("plate") == "H786P0J" ] assert len(event_data) == 1 assert event_data[0]["plate"] == "H786P0J" assert event_data[0]["confidence"] == float(90.436699) assert event_data[0]["entity_id"] == "image_processing.test_local"
def test_openalpr_process_image(self, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.post( OPENALPR_API_URL, params=self.params, text=load_fixture('alpr_cloud.json'), status=200 ) with patch('homeassistant.components.camera.async_get_image', return_value=mock_coro( camera.Image('image/jpeg', b'image'))): common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() state = self.hass.states.get('image_processing.test_local') assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 5 assert state.attributes.get('vehicles') == 1 assert state.state == 'H786P0J' event_data = [event.data for event in self.alpr_events if event.data.get('plate') == 'H786P0J'] assert len(event_data) == 1 assert event_data[0]['plate'] == 'H786P0J' assert event_data[0]['confidence'] == float(90.436699) assert event_data[0]['entity_id'] == \ 'image_processing.test_local'
def test_openalpr_process_image(self, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.post(OPENALPR_API_URL, params=self.params, text=load_fixture('alpr_cloud.json'), status=200) with patch('homeassistant.components.camera.async_get_image', return_value=mock_coro(camera.Image('image/jpeg', b'image'))): common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() state = self.hass.states.get('image_processing.test_local') assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 5 assert state.attributes.get('vehicles') == 1 assert state.state == 'H786P0J' event_data = [ event.data for event in self.alpr_events if event.data.get('plate') == 'H786P0J' ] assert len(event_data) == 1 assert event_data[0]['plate'] == 'H786P0J' assert event_data[0]['confidence'] == float(90.436699) assert event_data[0]['entity_id'] == \ 'image_processing.test_local'
def test_get_image_from_camera(self, mock_camera_read): """Grab an image from camera entity.""" common.scan(self.hass, entity_id="image_processing.test") self.hass.block_till_done() state = self.hass.states.get("image_processing.test") assert mock_camera_read.called assert state.state == "1" assert state.attributes["image"] == b"Test"
def test_get_image_without_exists_camera(self, mock_image): """Try to get image without exists camera.""" self.hass.states.remove("camera.demo_camera") common.scan(self.hass, entity_id="image_processing.test") self.hass.block_till_done() state = self.hass.states.get("image_processing.test") assert mock_image.called assert state.state == "0"
def test_get_image_without_exists_camera(self, mock_image): """Try to get image without exists camera.""" self.hass.states.remove('camera.demo_camera') common.scan(self.hass, entity_id='image_processing.test') self.hass.block_till_done() state = self.hass.states.get('image_processing.test') assert mock_image.called assert state.state == '0'
def test_get_image_from_camera(self, mock_camera): """Grab an image from camera entity.""" self.hass.start() common.scan(self.hass, entity_id='image_processing.test') self.hass.block_till_done() state = self.hass.states.get('image_processing.test') assert mock_camera.called assert state.state == '1' assert state.attributes['image'] == b'Test'
def test_get_image_from_camera(self, mock_camera): """Grab an image from camera entity.""" self.hass.start() common.scan(self.hass, entity_id='image_processing.test') self.hass.block_till_done() state = self.hass.states.get('image_processing.test') assert mock_camera.called assert state.state == '1' assert state.attributes['image'] == b'Test'
def test_openalpr_process_image_api_timeout(self, aioclient_mock): """Set up and scan a picture and test api error.""" aioclient_mock.post(OPENALPR_API_URL, params=self.params, exc=asyncio.TimeoutError()) with patch('homeassistant.components.camera.async_get_image', return_value=mock_coro(camera.Image('image/jpeg', b'image'))): common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 0
def test_openalpr_process_image_api_timeout(self, aioclient_mock): """Set up and scan a picture and test api error.""" aioclient_mock.post( OPENALPR_API_URL, params=self.params, exc=asyncio.TimeoutError() ) with patch('homeassistant.components.camera.async_get_image', return_value=mock_coro( camera.Image('image/jpeg', b'image'))): common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 0
def test_openalpr_process_image_api_timeout(self, aioclient_mock): """Set up and scan a picture and test api error.""" aioclient_mock.post(OPENALPR_API_URL, params=self.params, exc=asyncio.TimeoutError()) with patch( "openpeerpower.components.camera.async_get_image", return_value=camera.Image("image/jpeg", b"image"), ): common.scan(self.opp, entity_id="image_processing.test_local") self.opp.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 0
def test_ms_identify_process_image(self, poll_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get( self.endpoint_url.format("persongroups"), text=load_fixture('microsoft_face_persongroups.json')) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group1/persons"), text=load_fixture('microsoft_face_persons.json')) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group2/persons"), text=load_fixture('microsoft_face_persons.json')) setup_component(self.hass, ip.DOMAIN, self.config) state = self.hass.states.get('camera.demo_camera') url = "{0}{1}".format(self.hass.config.api.base_url, state.attributes.get(ATTR_ENTITY_PICTURE)) face_events = [] @callback def mock_face_event(event): """Mock event.""" face_events.append(event) self.hass.bus.listen('image_processing.detect_face', mock_face_event) aioclient_mock.get(url, content=b'image') aioclient_mock.post(self.endpoint_url.format("detect"), text=load_fixture('microsoft_face_detect.json')) aioclient_mock.post(self.endpoint_url.format("identify"), text=load_fixture('microsoft_face_identify.json')) common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() state = self.hass.states.get('image_processing.test_local') assert len(face_events) == 1 assert state.attributes.get('total_faces') == 2 assert state.state == 'David' assert face_events[0].data['name'] == 'David' assert face_events[0].data['confidence'] == float(92) assert face_events[0].data['entity_id'] == \ 'image_processing.test_local'
def test_alpr_event_single_call(self, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') common.scan(self.hass, entity_id='image_processing.demo_alpr') self.hass.block_till_done() state = self.hass.states.get('image_processing.demo_alpr') assert len(self.alpr_events) == 4 assert state.state == 'AC3829' event_data = [event.data for event in self.alpr_events if event.data.get('plate') == 'AC3829'] assert len(event_data) == 1 assert event_data[0]['plate'] == 'AC3829' assert event_data[0]['confidence'] == 98.3 assert event_data[0]['entity_id'] == 'image_processing.demo_alpr'
def test_openalpr_process_image_api_error(self, aioclient_mock): """Set up and scan a picture and test api error.""" aioclient_mock.post( OPENALPR_API_URL, params=self.params, text="{'error': 'error message'}", status=400, ) with patch( "homeassistant.components.camera.async_get_image", return_value=camera.Image("image/jpeg", b"image"), ): common.scan(self.hass, entity_id="image_processing.test_local") self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(self.alpr_events) == 0
def test_alpr_event_single_call(self, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b"image") common.scan(self.hass, entity_id="image_processing.demo_alpr") self.hass.block_till_done() state = self.hass.states.get("image_processing.demo_alpr") assert len(self.alpr_events) == 4 assert state.state == "AC3829" event_data = [ event.data for event in self.alpr_events if event.data.get("plate") == "AC3829" ] assert len(event_data) == 1 assert event_data[0]["plate"] == "AC3829" assert event_data[0]["confidence"] == 98.3 assert event_data[0]["entity_id"] == "image_processing.demo_alpr"
def test_alpr_event_single_call(self, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') common.scan(self.hass, entity_id='image_processing.demo_alpr') self.hass.block_till_done() state = self.hass.states.get('image_processing.demo_alpr') assert len(self.alpr_events) == 4 assert state.state == 'AC3829' event_data = [ event.data for event in self.alpr_events if event.data.get('plate') == 'AC3829' ] assert len(event_data) == 1 assert event_data[0]['plate'] == 'AC3829' assert event_data[0]['confidence'] == 98.3 assert event_data[0]['entity_id'] == 'image_processing.demo_alpr'
def test_face_event_call_no_confidence(self, mock_config, aioclient_mock): """Set up and scan a picture and test faces from event.""" aioclient_mock.get(self.url, content=b"image") common.scan(self.hass, entity_id="image_processing.demo_face") self.hass.block_till_done() state = self.hass.states.get("image_processing.demo_face") assert len(self.face_events) == 3 assert state.state == "4" assert state.attributes["total_faces"] == 4 event_data = [ event.data for event in self.face_events if event.data.get("name") == "Hans" ] assert len(event_data) == 1 assert event_data[0]["name"] == "Hans" assert event_data[0]["confidence"] == 98.34 assert event_data[0]["gender"] == "male" assert event_data[0]["entity_id"] == "image_processing.demo_face"
def test_face_event_call_no_confidence(self, mock_config, aioclient_mock): """Set up and scan a picture and test faces from event.""" aioclient_mock.get(self.url, content=b'image') common.scan(self.hass, entity_id='image_processing.demo_face') self.hass.block_till_done() state = self.hass.states.get('image_processing.demo_face') assert len(self.face_events) == 3 assert state.state == '4' assert state.attributes['total_faces'] == 4 event_data = [event.data for event in self.face_events if event.data.get('name') == 'Hans'] assert len(event_data) == 1 assert event_data[0]['name'] == 'Hans' assert event_data[0]['confidence'] == 98.34 assert event_data[0]['gender'] == 'male' assert event_data[0]['entity_id'] == \ 'image_processing.demo_face'
def test_openalpr_process_image(self, popen_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() state = self.hass.states.get('image_processing.test_local') assert popen_mock.called assert len(self.alpr_events) == 5 assert state.attributes.get('vehicles') == 1 assert state.state == 'PE3R2X' event_data = [event.data for event in self.alpr_events if event.data.get('plate') == 'PE3R2X'] assert len(event_data) == 1 assert event_data[0]['plate'] == 'PE3R2X' assert event_data[0]['confidence'] == float(98.9371) assert event_data[0]['entity_id'] == \ 'image_processing.test_local'
def test_openalpr_process_image(self, popen_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b"image") common.scan(self.hass, entity_id="image_processing.test_local") self.hass.block_till_done() state = self.hass.states.get("image_processing.test_local") assert popen_mock.called assert len(self.alpr_events) == 5 assert state.attributes.get("vehicles") == 1 assert state.state == "PE3R2X" event_data = [ event.data for event in self.alpr_events if event.data.get("plate") == "PE3R2X" ] assert len(event_data) == 1 assert event_data[0]["plate"] == "PE3R2X" assert event_data[0]["confidence"] == float(98.9371) assert event_data[0]["entity_id"] == "image_processing.test_local"
def test_face_event_call_no_confidence(self, mock_config, aioclient_mock): """Set up and scan a picture and test faces from event.""" aioclient_mock.get(self.url, content=b'image') common.scan(self.hass, entity_id='image_processing.demo_face') self.hass.block_till_done() state = self.hass.states.get('image_processing.demo_face') assert len(self.face_events) == 3 assert state.state == '4' assert state.attributes['total_faces'] == 4 event_data = [ event.data for event in self.face_events if event.data.get('name') == 'Hans' ] assert len(event_data) == 1 assert event_data[0]['name'] == 'Hans' assert event_data[0]['confidence'] == 98.34 assert event_data[0]['gender'] == 'male' assert event_data[0]['entity_id'] == \ 'image_processing.demo_face'
def test_openalpr_process_image(self, popen_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() state = self.hass.states.get('image_processing.test_local') assert popen_mock.called assert len(self.alpr_events) == 5 assert state.attributes.get('vehicles') == 1 assert state.state == 'PE3R2X' event_data = [ event.data for event in self.alpr_events if event.data.get('plate') == 'PE3R2X' ] assert len(event_data) == 1 assert event_data[0]['plate'] == 'PE3R2X' assert event_data[0]['confidence'] == float(98.9371) assert event_data[0]['entity_id'] == \ 'image_processing.test_local'
def test_ms_detect_process_image(self, poll_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get( self.endpoint_url.format("persongroups"), text=load_fixture('microsoft_face_persongroups.json') ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group1/persons"), text=load_fixture('microsoft_face_persons.json') ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group2/persons"), text=load_fixture('microsoft_face_persons.json') ) setup_component(self.hass, ip.DOMAIN, self.config) state = self.hass.states.get('camera.demo_camera') url = "{0}{1}".format( self.hass.config.api.base_url, state.attributes.get(ATTR_ENTITY_PICTURE)) face_events = [] @callback def mock_face_event(event): """Mock event.""" face_events.append(event) self.hass.bus.listen('image_processing.detect_face', mock_face_event) aioclient_mock.get(url, content=b'image') aioclient_mock.post( self.endpoint_url.format("detect"), text=load_fixture('microsoft_face_detect.json'), 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') assert len(face_events) == 1 assert state.attributes.get('total_faces') == 1 assert state.state == '1' assert face_events[0].data['age'] == 71.0 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'
def test_ms_detect_process_image(self, poll_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get( self.endpoint_url.format("persongroups"), text=load_fixture("microsoft_face_persongroups.json"), ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group1/persons"), text=load_fixture("microsoft_face_persons.json"), ) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group2/persons"), text=load_fixture("microsoft_face_persons.json"), ) setup_component(self.hass, ip.DOMAIN, self.config) state = self.hass.states.get("camera.demo_camera") url = "{0}{1}".format(self.hass.config.api.base_url, state.attributes.get(ATTR_ENTITY_PICTURE)) face_events = [] @callback def mock_face_event(event): """Mock event.""" face_events.append(event) self.hass.bus.listen("image_processing.detect_face", mock_face_event) aioclient_mock.get(url, content=b"image") aioclient_mock.post( self.endpoint_url.format("detect"), text=load_fixture("microsoft_face_detect.json"), 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") assert len(face_events) == 1 assert state.attributes.get("total_faces") == 1 assert state.state == "1" assert face_events[0].data["age"] == 71.0 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"
def test_ms_identify_process_image(self, poll_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get( self.endpoint_url.format("persongroups"), text=load_fixture('microsoft_face_persongroups.json')) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group1/persons"), text=load_fixture('microsoft_face_persons.json')) aioclient_mock.get( self.endpoint_url.format("persongroups/test_group2/persons"), text=load_fixture('microsoft_face_persons.json')) setup_component(self.hass, ip.DOMAIN, self.config) state = self.hass.states.get('camera.demo_camera') url = "{0}{1}".format(self.hass.config.api.base_url, state.attributes.get(ATTR_ENTITY_PICTURE)) face_events = [] @callback def mock_face_event(event): """Mock event.""" face_events.append(event) self.hass.bus.listen('image_processing.detect_face', mock_face_event) aioclient_mock.get(url, content=b'image') aioclient_mock.post(self.endpoint_url.format("detect"), text=load_fixture('microsoft_face_detect.json')) aioclient_mock.post(self.endpoint_url.format("identify"), text=load_fixture('microsoft_face_identify.json')) common.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() state = self.hass.states.get('image_processing.test_local') assert len(face_events) == 1 assert state.attributes.get('total_faces') == 2 assert state.state == 'David' assert face_events[0].data['name'] == 'David' 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