def _mock_door_operation_activity(lock, action, offset): return DoorOperationActivity( SOURCE_LOCK_OPERATE, { "dateTime": (time.time() + offset) * 1000, "deviceID": lock.device_id, "deviceType": "lock", "action": action, }, )
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