Skip to content

Commit

Permalink
Merge pull request #40 from bdraco/doorbell_image_updates_from_activity
Browse files Browse the repository at this point in the history
Add ability to update doorbell images from activity
  • Loading branch information
snjoetw authored Feb 22, 2020
2 parents f41a3a9 + e99e939 commit 860c2cc
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 16 deletions.
22 changes: 18 additions & 4 deletions august/activity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
from enum import Enum
import dateutil.parser

from august.lock import LockDoorStatus, LockStatus

Expand All @@ -13,10 +14,17 @@
ACTION_DOORBELL_CALL_MISSED = "doorbell_call_missed"
ACTION_DOORBELL_CALL_HANGUP = "doorbell_call_hangup"

ACTIVITY_ACTIONS_DOORBELL_DING = [ACTION_DOORBELL_CALL_MISSED, ACTION_DOORBELL_CALL_HANGUP]
ACTIVITY_ACTIONS_DOORBELL_DING = [
ACTION_DOORBELL_CALL_MISSED,
ACTION_DOORBELL_CALL_HANGUP,
]
ACTIVITY_ACTIONS_DOORBELL_MOTION = [ACTION_DOORBELL_MOTION_DETECTED]
ACTIVITY_ACTIONS_DOORBELL_VIEW = [ACTION_DOORBELL_CALL_INITIATED]
ACTIVITY_ACTIONS_LOCK_OPERATION = [ACTION_LOCK_LOCK, ACTION_LOCK_UNLOCK, ACTION_LOCK_ONETOUCHLOCK]
ACTIVITY_ACTIONS_LOCK_OPERATION = [
ACTION_LOCK_LOCK,
ACTION_LOCK_UNLOCK,
ACTION_LOCK_ONETOUCHLOCK,
]
ACTIVITY_ACTIONS_DOOR_OPERATION = [ACTION_DOOR_CLOSED, ACTION_DOOR_OPEN]

