def test_remote_lock_activity(self): remote_lock_activity = LockOperationActivity( SOURCE_LOG, 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_auto_unlock_activity(self): auto_unlock_activity = LockOperationActivity( SOURCE_LOG, json.loads(load_fixture("auto_unlock_activity.json")) ) assert auto_unlock_activity.activity_type == ActivityType.LOCK_OPERATION 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_autorelock_activity(self): auto_relock_operation_activity = LockOperationActivity( SOURCE_LOG, 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
def test_lock_activity(self): lock_operation_activity = LockOperationActivity( SOURCE_LOG, 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_bluetooth_lock_activity(self): bluetooth_lock_activity = LockOperationActivity( SOURCE_LOG, 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_unlock_activity(self): unlock_operation_activity = LockOperationActivity( SOURCE_LOG, 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 _mock_lock_operation_activity(lock, action, offset): return LockOperationActivity( SOURCE_LOCK_OPERATE, { "dateTime": (time.time() + offset) * 1000, "deviceID": lock.device_id, "deviceType": "lock", "action": action, }, )
def test_update_lock_with_activity_has_no_status(self): lock = LockDetail( json.loads(load_fixture("get_lock.nostatus_with_doorsense.json"))) self.assertEqual("ABC", lock.device_id) self.assertEqual(LockStatus.UNKNOWN, lock.lock_status) self.assertEqual(LockDoorStatus.UNKNOWN, lock.door_state) self.assertEqual(None, lock.lock_status_datetime) self.assertEqual(None, lock.door_state_datetime) unlock_operation_activity = LockOperationActivity( SOURCE_LOG, json.loads(load_fixture("unlock_activity.json"))) self.assertTrue( update_lock_detail_from_activity(lock, unlock_operation_activity)) self.assertEqual(LockStatus.UNLOCKED, lock.lock_status)
def _activity_from_dict(activity_dict): action = activity_dict.get("action") activity_dict["dateTime"] = time.time() * 1000 if action in ACTIVITY_ACTIONS_DOORBELL_DING: return DoorbellDingActivity(SOURCE_LOG, activity_dict) if action in ACTIVITY_ACTIONS_DOORBELL_MOTION: return DoorbellMotionActivity(SOURCE_LOG, activity_dict) if action in ACTIVITY_ACTIONS_DOORBELL_VIEW: return DoorbellViewActivity(SOURCE_LOG, activity_dict) if action in ACTIVITY_ACTIONS_LOCK_OPERATION: return LockOperationActivity(SOURCE_LOG, activity_dict) if action in ACTIVITY_ACTIONS_DOOR_OPERATION: return DoorOperationActivity(SOURCE_LOG, activity_dict) if action in ACTIVITY_ACTIONS_BRIDGE_OPERATION: return BridgeOperationActivity(SOURCE_LOG, activity_dict) return None
def _activity_from_dict(source, activity_dict): action = activity_dict.get("action") if action in ACTIVITY_ACTIONS_DOORBELL_DING: return DoorbellDingActivity(source, activity_dict) if action in ACTIVITY_ACTIONS_DOORBELL_MOTION: return DoorbellMotionActivity(source, activity_dict) if action in ACTIVITY_ACTIONS_DOORBELL_IMAGE_CAPTURE: return DoorbellImageCaptureActivity(source, activity_dict) if action in ACTIVITY_ACTIONS_DOORBELL_VIEW: return DoorbellViewActivity(source, activity_dict) if action in ACTIVITY_ACTIONS_LOCK_OPERATION: return LockOperationActivity(source, activity_dict) if action in ACTIVITY_ACTIONS_DOOR_OPERATION: return DoorOperationActivity(source, activity_dict) if action in ACTIVITY_ACTIONS_BRIDGE_OPERATION: return BridgeOperationActivity(source, activity_dict) return None
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( SOURCE_LOG, json.loads(load_fixture("lock_activity.json"))) unlock_operation_activity = LockOperationActivity( SOURCE_LOG, json.loads(load_fixture("unlock_activity.json"))) open_operation_activity = DoorOperationActivity( SOURCE_LOG, json.loads(load_fixture("door_open_activity.json"))) closed_operation_activity = DoorOperationActivity( SOURCE_LOG, json.loads(load_fixture("door_closed_activity.json"))) closed_operation_wrong_deviceid_activity = DoorOperationActivity( SOURCE_LOG, json.loads( load_fixture("door_closed_activity_wrong_deviceid.json")), ) closed_operation_wrong_houseid_activity = DoorOperationActivity( SOURCE_LOG, 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) # We do not always have the houseid so we do not throw # as long as the deviceid is correct since they are unique self.assertFalse( update_lock_detail_from_activity( lock, closed_operation_wrong_houseid_activity)) self.assertEqual(LockDoorStatus.OPEN, lock.door_state) self.assertEqual(LockStatus.LOCKED, lock.lock_status) activities = _convert_lock_result_to_activities( json.loads(load_fixture("unlock.json"))) for activity in activities: self.assertTrue(update_lock_detail_from_activity(lock, activity)) self.assertEqual(LockDoorStatus.CLOSED, lock.door_state) self.assertEqual(LockStatus.UNLOCKED, lock.lock_status) bridge_offline_activity = BridgeOperationActivity( SOURCE_PUBNUB, { "action": "associated_bridge_offline", "callingUser": { "UserID": None }, "dateTime": 1512906510272.0, "deviceName": "Front Door Lock", "deviceType": "lock", "deviceID": lock.device_id, "house": "000000000000", "info": {}, }, ) assert bridge_offline_activity.source == SOURCE_PUBNUB self.assertTrue( update_lock_detail_from_activity(lock, bridge_offline_activity)) assert lock.bridge_is_online is False bridge_online_activity = BridgeOperationActivity( SOURCE_PUBNUB, { "action": "associated_bridge_online", "callingUser": { "UserID": None }, "dateTime": 1512906510272.0, "deviceName": "Front Door Lock", "deviceType": "lock", "deviceID": lock.device_id, "house": "000000000000", "info": {}, }, ) self.assertTrue( update_lock_detail_from_activity(lock, bridge_online_activity)) assert lock.bridge_is_online is True assert bridge_online_activity.source == SOURCE_PUBNUB