Synology Surveillance Station - Enable / Disable Camera(s) script

Hi all,
For those who care, I made “a little PyScript’s script” to enable / disable camera(s) in Surveillance Station via automations (and detection…).

It’s based on the Surveillance Station API and works on DSM 6 / SS 9 versions.
Based on that, you can add Snapshots functions, record, PTZ… whatever…

import json
import requests

#
# Based on Synology Surveillance Station API Specs
# https://global.download.synology.com/download/Document/Software/DeveloperGuide/Package/SurveillanceStation/All/enu/Surveillance_Station_Web_API.pdf
#
# list of camera(s) to activate/desactivate, must be the name in Surveillance Station
synology_cameras_to_work_with = ['cam1', 'cam3', 'camx']
# Synology account name, must be "administrator" in surveillance station
synology_surveillance_account_name = "account_name"
# Password
synology_surveillance_account_password = "account_password"
# URL / Ip Address of the Synology Target, end with /
synology_main_url = "https://X.X.X.X/"
# DSM Version
synology_dsm_version = 6
# Surveillance Station version
synology_surveillance_version = 9
# to store the session id
synology_session_sid = ""
# Login Url
synology_auth_login_url = f"{synology_main_url}webapi/auth.cgi?api=SYNO.API.Auth&method=login&version={synology_dsm_version}&account={synology_surveillance_account_name}&passwd={synology_surveillance_account_password}&session=SurveillanceStation&format=sid"
# Logout Url
synology_auth_logout_url =f"{synology_main_url}webapi/auth.cgi?api=SYNO.API.Auth&method=logout&version={synology_dsm_version}&session=SurveillanceStation&_sid=XXSIDXX"
# Cameras list Url
synology_cameras_list_url = f"{synology_main_url}webapi/entry.cgi?version={synology_surveillance_version}&api=SYNO.SurveillanceStation.Camera&method=List&_sid=XXSIDXX"
# Camera enable Url
synology_camera_enable_url = f"{synology_main_url}webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera&method=Enable&version={synology_surveillance_version}&idList=XXLISTXX&_sid=XXSIDXX"
# Camera disable Url
synology_camera_disable_url = f"{synology_main_url}webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera&method=Disable&version={synology_surveillance_version}&idList=XXLISTXX&_sid=XXSIDXX"
# To store camera(s) id to work with
synology_cameras_ids_to_work_with = ""

@service
def synology_enable_cameras():
    synology_cameras_ids_to_work_with = ""
    #log.warning('Synology enable cameras start')
    #
    # Login
    #
    #log.warning(f"Call avec {synology_auth_login_url}")
    try:
        response_auth = task.executor(requests.get,synology_auth_login_url)
        #log.warning(f"response : {response_auth.status_code} headers: {response_auth.headers}")
        #log.warning(f"reponse.json : {response_auth.json()}")
        data = response_auth.json()
        synology_session_sid = data['data']['sid']
        #log.warning(f"recu SID={synology_session_sid}")
    except:
        log.error('An exception occurs during Synology Cameras Enable - Login phase')
        raise
    else:
        #
        # End Login, next
        #
        #
        # List cameras
        #
        camera_list_url = synology_cameras_list_url.replace("XXSIDXX", synology_session_sid)
        #log.warning(f"Cameras list call avec {camera_list_url}")
        try:
            response_listcam = task.executor(requests.get,camera_list_url)
            #log.warning(f"response : {response_listcam.status_code} headers: {response_listcam.headers}")
            #log.warning(f"reponse.json : {response_listcam.json()}")
            data = response_listcam.json()
        except:
            log.error('An exception occurs during Synology Cameras Enable - List all cameras phase')
            raise
        else:
            #
            # End List Cams - Next
            #
            #
            # Enable Cams
            #
            # ['data'][list_camera[i]]['id'] - ['status'] - ['model'] - ['new_name'] - ['status']
            for camera in data['data']['cameras']:
                #log.warning(f"Camera id={camera['id']} , model={camera['model']} , name={camera['newName']} , status={camera['status']}")

                # Check if camera is in list
                for cam_to_work_with in synology_cameras_to_work_with:
                    if cam_to_work_with == camera['newName']:
                        # Find a camera, add to list to enable
                        #log.warning(f"Find this one : {cam_to_work_with} => ID={camera['id']}")
                        if synology_cameras_ids_to_work_with.strip() == "":
                            synology_cameras_ids_to_work_with = str(camera['id'])
                        else:
                            synology_cameras_ids_to_work_with = synology_cameras_ids_to_work_with + "," + str(camera['id'])
            # End for
            # If not empty, enable list of cameras
            if synology_cameras_ids_to_work_with.strip() =="":
                log.warning("No camera to disable...")
            else:
                #log.warning(f"List of cams to enable:{synology_cameras_ids_to_work_with}")
                camera_enable_url = synology_camera_enable_url.replace("XXLISTXX", synology_cameras_ids_to_work_with)
                camera_enable_url = camera_enable_url.replace("XXSIDXX", synology_session_sid)
                #log.warning(f"Call enable with {camera_enable_url}")
                try:
                    #log.warning("Logout...")
                    response_cams_enable = task.executor(requests.get, camera_enable_url)
                    #log.warning(f"response : {response_cams_enable.status_code} headers: {response_cams_enable.headers}")
                    #log.warning(f"response : {response_cams_enable.json()}")
                    #log.warning("Cams enable End")
                except:
                    log.error('An exception occurs during Synology Cameras Enable - Enabling phase')
                    raise
                #else:
                    #log.warning('End of Enable Cameras ;) ')
            #
            # End Enable Cams
            #
            #
            # Logout
            # 
            logout_url = synology_auth_logout_url.replace("XXSIDXX", synology_session_sid)
            try:
                #log.warning("Logout...")
                response_logout = task.executor(requests.get, logout_url)
                #log.warning(f"response : {response_logout.status_code} headers: {response_logout.headers}")
                #log.warning(f"response : {response_logout.json()}")
                #log.warning("Logout End")
            except:
                log.error('An exception occurs during Synology Cameras Enable - Logout phase')
                raise
            #else:
                #log.warning('End of Enable Cameras ;) ')
            #
            # End Logout
            #

