#! /usr/bin/env python3 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 # Sources : # https://community.jeedom.com/t/tutoriel-integration-ring-doorbell/22401 # https://github.com/tchellomello/python-ring-doorbell # https://python-ring-doorbell.readthedocs.io/ # VARIABLES A ADAPTER : # Adjust path to ring token cache file cache_file = Path("/var/www/html/plugins/script/data/ring_token.cache") # ----------------------------------------- # Adjust jeedom API URL (replace XXXXXX with Jeedom local IP and YYYYY with Jeedom API Key) url = "http://XXXXXX/core/api/jeeApi.php?plugin=virtual&apikey=YYYYY&type=virtual&id=%s&value=%s" # ----------------------------------------- # Adjust path to video repository mydir = "/var/www/html/video/" # ----------------------------------------- # Set the correct command ID to service status num_service = 2259 # ----------------------------------------- # Set the correct command ID to connection status num_status = 2260 # ----------------------------------------- # Set the correct command ID to battery life and connection status num_battery = 2261 # ----------------------------------------- # Set the correct command ID to historique num_histo = 2257 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): 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()) tz = pytz.timezone("Europe/Paris") ring = Ring(auth) try: ring.update_data() # send service status to Jeedom if ring.session['profile']['authentication_token'] != '': SendToJeedom(num_service,1) else: SendToJeedom(num_service,0) # retrieve doorbell entree devices = ring.devices() doorbells = devices['doorbots'] doorbellEntree = doorbells[0] # send battery life and connection status to jeedom SendToJeedom(num_battery,doorbellEntree.battery_life) SendToJeedom(num_status,doorbellEntree.connection_status) # listing the last 15 events of any kind for event in doorbellEntree.history(limit=1,timezone="Europe/Paris"): res = "répondu" 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) SendToJeedom(num_histo, "%s - %s" % (event['created_at'], res)) except Exception as e: pprint("Impossible de mettre à jour les données Ring") if __name__ == "__main__": main()