Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait for command to all PUT methods #771

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,018 changes: 549 additions & 469 deletions blinkpy/api.py

Large diffs are not rendered by default.

1,124 changes: 573 additions & 551 deletions blinkpy/camera.py

Large diffs are not rendered by default.

1,557 changes: 791 additions & 766 deletions blinkpy/sync_module.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/mock_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ def __init__(self, json_data, status_code, headers={}, raw_data=None):
async def json(self):
"""Return json data from get_request."""
return self.json_data

def get(self, name):
"""Return field for json."""
return self.json_data[name]
43 changes: 36 additions & 7 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from blinkpy.auth import Auth
import tests.mock_responses as mresp

COMMAND_RESPONSE = {"network_id": "12345", "id": "54321"}
COMMAND_COMPLETE = {"complete": True, "status_code": 908}
COMMAND_NOT_COMPLETE = {"complete": False, "status_code": 908}


@mock.patch("blinkpy.auth.Auth.query")
class TestAPI(IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -57,21 +61,27 @@ async def test_request_network_status(self, mock_resp):

async def test_request_command_status(self, mock_resp):
"""Test command_status."""
mock_resp.return_value = {"command": "done"}
mock_resp.side_effect = ({"command": "done"}, COMMAND_COMPLETE)
self.assertEqual(
await api.request_command_status(self.blink, "network", "command"),
{"command": "done"},
)

async def test_request_new_image(self, mock_resp):
"""Test api request new image."""
mock_resp.return_value = mresp.MockResponse({}, 200)
mock_resp.side_effect = (
mresp.MockResponse(COMMAND_RESPONSE, 200),
COMMAND_COMPLETE,
)
response = await api.request_new_image(self.blink, "network", "camera")
self.assertEqual(response.status, 200)

async def test_request_new_video(self, mock_resp):
"""Test api request new Video."""
mock_resp.return_value = mresp.MockResponse({}, 200)
mock_resp.side_effect = (
mresp.MockResponse(COMMAND_RESPONSE, 200),
COMMAND_COMPLETE,
)
response = await api.request_new_video(self.blink, "network", "camera")
self.assertEqual(response.status, 200)

Expand All @@ -97,23 +107,32 @@ async def test_request_camera_usage(self, mock_resp):

async def test_request_motion_detection_enable(self, mock_resp):
"""Test Motion detect enable."""
mock_resp.return_value = mresp.MockResponse({}, 200)
mock_resp.side_effect = (
mresp.MockResponse(COMMAND_RESPONSE, 200),
COMMAND_COMPLETE,
)
response = await api.request_motion_detection_enable(
self.blink, "network", "camera"
)
self.assertEqual(response.status, 200)

async def test_request_motion_detection_disable(self, mock_resp):
"""Test Motion detect enable."""
mock_resp.return_value = mresp.MockResponse({}, 200)
mock_resp.side_effect = (
mresp.MockResponse(COMMAND_RESPONSE, 200),
COMMAND_COMPLETE,
)
response = await api.request_motion_detection_disable(
self.blink, "network", "camera"
)
self.assertEqual(response.status, 200)

async def test_request_local_storage_clip(self, mock_resp):
"""Test Motion detect enable."""
mock_resp.return_value = mresp.MockResponse({}, 200)
mock_resp.side_effect = (
mresp.MockResponse(COMMAND_RESPONSE, 200),
COMMAND_COMPLETE,
)
response = await api.request_local_storage_clip(
self.blink, "network", "sync_id", "manifest_id", "clip_id"
)
Expand All @@ -135,7 +154,7 @@ async def test_request_get_config(self, mock_resp):

async def test_request_update_config(self, mock_resp):
"""Test Motion detect enable."""
mock_resp.return_value = mresp.MockResponse({}, 200)
mock_resp.return_value = mresp.MockResponse(COMMAND_RESPONSE, 200)
response = await api.request_update_config(
self.blink, "network", "camera_id", "owl"
)
Expand All @@ -149,3 +168,13 @@ async def test_request_update_config(self, mock_resp):
self.blink, "network", "camera_id", "other_camera"
)
)

async def test_wait_for_command(self, mock_resp):
"""Test Motion detect enable."""
mock_resp.side_effect = (COMMAND_NOT_COMPLETE, COMMAND_COMPLETE)
response = await api.wait_for_command(self.blink, COMMAND_RESPONSE)
assert response

mock_resp.side_effect = (COMMAND_NOT_COMPLETE, {})
response = await api.wait_for_command(self.blink, COMMAND_RESPONSE)
self.assertFalse(response)
6 changes: 5 additions & 1 deletion tests/test_camera_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
]
}

COMMAND_RESPONSE = {"network_id": "12345", "id": "54321"}
COMMAND_COMPLETE = {"complete": True, "status_code": 908}
COMMAND_NOT_COMPLETE = {"complete": False, "status_code": 908}


