Add Facebox teach service#14998
Add Facebox teach service#14998MartinHjelmare merged 22 commits intohome-assistant:devfrom robmarkcole:facebox-teach-service
Conversation
|
This PR adds functions to both validate that a file_path is valid, and that a file is an image file. Are these methods available already in the HA API? |
|
OK I removed the text of image type as the API can actually handle a wide range of image types, and will return an error if an invalid type is posted, to that case is handled. |
| try: | ||
| cv.isfile(file_path) | ||
| return True | ||
| except: |
| """ | ||
| import base64 | ||
| import logging | ||
| import os |
| import pytest | ||
| import requests | ||
| import requests_mock | ||
| from unittest import mock |
There was a problem hiding this comment.
'unittest.mock' imported but unused
|
You need use: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/core.py#L1064 |
|
ping @pvizeli comment addressed |
| facebox.teach(name, file_path) | ||
| return True | ||
|
|
||
| hass.services.register( |
There was a problem hiding this comment.
This won't work. Only one service is registered per domain and service name key. Move this out of the for loop.
| """Teach a facebox a name.""" | ||
| name = call.data.get(ATTR_NAME) | ||
| file_path = call.data.get(FILE_PATH) | ||
| facebox.teach(name, file_path) |
There was a problem hiding this comment.
We should lookup which entity should be used. See other platforms that register services for examples.
Move the service handler out of the for loop.
There was a problem hiding this comment.
Something like hass.data[DOMAIN].facebox.teach(name, file_path) I'm guessing
There was a problem hiding this comment.
There was a problem hiding this comment.
Appears you use SonosData to access all SonosDevice objects, so I gather I will need an equivalent to access my FaceClassifyEntity objects in the service handler?
There was a problem hiding this comment.
Yes, you need to store them in hass.data since a platform can be setup multiple times, adding more entities each time.
| CLASSIFIER = 'facebox' | ||
| EVENT_CLASSIFIER_TEACH = 'image_processing.teach_classifier' | ||
| FILE_PATH = 'file_path' | ||
| SERVICE_FACEBOX_TEACH_FACE = 'facebox_teach_face' |
There was a problem hiding this comment.
Simplify constant name to SERVICE_TEACH_FACE. Don't change the string though.
|
We can replace the decorators with these fixtures: @pytest.fixture
def mock_isfile():
"""Mock os.path.isfile."""
with patch('os.path.isfile', Mock(return_value=True)) as _mock_isfile:
yield _mock_isfile
@pytest.fixture
def mock_access():
"""Mock os.access."""
with patch('os.access', Mock(return_value=True)) as _mock_access:
yield _mock_access |
|
@MartinHjelmare I don't understand why but |
|
@MartinHjelmare the line which is causing the setup to fail on the final test is: async def test_teach_service(hass, mock_image, mock_isfile, mock_access):If I remove |
| assert state.attributes.get('matched_faces') == {} | ||
|
|
||
|
|
||
| async def test_teach_service(hass, mock_image): # mock_isfile, mock_access |
There was a problem hiding this comment.
at least two spaces before inline comment
Removes the fixtures which were causing `setup` to fail, replace with `@patch`
|
ping @pvizeli @MartinHjelmare |
|
|
||
| @patch('os.access', Mock(return_value=True)) | ||
| @patch('os.path.isfile', Mock(return_value=True)) | ||
| async def test_teach_service(hass, mock_image): |
There was a problem hiding this comment.
This test is never awaited. Please run the tests locally, eg with script/lazytox.py. You'll see the warning in the pytest warning summary.
MartinHjelmare
left a comment
There was a problem hiding this comment.
I've fixed the test. We were patching the wrong things and didn't patch the call to open file.
|
Can be merged when build passes. |
|
Merging. Coverage calc is not accurate. |
|
Many thanks @MartinHjelmare :-) |
* Adds service * Address pylint * Update facebox.py * patch tests * Update facebox.py * Update test_facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Update test_facebox.py * Update test_facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Adds total_matched_faces * Update test_facebox.py * Update facebox.py * Update test_facebox.py * Update test_facebox.py * Remove fixtures Removes the fixtures which were causing `setup` to fail, replace with `@patch` * Fix teach service test and lint issues
* Adds service * Address pylint * Update facebox.py * patch tests * Update facebox.py * Update test_facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Update test_facebox.py * Update test_facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Adds total_matched_faces * Update test_facebox.py * Update facebox.py * Update test_facebox.py * Update test_facebox.py * Remove fixtures Removes the fixtures which were causing `setup` to fail, replace with `@patch` * Fix teach service test and lint issues
* Adds service * Address pylint * Update facebox.py * patch tests * Update facebox.py * Update test_facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Update test_facebox.py * Update test_facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Update facebox.py * Adds total_matched_faces * Update test_facebox.py * Update facebox.py * Update test_facebox.py * Update test_facebox.py * Remove fixtures Removes the fixtures which were causing `setup` to fail, replace with `@patch` * Fix teach service test and lint issues
Description:
This PR adds the service
image_processing.facebox_teach_facethat can be used to teach Facebox faces, as described in this blog post. Example valid service data is:An
image_processing.teach_classifierevent is fired for each service call, providing feedback on whether teaching has been successful or unsuccessful. In the unsuccessful case, themessagefield of the event_data will contain info on the cause of failure, and a warning is also published in the HA logs.Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5553
Checklist:
tox. Your PR cannot be merged unless tests passIf user exposed functionality or configuration variables are added/changed: