Add sighthound timestamped file (#32202)
* Update image_processing.py Adds save timestamp file and adds last_detection attribute * Update test_image_processing.py Adds test * Adds assert pil_img.save.call_args * Test timestamp filename * Add test bad data * Update test_image_processing.py * Fix bad image data test * Update homeassistant/components/sighthound/image_processing.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
e416f17e4d
commit
3ca97a0517
2 files changed
+111
-9
No files matched your search
@@ -1,8 +1,11 @@
|
||||
"""Tests for the Sighthound integration."""
|
||||
from copy import deepcopy
|
||||
import datetime
|
||||
import os
|
||||
from unittest.mock import patch
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from PIL import UnidentifiedImageError
|
||||
import pytest
|
||||
import simplehound.core as hound
|
||||
|
||||
@@ -40,11 +43,13 @@ MOCK_DETECTIONS = {
|
||||
"requestId": "545cec700eac4d389743e2266264e84b",
|
||||
}
|
||||
|
||||
MOCK_NOW = datetime.datetime(2020, 2, 20, 10, 5, 3)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_detections():
|
||||
"""Return a mock detection."""
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"simplehound.core.cloud.detect", return_value=MOCK_DETECTIONS
|
||||
) as detection:
|
||||
yield detection
|
||||
@@ -53,16 +58,35 @@ def mock_detections():
|
||||
@pytest.fixture
|
||||
def mock_image():
|
||||
"""Return a mock camera image."""
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"homeassistant.components.demo.camera.DemoCamera.camera_image",
|
||||
return_value=b"Test",
|
||||
) as image:
|
||||
yield image
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_bad_image_data():
|
||||
"""Mock bad image data."""
|
||||
with mock.patch(
|
||||
"homeassistant.components.sighthound.image_processing.Image.open",
|
||||
side_effect=UnidentifiedImageError,
|
||||
) as bad_data:
|
||||
yield bad_data
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_now():
|
||||
"""Return a mock now datetime."""
|
||||
with mock.patch("homeassistant.util.dt.now", return_value=MOCK_NOW) as now_dt:
|
||||
yield now_dt
|
||||
|
||||
|
||||
async def test_bad_api_key(hass, caplog):
|
||||
"""Catch bad api key."""
|
||||
with patch("simplehound.core.cloud.detect", side_effect=hound.SimplehoundException):
|
||||
with mock.patch(
|
||||
"simplehound.core.cloud.detect", side_effect=hound.SimplehoundException
|
||||
):
|
||||
await async_setup_component(hass, ip.DOMAIN, VALID_CONFIG)
|
||||
assert "Sighthound error" in caplog.text
|
||||
assert not hass.states.get(VALID_ENTITY_ID)
|
||||
@@ -97,6 +121,21 @@ async def test_process_image(hass, mock_image, mock_detections):
|
||||
assert len(person_events) == 2
|
||||
|
||||
|
||||
async def test_catch_bad_image(
|
||||
hass, caplog, mock_image, mock_detections, mock_bad_image_data
|
||||
):
|
||||
"""Process an image."""
|
||||
valid_config_save_file = deepcopy(VALID_CONFIG)
|
||||
valid_config_save_file[ip.DOMAIN].update({sh.CONF_SAVE_FILE_FOLDER: TEST_DIR})
|
||||
await async_setup_component(hass, ip.DOMAIN, valid_config_save_file)
|
||||
assert hass.states.get(VALID_ENTITY_ID)
|
||||
|
||||
data = {ATTR_ENTITY_ID: VALID_ENTITY_ID}
|
||||
await hass.services.async_call(ip.DOMAIN, ip.SERVICE_SCAN, service_data=data)
|
||||
await hass.async_block_till_done()
|
||||
assert "Sighthound unable to process image" in caplog.text
|
||||
|
||||
|
||||
async def test_save_image(hass, mock_image, mock_detections):
|
||||
"""Save a processed image."""
|
||||
valid_config_save_file = deepcopy(VALID_CONFIG)
|
||||
@@ -104,7 +143,7 @@ async def test_save_image(hass, mock_image, mock_detections):
|
||||
await async_setup_component(hass, ip.DOMAIN, valid_config_save_file)
|
||||
assert hass.states.get(VALID_ENTITY_ID)
|
||||
|
||||
with patch(
|
||||
with mock.patch(
|
||||
"homeassistant.components.sighthound.image_processing.Image.open"
|
||||
) as pil_img_open:
|
||||
pil_img = pil_img_open.return_value
|
||||
@@ -115,3 +154,34 @@ async def test_save_image(hass, mock_image, mock_detections):
|
||||
state = hass.states.get(VALID_ENTITY_ID)
|
||||
assert state.state == "2"
|
||||
assert pil_img.save.call_count == 1
|
||||
|
||||
directory = Path(TEST_DIR)
|
||||
latest_save_path = directory / "sighthound_demo_camera_latest.jpg"
|
||||
assert pil_img.save.call_args_list[0] == mock.call(latest_save_path)
|
||||
|
||||
|
||||
async def test_save_timestamped_image(hass, mock_image, mock_detections, mock_now):
|
||||
"""Save a processed image."""
|
||||
valid_config_save_ts_file = deepcopy(VALID_CONFIG)
|
||||
valid_config_save_ts_file[ip.DOMAIN].update({sh.CONF_SAVE_FILE_FOLDER: TEST_DIR})
|
||||
valid_config_save_ts_file[ip.DOMAIN].update({sh.CONF_SAVE_TIMESTAMPTED_FILE: True})
|
||||
await async_setup_component(hass, ip.DOMAIN, valid_config_save_ts_file)
|
||||
assert hass.states.get(VALID_ENTITY_ID)
|
||||
|
||||
with mock.patch(
|
||||
"homeassistant.components.sighthound.image_processing.Image.open"
|
||||
) as pil_img_open:
|
||||
pil_img = pil_img_open.return_value
|
||||
pil_img = pil_img.convert.return_value
|
||||
data = {ATTR_ENTITY_ID: VALID_ENTITY_ID}
|
||||
await hass.services.async_call(ip.DOMAIN, ip.SERVICE_SCAN, service_data=data)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get(VALID_ENTITY_ID)
|
||||
assert state.state == "2"
|
||||
assert pil_img.save.call_count == 2
|
||||
|
||||
directory = Path(TEST_DIR)
|
||||
timestamp_save_path = (
|
||||
directory / "sighthound_demo_camera_2020-02-20_10:05:03.jpg"
|
||||
)
|
||||
assert pil_img.save.call_args_list[1] == mock.call(timestamp_save_path)
|
||||
Reference in new issue
Block a user