-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from bdraco/update_lock_detail_with_activity
Update lock detail with activity
- Loading branch information
Showing
10 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ august/exceptions.py | |
august/keypad.py | ||
august/lock.py | ||
august/pin.py | ||
august/util.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import datetime | ||
|
||
from august.activity import ( | ||
ACTIVITY_ACTION_STATES, | ||
DoorOperationActivity, | ||
LockOperationActivity, | ||
) | ||
|
||
|
||
def update_lock_detail_from_activity(lock_detail, activity): | ||
"""Update the LockDetail from an activity.""" | ||
activity_end_time_utc = as_utc_from_local(activity.activity_end_time) | ||
if activity.house_id != lock_detail.house_id: | ||
raise ValueError | ||
if activity.device_id != lock_detail.device_id: | ||
raise ValueError | ||
if isinstance(activity, LockOperationActivity): | ||
if lock_detail.lock_status_datetime >= activity_end_time_utc: | ||
return False | ||
lock_detail.lock_status = ACTIVITY_ACTION_STATES[activity.action] | ||
lock_detail.lock_status_datetime = activity_end_time_utc | ||
elif isinstance(activity, DoorOperationActivity): | ||
if lock_detail.door_state_datetime >= activity_end_time_utc: | ||
return False | ||
lock_detail.door_state = ACTIVITY_ACTION_STATES[activity.action] | ||
lock_detail.door_state_datetime = activity_end_time_utc | ||
else: | ||
raise ValueError | ||
|
||
return True | ||
|
||
|
||
def as_utc_from_local(dtime): | ||
"""Converts the datetime returned from an activity to UTC.""" | ||
return dtime.astimezone(tz=datetime.timezone.utc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"action" : "doorclosed", | ||
"callingUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
}, | ||
"dateTime" : 1582007217000, | ||
"deviceID" : "ABC", | ||
"deviceName" : "MockHouse Tech Room Door", | ||
"deviceType" : "lock", | ||
"entities" : { | ||
"activity" : "activityId", | ||
"callingUser" : "deleted", | ||
"device" : "ABC", | ||
"house" : "123", | ||
"otherUser" : "deleted" | ||
}, | ||
"house" : { | ||
"houseID" : "123", | ||
"houseName" : "MockHouse" | ||
}, | ||
"info" : { | ||
"DateLogActionID" : "ABC+Time" | ||
}, | ||
"otherUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"action" : "doorclosed", | ||
"callingUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
}, | ||
"dateTime" : 1582007217000, | ||
"deviceID" : "notABC", | ||
"deviceName" : "MockHouse Tech Room Door", | ||
"deviceType" : "lock", | ||
"entities" : { | ||
"activity" : "activityId", | ||
"callingUser" : "deleted", | ||
"device" : "ABC", | ||
"house" : "123", | ||
"otherUser" : "deleted" | ||
}, | ||
"house" : { | ||
"houseID" : "123", | ||
"houseName" : "MockHouse" | ||
}, | ||
"info" : { | ||
"DateLogActionID" : "ABC+Time" | ||
}, | ||
"otherUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"action" : "doorclosed", | ||
"callingUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
}, | ||
"dateTime" : 1582007217000, | ||
"deviceID" : "ABC", | ||
"deviceName" : "MockHouse Tech Room Door", | ||
"deviceType" : "lock", | ||
"entities" : { | ||
"activity" : "activityId", | ||
"callingUser" : "deleted", | ||
"device" : "ABC", | ||
"house" : "not123", | ||
"otherUser" : "deleted" | ||
}, | ||
"house" : { | ||
"houseID" : "not123", | ||
"houseName" : "MockHouse" | ||
}, | ||
"info" : { | ||
"DateLogActionID" : "ABC+Time" | ||
}, | ||
"otherUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"action" : "dooropen", | ||
"callingUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
}, | ||
"dateTime" : 1582007219000, | ||
"deviceID" : "ABC", | ||
"deviceName" : "MockHouse Tech Room Door", | ||
"deviceType" : "lock", | ||
"entities" : { | ||
"activity" : "ActivityId", | ||
"callingUser" : "deleted", | ||
"device" : "ABC", | ||
"house" : "123", | ||
"otherUser" : "deleted" | ||
}, | ||
"house" : { | ||
"houseID" : "123", | ||
"houseName" : "MockHouse" | ||
}, | ||
"info" : { | ||
"DateLogActionID" : "ABC+Time" | ||
}, | ||
"otherUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"action" : "lock", | ||
"callingUser" : { | ||
"FirstName" : "MockHouse", | ||
"LastName" : "House", | ||
"UserID" : "mockUserId2" | ||
}, | ||
"dateTime" : 1582007218000, | ||
"deviceID" : "ABC", | ||
"deviceName" : "MockHouseTDoor", | ||
"deviceType" : "lock", | ||
"entities" : { | ||
"activity" : "mockActivity2", | ||
"callingUser" : "mockUserId2", | ||
"device" : "ABC", | ||
"house" : "123", | ||
"otherUser" : "deleted" | ||
}, | ||
"house" : { | ||
"houseID" : "123", | ||
"houseName" : "MockHouse" | ||
}, | ||
"info" : { | ||
"DateLogActionID" : "ABC+Time", | ||
"remote" : true | ||
}, | ||
"otherUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"action" : "unlock", | ||
"callingUser" : { | ||
"FirstName" : "MockHouse", | ||
"LastName" : "House", | ||
"UserID" : "mockUserId2" | ||
}, | ||
"dateTime" : 1582007217000, | ||
"deviceID" : "ABC", | ||
"deviceName" : "MockHouseXDoor", | ||
"deviceType" : "lock", | ||
"entities" : { | ||
"activity" : "ActivityId", | ||
"callingUser" : "mockUserId2", | ||
"device" : "ABC", | ||
"house" : "123", | ||
"otherUser" : "deleted" | ||
}, | ||
"house" : { | ||
"houseID" : "123", | ||
"houseName" : "MockHouse" | ||
}, | ||
"info" : { | ||
"DateLogActionID" : "ABC+Time", | ||
"remote" : true | ||
}, | ||
"otherUser" : { | ||
"FirstName" : "Unknown", | ||
"LastName" : "User", | ||
"PhoneNo" : "deleted", | ||
"UserID" : "deleted", | ||
"UserName" : "deleteduser" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import json | ||
import os | ||
import unittest | ||
|
||
import dateutil.parser | ||
import datetime | ||
|
||
from august.activity import DoorOperationActivity, LockOperationActivity | ||
from august.lock import LockDetail, LockDoorStatus, LockStatus | ||
from august.util import update_lock_detail_from_activity, as_utc_from_local | ||
|
||
|
||
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 TestLockDetail(unittest.TestCase): | ||
def test_update_lock_with_activity(self): | ||
lock = LockDetail( | ||
json.loads(load_fixture("get_lock.online_with_doorsense.json")) | ||
) | ||
self.assertEqual("ABC", lock.device_id) | ||
self.assertEqual(LockStatus.LOCKED, lock.lock_status) | ||
self.assertEqual(LockDoorStatus.OPEN, lock.door_state) | ||
self.assertEqual( | ||
dateutil.parser.parse("2017-12-10T04:48:30.272Z"), lock.lock_status_datetime | ||
) | ||
self.assertEqual( | ||
dateutil.parser.parse("2017-12-10T04:48:30.272Z"), lock.door_state_datetime | ||
) | ||
|
||
lock_operation_activity = LockOperationActivity( | ||
json.loads(load_fixture("lock_activity.json")) | ||
) | ||
unlock_operation_activity = LockOperationActivity( | ||
json.loads(load_fixture("unlock_activity.json")) | ||
) | ||
open_operation_activity = DoorOperationActivity( | ||
json.loads(load_fixture("door_open_activity.json")) | ||
) | ||
closed_operation_activity = DoorOperationActivity( | ||
json.loads(load_fixture("door_closed_activity.json")) | ||
) | ||
closed_operation_wrong_deviceid_activity = DoorOperationActivity( | ||
json.loads(load_fixture("door_closed_activity_wrong_deviceid.json")) | ||
) | ||
closed_operation_wrong_houseid_activity = DoorOperationActivity( | ||
json.loads(load_fixture("door_closed_activity_wrong_houseid.json")) | ||
) | ||
|
||
self.assertTrue( | ||
update_lock_detail_from_activity(lock, unlock_operation_activity) | ||
) | ||
self.assertEqual(LockStatus.UNLOCKED, lock.lock_status) | ||
self.assertEqual( | ||
as_utc_from_local(datetime.datetime.fromtimestamp(1582007217000 / 1000)), | ||
lock.lock_status_datetime, | ||
) | ||
|
||
self.assertTrue(update_lock_detail_from_activity(lock, lock_operation_activity)) | ||
self.assertEqual(LockStatus.LOCKED, lock.lock_status) | ||
self.assertEqual( | ||
as_utc_from_local(datetime.datetime.fromtimestamp(1582007218000 / 1000)), | ||
lock.lock_status_datetime, | ||
) | ||
|
||
# returns false we send an older activity | ||
self.assertFalse( | ||
update_lock_detail_from_activity(lock, unlock_operation_activity) | ||
) | ||
|
||
self.assertTrue( | ||
update_lock_detail_from_activity(lock, closed_operation_activity) | ||
) | ||
self.assertEqual(LockDoorStatus.CLOSED, lock.door_state) | ||
self.assertEqual( | ||
as_utc_from_local(datetime.datetime.fromtimestamp(1582007217000 / 1000)), | ||
lock.door_state_datetime, | ||
) | ||
|
||
self.assertTrue(update_lock_detail_from_activity(lock, open_operation_activity)) | ||
self.assertEqual(LockDoorStatus.OPEN, lock.door_state) | ||
self.assertEqual( | ||
as_utc_from_local(datetime.datetime.fromtimestamp(1582007219000 / 1000)), | ||
lock.door_state_datetime, | ||
) | ||
|
||
# returns false we send an older activity | ||
self.assertFalse( | ||
update_lock_detail_from_activity(lock, closed_operation_activity) | ||
) | ||
|
||
with self.assertRaises(ValueError): | ||
update_lock_detail_from_activity( | ||
lock, closed_operation_wrong_deviceid_activity | ||
) | ||
|
||
with self.assertRaises(ValueError): | ||
update_lock_detail_from_activity( | ||
lock, closed_operation_wrong_houseid_activity | ||
) |