From d11777b98641e0c0d92ad82f1d8c85edb1ed9af0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Mar 2020 23:51:04 +0000 Subject: [PATCH] Add additional details to the Lock activity * Lock operation was remote * Lock operation was done via keypad * Lock operation done by autorelock * Lock operator image url * Lock operator thumbnail url --- august/activity.py | 38 +++++++++++ setup.py | 2 +- tests/fixtures/auto_relock_activity.json | 38 +++++++++++ tests/fixtures/auto_unlock_activity.json | 38 +++++++++++ tests/fixtures/bluetooth_lock_activity.json | 52 +++++++++++++++ tests/fixtures/keypad_lock_activity.json | 38 +++++++++++ tests/fixtures/remote_lock_activity.json | 37 +++++++++++ tests/test_activity.py | 73 +++++++++++++++++++++ 8 files changed, 315 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/auto_relock_activity.json create mode 100644 tests/fixtures/auto_unlock_activity.json create mode 100644 tests/fixtures/bluetooth_lock_activity.json create mode 100644 tests/fixtures/keypad_lock_activity.json create mode 100644 tests/fixtures/remote_lock_activity.json diff --git a/august/activity.py b/august/activity.py index ac44968..9e1af15 100644 --- a/august/activity.py +++ b/august/activity.py @@ -167,14 +167,52 @@ def __init__(self, data): super().__init__(ActivityType.LOCK_OPERATION, data) calling_user = data.get("callingUser", {}) + + info = data.get("info", {}) + self._operated_remote = info.get("remote", False) + self._operated_keypad = info.get("keypad", False) + self._operated_autorelock = calling_user.get("UserID") == "automaticrelock" self._operated_by = "{} {}".format( calling_user.get("FirstName"), calling_user.get("LastName"), ) + image_info = calling_user.get("imageInfo", {}) + self._operator_image_url = image_info.get("original", {}).get( + "secure_url", None + ) + self._operator_thumbnail_url = image_info.get("thumbnail", {}).get( + "secure_url", None + ) + @property def operated_by(self): return self._operated_by + @property + def operated_remote(self): + """Operation was remote.""" + return self._operated_remote + + @property + def operated_keypad(self): + """Operation used keypad.""" + return self._operated_keypad + + @property + def operated_autorelock(self): + """Operation done by automatic relock.""" + return self._operated_autorelock + + @property + def operator_image_url(self): + """URL to the image of the lock operator.""" + return self._operator_image_url + + @property + def operator_thumbnail_url(self): + """URL to the thumbnail of the lock operator.""" + return self._operator_thumbnail_url + class DoorOperationActivity(Activity): def __init__(self, data): diff --git a/setup.py b/setup.py index 8105b74..40bea82 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="py-august", - version="0.23.0", + version="0.24.0", packages=["august"], url="https://github.com/snjoetw/py-august", license="MIT", diff --git a/tests/fixtures/auto_relock_activity.json b/tests/fixtures/auto_relock_activity.json new file mode 100644 index 0000000..de06ef1 --- /dev/null +++ b/tests/fixtures/auto_relock_activity.json @@ -0,0 +1,38 @@ +{ + "entities": { + "device": "deviceid", + "callingUser": "callinguser", + "otherUser": "deleted", + "house": "houseid", + "activity": "activityid" + }, + "house": { + "houseID": "house", + "houseName": "namehouse" + }, + "eventID": "eventid", + "dateTime": 1583535312002, + "action": "lock", + "deviceName": "lockname", + "deviceID": "deviceid", + "deviceType": "lock", + "callingUser": { + "FirstName": "I have no", + "LastName": "picture", + "UserID": "automaticrelock" + }, + "otherUser": { + "UserID": "deleted", + "FirstName": "Unknown", + "LastName": "User", + "UserName": "deleteduser", + "PhoneNo": "deleted" + }, + "info": { + "mechanical": "success", + "crypto": "success", + "dateTime": "2020-03-06T22:55:12.002Z", + "action": "lock", + "DateLogActionID": "uni" + } +} diff --git a/tests/fixtures/auto_unlock_activity.json b/tests/fixtures/auto_unlock_activity.json new file mode 100644 index 0000000..ed602d8 --- /dev/null +++ b/tests/fixtures/auto_unlock_activity.json @@ -0,0 +1,38 @@ +{ + "entities": { + "device": "deviceid", + "callingUser": "callinguser", + "otherUser": "deleted", + "house": "houseid", + "activity": "activityid" + }, + "house": { + "houseID": "house", + "houseName": "namehouse" + }, + "eventID": "eventid", + "dateTime": 1583535312002, + "action": "unlock", + "deviceName": "lockname", + "deviceID": "deviceid", + "deviceType": "lock", + "callingUser": { + "UserID": "userid", + "FirstName": "My", + "LastName": "Name" + }, + "otherUser": { + "UserID": "deleted", + "FirstName": "Unknown", + "LastName": "User", + "UserName": "deleteduser", + "PhoneNo": "deleted" + }, + "info": { + "mechanical": "success", + "crypto": "success", + "dateTime": "2020-03-06T22:55:12.002Z", + "action": "unlock", + "DateLogActionID": "uni" + } +} diff --git a/tests/fixtures/bluetooth_lock_activity.json b/tests/fixtures/bluetooth_lock_activity.json new file mode 100644 index 0000000..4f433d0 --- /dev/null +++ b/tests/fixtures/bluetooth_lock_activity.json @@ -0,0 +1,52 @@ +{ + "entities": { + "device": "deviceid", + "callingUser": "callinguser", + "otherUser": "deleted", + "house": "houseid", + "activity": "activityid" + }, + "house": { + "houseID": "house", + "houseName": "namehouse" + }, + "eventID": "eventid", + "dateTime": 1583535312002, + "action": "lock", + "deviceName": "lockname", + "deviceID": "deviceid", + "deviceType": "lock", + "callingUser": { + "UserID": "myuser", + "FirstName": "I have a", + "LastName": "picture", + "imageInfo": { + "original": { + "width": 400, + "height": 400, + "format": "jpg", + "secure_url": "https://image.url" + }, + "thumbnail": { + "width": 128, + "height": 128, + "format": "jpg", + "secure_url": "https://thumbnail.url" + } + } + }, + "otherUser": { + "UserID": "deleted", + "FirstName": "Unknown", + "LastName": "User", + "UserName": "deleteduser", + "PhoneNo": "deleted" + }, + "info": { + "mechanical": "success", + "crypto": "success", + "dateTime": "2020-03-06T22:55:12.002Z", + "action": "lock", + "DateLogActionID": "uni" + } +} diff --git a/tests/fixtures/keypad_lock_activity.json b/tests/fixtures/keypad_lock_activity.json new file mode 100644 index 0000000..84384ce --- /dev/null +++ b/tests/fixtures/keypad_lock_activity.json @@ -0,0 +1,38 @@ +{ + "entities": { + "device": "deviceid", + "callingUser": "callinguser", + "otherUser": "deleted", + "house": "houseid", + "activity": "activityid" + }, + "house": { + "houseID": "house", + "houseName": "namehouse" + }, + "source": { + "sourceType": "mercury" + }, + "eventID": "eventid", + "dateTime": 1583535312002, + "action": "lock", + "deviceName": "lockname", + "deviceID": "deviceid", + "deviceType": "lock", + "callingUser": { + "UserID": "userid", + "FirstName": "My", + "LastName": "Name" + }, + "otherUser": { + "UserID": "deleted", + "FirstName": "Unknown", + "LastName": "User", + "UserName": "deleteduser", + "PhoneNo": "deleted" + }, + "info": { + "agent": "mercury", + "keypad": true + } +} diff --git a/tests/fixtures/remote_lock_activity.json b/tests/fixtures/remote_lock_activity.json new file mode 100644 index 0000000..dff5056 --- /dev/null +++ b/tests/fixtures/remote_lock_activity.json @@ -0,0 +1,37 @@ +{ + "entities": { + "device": "deviceid", + "callingUser": "callinguser", + "otherUser": "deleted", + "house": "houseid", + "activity": "activityid" + }, + "house": { + "houseID": "house", + "houseName": "namehouse" + }, + "source": { + "sourceType": "mercury" + }, + "eventID": "eventid", + "dateTime": 1583535312002, + "action": "lock", + "deviceName": "lockname", + "deviceID": "deviceid", + "deviceType": "lock", + "callingUser": { + "UserID": "userid", + "FirstName": "My", + "LastName": "Name" + }, + "otherUser": { + "UserID": "deleted", + "FirstName": "Unknown", + "LastName": "User", + "UserName": "deleteduser", + "PhoneNo": "deleted" + }, + "info": { + "remote": true + } +} diff --git a/tests/test_activity.py b/tests/test_activity.py index 2db834d..2923d04 100644 --- a/tests/test_activity.py +++ b/tests/test_activity.py @@ -1,4 +1,6 @@ +import json import unittest +import os from august.activity import ( ACTION_DOOR_CLOSED, @@ -16,10 +18,18 @@ ACTIVITY_ACTIONS_DOORBELL_MOTION, ACTIVITY_ACTIONS_DOORBELL_VIEW, ACTIVITY_ACTIONS_LOCK_OPERATION, + LockOperationActivity, ) from august.lock import LockDoorStatus, LockStatus +def load_fixture(filename): + """Load a fixture.""" + path = os.path.join(os.path.dirname(__file__), "fixtures", filename) + with open(path) as fptr: + return fptr.read() + + class TestActivity(unittest.TestCase): def test_activity_action_states(self): self.assertIs( @@ -48,3 +58,66 @@ def test_activity_actions(self): self.assertCountEqual( ACTIVITY_ACTIONS_DOOR_OPERATION, [ACTION_DOOR_OPEN, ACTION_DOOR_CLOSED] ) + + def test_auto_unlock_activity(self): + auto_unlock_activity = LockOperationActivity( + json.loads(load_fixture("auto_unlock_activity.json")) + ) + assert auto_unlock_activity.operated_by == "My Name" + assert auto_unlock_activity.operated_remote is False + assert auto_unlock_activity.operated_keypad is False + + def test_bluetooth_lock_activity(self): + bluetooth_lock_activity = LockOperationActivity( + json.loads(load_fixture("bluetooth_lock_activity.json")) + ) + assert bluetooth_lock_activity.operated_by == "I have a picture" + assert bluetooth_lock_activity.operated_remote is False + assert bluetooth_lock_activity.operated_keypad is False + assert bluetooth_lock_activity.operator_image_url == "https://image.url" + assert bluetooth_lock_activity.operator_thumbnail_url == "https://thumbnail.url" + + def test_keypad_lock_activity(self): + keypad_lock_activity = LockOperationActivity( + json.loads(load_fixture("keypad_lock_activity.json")) + ) + assert keypad_lock_activity.operated_by == "My Name" + assert keypad_lock_activity.operated_remote is False + assert keypad_lock_activity.operated_keypad is True + + def test_remote_lock_activity(self): + remote_lock_activity = LockOperationActivity( + json.loads(load_fixture("remote_lock_activity.json")) + ) + assert remote_lock_activity.operated_by == "My Name" + assert remote_lock_activity.operated_remote is True + assert remote_lock_activity.operated_keypad is False + + def test_lock_activity(self): + lock_operation_activity = LockOperationActivity( + json.loads(load_fixture("lock_activity.json")) + ) + assert lock_operation_activity.operated_by == "MockHouse House" + assert lock_operation_activity.operated_remote is True + assert lock_operation_activity.operated_keypad is False + assert lock_operation_activity.operated_autorelock is False + + def test_unlock_activity(self): + unlock_operation_activity = LockOperationActivity( + json.loads(load_fixture("unlock_activity.json")) + ) + assert unlock_operation_activity.operated_by == "MockHouse House" + assert unlock_operation_activity.operated_keypad is False + assert unlock_operation_activity.operated_remote is True + assert unlock_operation_activity.operator_image_url is None + assert unlock_operation_activity.operated_autorelock is False + assert unlock_operation_activity.operator_thumbnail_url is None + + def test_autorelock_activity(self): + auto_relock_operation_activity = LockOperationActivity( + json.loads(load_fixture("auto_relock_activity.json")) + ) + assert auto_relock_operation_activity.operated_by == "I have no picture" + assert auto_relock_operation_activity.operated_remote is False + assert auto_relock_operation_activity.operated_autorelock is True + assert auto_relock_operation_activity.operated_keypad is False