async def test_openalpr_process_image(alpr_events, setup_openalpr_cloud, hass, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.post( OPENALPR_API_URL, params=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.async_scan(hass, entity_id="image_processing.test_local") await hass.async_block_till_done() state = hass.states.get("image_processing.test_local") assert len(aioclient_mock.mock_calls) == 1 assert len(alpr_events) == 5 assert state.attributes.get("vehicles") == 1 assert state.state == "H786P0J" event_data = [ event.data for event in 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"
async def test_openalpr_process_image( setup_openalpr_local, url, hass, alpr_events, popen_mock, aioclient_mock, ): """Set up and scan a picture and test plates from event.""" aioclient_mock.get(url, content=b"image") common.async_scan(hass, entity_id="image_processing.test_local") await hass.async_block_till_done() state = hass.states.get("image_processing.test_local") assert popen_mock.called assert len(alpr_events) == 5 assert state.attributes.get("vehicles") == 1 assert state.state == "PE3R2X" event_data = [ event.data for event in 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"
async def test_get_image_from_camera(mock_camera_read, hass, aiohttp_unused_port, enable_custom_integrations): """Grab an image from camera entity.""" await setup_image_processing(hass, aiohttp_unused_port) common.async_scan(hass, entity_id="image_processing.test") await hass.async_block_till_done() state = hass.states.get("image_processing.test") assert mock_camera_read.called assert state.state == "1" assert state.attributes["image"] == b"Test"
async def test_get_image_without_exists_camera(mock_image, hass, aiohttp_unused_port, enable_custom_integrations): """Try to get image without exists camera.""" await setup_image_processing(hass, aiohttp_unused_port) hass.states.async_remove("camera.demo_camera") common.async_scan(hass, entity_id="image_processing.test") await hass.async_block_till_done() state = hass.states.get("image_processing.test") assert mock_image.called assert state.state == "0"
async def test_openalpr_process_image_api_timeout(alpr_events, setup_openalpr_cloud, hass, aioclient_mock): """Set up and scan a picture and test api error.""" aioclient_mock.post(OPENALPR_API_URL, params=PARAMS, exc=asyncio.TimeoutError()) with patch( "homeassistant.components.camera.async_get_image", return_value=camera.Image("image/jpeg", b"image"), ): common.async_scan(hass, entity_id="image_processing.test_local") await hass.async_block_till_done() assert len(aioclient_mock.mock_calls) == 1 assert len(alpr_events) == 0
async def test_alpr_event_single_call(hass, aioclient_mock): """Set up and scan a picture and test plates from event.""" alpr_events = await setup_image_processing_alpr(hass) aioclient_mock.get(get_url(hass), content=b"image") common.async_scan(hass, entity_id="image_processing.demo_alpr") await hass.async_block_till_done() state = hass.states.get("image_processing.demo_alpr") assert len(alpr_events) == 4 assert state.state == "AC3829" event_data = [ event.data for event in 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"
async def test_face_event_call(hass, aioclient_mock): """Set up and scan a picture and test faces from event.""" face_events = await setup_image_processing_face(hass) aioclient_mock.get(get_url(hass), content=b"image") common.async_scan(hass, entity_id="image_processing.demo_face") await hass.async_block_till_done() state = hass.states.get("image_processing.demo_face") assert len(face_events) == 2 assert state.state == "Hans" assert state.attributes["total_faces"] == 4 event_data = [ event.data for event in 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"
async def test_ms_identify_process_image(hass, poll_mock, aioclient_mock): """Set up and scan a picture and test plates from event.""" aioclient_mock.get( ENDPOINT_URL.format("persongroups"), text=load_fixture("microsoft_face_persongroups.json"), ) aioclient_mock.get( ENDPOINT_URL.format("persongroups/test_group1/persons"), text=load_fixture("microsoft_face_persons.json"), ) aioclient_mock.get( ENDPOINT_URL.format("persongroups/test_group2/persons"), text=load_fixture("microsoft_face_persons.json"), ) await async_setup_component(hass, ip.DOMAIN, CONFIG) await hass.async_block_till_done() state = hass.states.get("camera.demo_camera") url = f"{hass.config.internal_url}{state.attributes.get(ATTR_ENTITY_PICTURE)}" face_events = [] @callback def mock_face_event(event): """Mock event.""" face_events.append(event) hass.bus.async_listen("image_processing.detect_face", mock_face_event) aioclient_mock.get(url, content=b"image") aioclient_mock.post( ENDPOINT_URL.format("detect"), text=load_fixture("microsoft_face_detect.json"), ) aioclient_mock.post( ENDPOINT_URL.format("identify"), text=load_fixture("microsoft_face_identify.json"), ) common.async_scan(hass, entity_id="image_processing.test_local") await hass.async_block_till_done() state = 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(ENDPOINT_URL.format("detect"), text="[]") common.async_scan(hass, entity_id="image_processing.test_local") await hass.async_block_till_done() state = 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