Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doorbell image fetch #41

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions august/doorbell.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import datetime

import dateutil.parser
import requests

from august.device import Device, DeviceDetail


Expand All @@ -18,6 +23,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 +59,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,14 +78,37 @@ 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

@property
def has_subscription(self):
return self._has_subscription

def get_doorbell_image(self, timeout=10):
return requests.get(self._image_url, timeout=timeout).content
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.19.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