import json import getpass from pathlib import Path from pprint import pprint from ring_doorbell import Ring, Auth from oauthlib.oauth2 import MissingTokenError import pytz import requests import glob, os, os.path # TODO : adjust path to ring token cache file # ex: cache_file = Path("/var/www/html/plugins/script/core/ressources/ring_token.cache") cache_file = Path("/XXXX/ring_token.cache") # ----------------------------------------- def token_updated(token): cache_file.write_text(json.dumps(token)) def otp_callback(): auth_code = input("2FA code: ") return auth_code def SendToJeedom(cmd_id, value): # TODO : adjust jeedom API URL # replace XXXXXX with Jeedom local IP and Jeedom API Key url = "http://XXXXXXX/core/api/jeeApi.php?plugin=virtual&apikey=XXXXXXX&type=virtual&id=%s&value=%s" # ----------------------------------------- r = requests.get(url % (cmd_id, value)) def main(): if cache_file.is_file(): auth = Auth("MyProject/1.0", json.loads(cache_file.read_text()), token_updated) else: username = input("Username: ") password = getpass.getpass("Password: ") auth = Auth("MyProject/1.0", None, token_updated) try: auth.fetch_token(username, password) except MissingTokenError: auth.fetch_token(username, password, otp_callback()) # TODO : adjust path to video repository # ex: mydir = "/var/www/html/video/" mydir = "/XXXXX/video/" # ----------------------------------------- tz = pytz.timezone("Europe/Paris") ring = Ring(auth) ring.update_data() # send service status to Jeedom if ring.session['profile']['authentication_token'] != '': # TODO : Set the correct command ID to service status SendToJeedom(1353,1) else: SendToJeedom(1353,0) # ----------------------------------------- # retrieve dorrbell entree devices = ring.devices() doorbells = devices['doorbots'] doorbellEntree = doorbells[0] # send battery life and connection status to jeedom # TODO : Set the correct command ID to battery life and connection status SendToJeedom(1352,doorbellEntree.battery_life) SendToJeedom(4417,doorbellEntree.connection_status) # ----------------------------------------- # listing the last 15 events of any kind for event in doorbellEntree.history(limit=1,timezone="Europe/Paris"): res = "repondu" if event['answered'] == True else "absent..." last_ding = Path(mydir + str(event['id']) + ".eid") if not last_ding.exists(): filelist = glob.glob(os.path.join(mydir, "*.eid")) for f in filelist: os.remove(f) f = open(mydir + str(event['id']) + ".eid","w") f.close() videoFile = os.path.join(mydir, "last_ding.mp4") doorbellEntree.recording_download(event['id'],filename=videoFile,override=True) # TODO : Set the correct command ID to historique SendToJeedom(1354, "%s - %s" % (event['created_at'], res)) # ----------------------------------------- if __name__ == "__main__": main()