def test_openalpr_process_image(self, aioclient_mock): """Setup and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') aioclient_mock.post(OPENALPR_API_URL, params=self.params, text=load_fixture('alpr_cloud.json'), status=200) ip.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) == 2 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'))): ip.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'))): ip.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): """Setup and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') aioclient_mock.post( OPENALPR_API_URL, params=self.params, text=load_fixture('alpr_cloud.json'), status=200 ) ip.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) == 2 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_without_exists_camera(self, mock_process, mock_image): """Try to get image without exists camera.""" self.hass.states.remove('camera.demo_camera') ip.scan(self.hass, entity_id='image_processing.demo') self.hass.block_till_done() assert mock_image.called assert not mock_process.called
def test_ms_identify_process_image(self, poll_mock, aioclient_mock): """Setup and scan a picture and test plates from event.""" aioclient_mock.get( mf.FACE_API_URL.format("persongroups"), text=load_fixture('microsoft_face_persongroups.json') ) aioclient_mock.get( mf.FACE_API_URL.format("persongroups/test_group1/persons"), text=load_fixture('microsoft_face_persons.json') ) aioclient_mock.get( mf.FACE_API_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( mf.FACE_API_URL.format("detect"), text=load_fixture('microsoft_face_detect.json') ) aioclient_mock.post( mf.FACE_API_URL.format("identify"), text=load_fixture('microsoft_face_identify.json') ) ip.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_get_image_from_camera(self, mock_process, mock_camera): """Grab a image from camera entity.""" self.hass.start() ip.scan(self.hass, entity_id='image_processing.demo') self.hass.block_till_done() assert mock_camera.called assert mock_process.called assert mock_process.call_args[0][1] == b'Test'
def test_get_image_from_camera(self, mock_camera): """Grab a image from camera entity.""" self.hass.start() ip.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): """Setup and scan a picture and test api error.""" aioclient_mock.get(self.url, content=b'image') aioclient_mock.post(OPENALPR_API_URL, params=self.params, exc=asyncio.TimeoutError()) ip.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 2 assert len(self.alpr_events) == 0
def test_openalpr_process_image_api_timeout(self, aioclient_mock): """Setup and scan a picture and test api error.""" aioclient_mock.get(self.url, content=b'image') aioclient_mock.post( OPENALPR_API_URL, params=self.params, exc=asyncio.TimeoutError() ) ip.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 2 assert len(self.alpr_events) == 0
def test_openalpr_process_image_api_error(self, aioclient_mock): """Setup and scan a picture and test api error.""" aioclient_mock.get(self.url, content=b'image') aioclient_mock.post( OPENALPR_API_URL, params=self.params, text="{'error': 'error message'}", status=400 ) ip.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 2 assert len(self.alpr_events) == 0
def test_openalpr_process_image_api_error(self, aioclient_mock): """Setup and scan a picture and test api error.""" aioclient_mock.get(self.url, content=b'image') aioclient_mock.post(OPENALPR_API_URL, params=self.params, text="{'error': 'error message'}", status=400) ip.scan(self.hass, entity_id='image_processing.test_local') self.hass.block_till_done() assert len(aioclient_mock.mock_calls) == 2 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'))): ip.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'))): ip.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): """Setup and scan a picture and test plates from event.""" aioclient_mock.get(self.url, content=b'image') ip.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(self, aioclient_mock): """Setup and scan a picture and test faces from event.""" aioclient_mock.get(self.url, content=b'image') ip.scan(self.hass, entity_id='image_processing.demo_face_identify') self.hass.block_till_done() state = self.hass.states.get('image_processing.demo_face_identify') assert len(self.face_events) == 2 assert state.state == 'Hans' 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]['entity_id'] == \ 'image_processing.demo_face_identify'
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') ip.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'