@mock.patch("blinkpy.auth.Auth.query")
class TestBlinkCameraSetup(IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -154,7 +158,7 @@ async def test_no_video_clips(self, mock_resp):
mock_resp.return_value = mresp.MockResponse({"test": 200}, 200, raw_data="")
self.camera.sync.homescreen = {"devices": []}
await self.camera.update(config, force_cache=True, expire_clips=False)
self.assertEqual(self.camera.clip, None)
self.assertEqual(self.camera.clip, "")
self.assertEqual(self.camera.video_from_cache, None)

async def test_recent_video_clips(self, mock_resp):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,23 @@ def test_mini_missing_attributes(self, mock_resp):
self.blink.sync.network_id = None
self.blink.sync.name = None
attr = camera.attributes
for key in attr:
for key, value in attr.items():
if key == "recent_clips":
self.assertEqual(attr[key], [])
self.assertEqual(value, [])
continue
self.assertEqual(attr[key], None)
self.assertIn(value, ("", None, False))

def test_doorbell_missing_attributes(self, mock_resp):
"""Test that attributes return None if missing."""
camera = BlinkDoorbell(self.blink.sync)
self.blink.sync.network_id = None
self.blink.sync.name = None
attr = camera.attributes
for key in attr:
for key, value in attr.items():
if key == "recent_clips":
self.assertEqual(attr[key], [])
self.assertEqual(value, [])
continue
self.assertEqual(attr[key], None)
self.assertIn(value, ("", None, False))

async def test_camera_stream(self, mock_resp):
"""Test that camera stream returns correct url."""
Expand Down
35 changes: 23 additions & 12 deletions tests/test_sync_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from tests.test_blink_functions import MockCamera
import tests.mock_responses as mresp

COMMAND_RESPONSE = {"network_id": "12345", "id": "54321"}
COMMAND_COMPLETE = {"complete": True, "status_code": 908}
COMMAND_NOT_COMPLETE = {"complete": False, "status_code": 908}

@mock.patch("blinkpy.auth.Auth.query")
class TestBlinkSyncModule(IsolatedAsyncioTestCase):
Expand Down Expand Up @@ -123,12 +126,12 @@ async def test_get_network_info(self, mock_resp) -> None:

async def test_get_network_info_failure(self, mock_resp) -> None:
"""Test failed network retrieval."""
mock_resp.return_value = {}
mock_resp.side_effect = (COMMAND_RESPONSE, COMMAND_COMPLETE)
self.blink.sync["test"].available = True
self.assertFalse(await self.blink.sync["test"].get_network_info())
self.assertFalse(self.blink.sync["test"].available)
self.blink.sync["test"].available = True
mock_resp.return_value = None
mock_resp.side_effect = None
self.assertFalse(await self.blink.sync["test"].get_network_info())
self.assertFalse(self.blink.sync["test"].available)

Expand Down Expand Up @@ -238,7 +241,8 @@ async def test_update_local_storage_manifest(self, mock_resp) -> None:
test_sync._local_storage["status"] = True
test_sync.sync_id = 1234
mock_resp.side_effect = [
{"id": 387372591, "network_id": 123456},
COMMAND_RESPONSE,
COMMAND_COMPLETE,
{
"version": "1.0",
"manifest_id": "4321",
Expand Down Expand Up @@ -305,7 +309,8 @@ async def test_check_new_videos_with_local_storage(self, mock_resp) -> None:
datetime.datetime.utcnow() - datetime.timedelta(seconds=60)
).isoformat()
mock_resp.side_effect = [
{"id": 387372591, "network_id": 123456},
COMMAND_RESPONSE,
COMMAND_COMPLETE,
{
"version": "1.0",
"manifest_id": "4321",
Expand All @@ -325,11 +330,15 @@ async def test_check_new_videos_with_local_storage(self, mock_resp) -> None:
],
},
{"media": []},
{"id": 489371591, "network_id": 123456},
{"id": 489371592, "network_id": 123456},
COMMAND_RESPONSE,
COMMAND_COMPLETE,
COMMAND_RESPONSE,
COMMAND_COMPLETE,
{"media": []},
{"id": 489371592, "network_id": 123456},
{"id": 489371592, "network_id": 123456},
COMMAND_RESPONSE,
COMMAND_COMPLETE,
COMMAND_RESPONSE,
COMMAND_COMPLETE,
]

test_sync._names_table[to_alphanumeric("Front_Door")] = "Front_Door"
Expand Down Expand Up @@ -385,7 +394,8 @@ async def test_check_missing_manifest_id_with_update_local_storage_manifest(
).isoformat()

mock_resp.side_effect = [
{"id": 387372591, "network_id": 123456},
COMMAND_RESPONSE,
COMMAND_COMPLETE,
{
"version": "1.0",
"clips": [
Expand Down Expand Up @@ -426,7 +436,8 @@ async def test_check_malformed_clips_with_update_local_storage_manifest(
).isoformat()

mock_resp.side_effect = [
{"id": 489371591, "network_id": 123456},
COMMAND_RESPONSE,
COMMAND_COMPLETE,
{
"version": "1.0",
"manifest_id": "4321",
Expand Down Expand Up @@ -483,7 +494,7 @@ async def test_sync_owl_init(self, mock_resp):
self.assertIsNotNone(test.serial)

self.blink.homescreen = {"owls": {"enabled": True}}
self.assertIsNone(await test.get_camera_info("test"))
self.assertEqual(await test.get_camera_info("test"),{})

async def test_sync_lotus_init(self, mock_resp):
"""Test sync lotus setup with no serial in response."""
Expand All @@ -496,7 +507,7 @@ async def test_sync_lotus_init(self, mock_resp):
self.assertIsNotNone(test.serial)

self.blink.homescreen = {"doorbells": {"enabled": True}}
self.assertIsNone(await test.get_camera_info("test"))
self.assertEqual(await test.get_camera_info("test"),{})

async def test_local_storage_media_item(self, mock_resp):
"""Test local storage media properties."""
Expand Down