@service
def synology_disable_cameras():
    #log.warning('synology disable cameras start')
    synology_cameras_ids_to_work_with = ""
    #
    # Login
    #
    #log.warning(f"Call avec {synology_auth_login_url}")
    try:
        response_auth = task.executor(requests.get,synology_auth_login_url)
        #log.warning(f"response : {response_auth.status_code} headers: {response_auth.headers}")
        #log.warning(f"reponse.json : {response_auth.json()}")
        data = response_auth.json()
        synology_session_sid = data['data']['sid']
        #log.warning(f"recu SID={synology_session_sid}")
    except:
        log.error('An exception occurs during Synology Cameras Disable - Login phase')
        raise
    else:
        #
        # End Login, next
        #
        #
        # List cameras
        #
        camera_list_url = synology_cameras_list_url.replace("XXSIDXX", synology_session_sid)
        #log.warning(f"Cameras list call avec {camera_list_url}")
        try:
            response_listcam = task.executor(requests.get,camera_list_url)
            #log.warning(f"response : {response_listcam.status_code} headers: {response_listcam.headers}")
            #log.warning(f"reponse.json : {response_listcam.json()}")
            data = response_listcam.json()
        except:
            log.error('An exception occurs during Synology Cameras Disable - List all cameras phase')
            raise
        else:
            #
            # End List Cams - Next
            #
            #
            # Enable Cams
            #
            # ['data'][list_camera[i]]['id'] - ['status'] - ['model'] - ['new_name'] - ['status']
            for camera in data['data']['cameras']:
                #log.warning(f"Camera id={camera['id']} , model={camera['model']} , name={camera['newName']} , status={camera['status']}")

                # Check if camera is in list
                for cam_to_work_with in synology_cameras_to_work_with:
                    if cam_to_work_with == camera['newName']:
                        # Find a camera, add to list to enable
                        #log.warning(f"Find this one : {cam_to_work_with} => ID={camera['id']}")
                        if synology_cameras_ids_to_work_with.strip() == "":
                            synology_cameras_ids_to_work_with = str(camera['id'])
                        else:
                            synology_cameras_ids_to_work_with = synology_cameras_ids_to_work_with + "," + str(camera['id'])
            # End for
            # If not empty, enable list of cameras
            if synology_cameras_ids_to_work_with.strip() =="":
                log.warning("No camera to enable...")
            else:
                #log.warning(f"List of cams to enable:{synology_cameras_ids_to_work_with}")
                camera_disable_url = synology_camera_disable_url.replace("XXLISTXX", synology_cameras_ids_to_work_with)
                camera_disable_url = camera_disable_url.replace("XXSIDXX", synology_session_sid)
                #log.warning(f"Call enable with {camera_enable_url}")
                try:
                    #log.warning("Logout...")
                    response_cams_enable = task.executor(requests.get, camera_disable_url)
                    #log.warning(f"response : {response_cams_enable.status_code} headers: {response_cams_enable.headers}")
                    #log.warning(f"response : {response_cams_enable.json()}")
                    #log.warning("Cams enable End")
                except:
                    log.error('An exception occurs during Synology Cameras Disable - Disabling phase')
                    raise
                #else:
                    #log.warning('End of Disable Cameras ;) ')
            #
            # End Enable Cams
            #
            #
            # Logout
            # 
            logout_url = synology_auth_logout_url.replace("XXSIDXX", synology_session_sid)
            try:
                #log.warning("Logout...")
                response_logout = task.executor(requests.get, logout_url)
                #log.warning(f"response : {response_logout.status_code} headers: {response_logout.headers}")
                #log.warning(f"response : {response_logout.json()}")
                #log.warning("Logout End")
            except:
                log.error('An exception occurs during Synology Cameras Disable - Logout phase')
                raise
            #else:
                #log.warning('End of Disable Cameras ;) ')
            #
            # End Logout
            #

Log lines are here if you want to see how the script works / what the SS returns are.

Regards,
Goldorak92

1 Like