ACTIVITY_ACTION_STATES = {
Expand Down Expand Up @@ -97,11 +105,18 @@ def __init__(self, data):

image = data.get("info", {}).get("image")
self._image_url = None if image is None else image.get("secure_url")
self._image_created_at_datetime = None
if image is not None and "created_at" in image:
self._image_created_at_datetime = dateutil.parser.parse(image["created_at"])

@property
def image_url(self):
return self._image_url

@property
def image_created_at_datetime(self):
return self._image_created_at_datetime


class DoorbellDingActivity(Activity):
def __init__(self, data):
Expand Down Expand Up @@ -153,8 +168,7 @@ def __init__(self, data):

calling_user = data.get("callingUser", {})
self._operated_by = "{} {}".format(
calling_user.get("FirstName"),
calling_user.get("LastName"),
calling_user.get("FirstName"), calling_user.get("LastName"),
)

@property
Expand Down
34 changes: 34 additions & 0 deletions august/doorbell.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import datetime

import dateutil.parser

from august.device import Device, DeviceDetail


Expand All @@ -18,6 +22,10 @@ def serial_number(self):
def status(self):
return self._status

@property
def is_standby(self):
return self.status == "standby"

@property
def is_online(self):
return self.status == "doorbell_call_status_online"
Expand Down Expand Up @@ -50,6 +58,12 @@ def __init__(self, data):
recent_image = data.get("recentImage", {})
self._image_url = recent_image.get("secure_url", None)
self._has_subscription = data.get("dvrSubscriptionSetupDone", False)
self._image_created_at_datetime = None

if "created_at" in recent_image:
self._image_created_at_datetime = dateutil.parser.parse(
recent_image["created_at"]
)

self._battery_level = None
if "telemetry" in data:
Expand All @@ -63,10 +77,30 @@ def status(self):
def is_online(self):
return self.status == "doorbell_call_status_online"

@property
def is_standby(self):
return self.status == "standby"

@property
def image_created_at_datetime(self):
return self._image_created_at_datetime

@image_created_at_datetime.setter
def image_created_at_datetime(self, var):
"""Update the doorbell image created_at datetime (usually form the activity log)."""
if not isinstance(var, datetime.date):
raise ValueError
self._image_created_at_datetime = var

@property
def image_url(self):
return self._image_url

@image_url.setter
def image_url(self, var):
"""Update the doorbell image url (usually form the activity log)."""
self._image_url = var

@property
def battery_level(self):
return self._battery_level
Expand Down
24 changes: 24 additions & 0 deletions august/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from august.activity import (
ACTIVITY_ACTION_STATES,
DoorbellMotionActivity,
DoorOperationActivity,
LockOperationActivity,
)
Expand All @@ -28,6 +29,29 @@ def update_lock_detail_from_activity(lock_detail, activity):
return True


def update_doorbell_image_from_activity(doorbell_detail, activity):
"""Update the DoorDetail from an activity with a new image."""
if activity.device_id != doorbell_detail.device_id:
raise ValueError
if isinstance(activity, DoorbellMotionActivity):
if activity.image_created_at_datetime is None:
return False

if (doorbell_detail.image_created_at_datetime is None
or doorbell_detail.image_created_at_datetime
< activity.image_created_at_datetime):
doorbell_detail.image_url = activity.image_url
doorbell_detail.image_created_at_datetime = (
activity.image_created_at_datetime
)
else:
return False
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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='py-august',
version='0.17.0',
version='0.18.0',
packages=['august'],
url='https://github.com/snjoetw/py-august',
license='MIT',
Expand Down
56 changes: 56 additions & 0 deletions tests/fixtures/doorbell_motion_activity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"action" : "doorbell_motion_detected",
"callingUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
},
"dateTime" : 1582220686158,
"deviceID" : "K98GiDT45GUL",
"deviceName" : "Front Door",
"deviceType" : "doorbell",
"entities" : {
"activity" : "any",
"callingUser" : "deleted",
"device" : "K98GiDT45GUL",
"house" : "any",
"otherUser" : "deleted"
},
"house" : {
"houseID" : "any",
"houseName" : "any"
},
"info" : {
"dvrID" : "any",
"hasSubscription" : false,
"image" : {
"bytes" : 42865,
"created_at" : "2020-02-20T17:44:45Z",
"etag" : "andy",
"format" : "jpg",
"height" : 576,
"original_filename" : "file",
"placeholder" : false,
"public_id" : "any",
"resource_type" : "image",
"secure_url" : "https://my.updated.image/image.jpg",
"signature" : "abc",
"tags" : [],
"type" : "upload",
"url" : "http://my.updated.image/image.jpg",
"version" : 1582220685,
"width" : 720
},
"videoAvailable" : true,
"videoUploadProgress" : "complete"
},
"otherUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
}
}
38 changes: 38 additions & 0 deletions tests/fixtures/doorbell_motion_activity_no_image.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"action" : "doorbell_motion_detected",
"callingUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
},
"dateTime" : 1582220686158,
"deviceID" : "K98GiDT45GUL",
"deviceName" : "Front Door",
"deviceType" : "doorbell",
"entities" : {
"activity" : "any",
"callingUser" : "deleted",
"device" : "K98GiDT45GUL",
"house" : "any",
"otherUser" : "deleted"
},
"house" : {
"houseID" : "any",
"houseName" : "any"
},
"info" : {
"dvrID" : "any",
"hasSubscription" : false,
"videoAvailable" : true,
"videoUploadProgress" : "complete"
},
"otherUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
}
}
56 changes: 56 additions & 0 deletions tests/fixtures/doorbell_motion_activity_old.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"action" : "doorbell_motion_detected",
"callingUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
},
"dateTime" : 1482220686158,
"deviceID" : "K98GiDT45GUL",
"deviceName" : "Front Door",
"deviceType" : "doorbell",
"entities" : {
"activity" : "any",
"callingUser" : "deleted",
"device" : "K98GiDT45GUL",
"house" : "any",
"otherUser" : "deleted"
},
"house" : {
"houseID" : "any",
"houseName" : "any"
},
"info" : {
"dvrID" : "any",
"hasSubscription" : false,
"image" : {
"bytes" : 42865,
"created_at" : "2020-02-20T17:44:45Z",
"etag" : "andy",
"format" : "jpg",
"height" : 576,
"original_filename" : "file",
"placeholder" : false,
"public_id" : "any",
"resource_type" : "image",
"secure_url" : "https://this.is.the.old.url/should_not_take.jpg",
"signature" : "abc",
"tags" : [],
"type" : "upload",
"url" : "http://this.is.the.old.url/should_not_take.jpg",
"version" : 1482220685,
"width" : 720
},
"videoAvailable" : true,
"videoUploadProgress" : "complete"
},
"otherUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
}
}
56 changes: 56 additions & 0 deletions tests/fixtures/doorbell_motion_activity_wrong.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"action" : "doorbell_motion_detected",
"callingUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
},
"dateTime" : 1582220686158,
"deviceID" : "thisisthewrongdeviceid",
"deviceName" : "Front Door",
"deviceType" : "doorbell",
"entities" : {
"activity" : "any",
"callingUser" : "deleted",
"device" : "thisisthewrongdeviceid",
"house" : "any",
"otherUser" : "deleted"
},
"house" : {
"houseID" : "any",
"houseName" : "any"
},
"info" : {
"dvrID" : "any",
"hasSubscription" : false,
"image" : {
"bytes" : 42865,
"created_at" : "2020-02-20T17:44:45Z",
"etag" : "andy",
"format" : "jpg",
"height" : 576,
"original_filename" : "file",
"placeholder" : false,
"public_id" : "any",
"resource_type" : "image",
"secure_url" : "https://my.updated.image/image.jpg",
"signature" : "abc",
"tags" : [],
"type" : "upload",
"url" : "http://my.updated.image/image.jpg",
"version" : 1582220685,
"width" : 720
},
"videoAvailable" : true,
"videoUploadProgress" : "complete"
},
"otherUser" : {
"FirstName" : "Unknown",
"LastName" : "User",
"PhoneNo" : "deleted",
"UserID" : "deleted",
"UserName" : "deleteduser"
}
}
Loading

0 comments on commit 860c2cc

Please sign in to comment.