Synology Surveillance Station IP Camera

Any plan to make this into a proper component?

Unfortunately no. I am not a programmer so I lack the knowledge and skills to pull that off. I was only able to put together my script through a fair amount of trial and error.

I used @fanaticDavid 's example and created a way to toggle the home mode in the Surveillance Station. I don’t want to have my cameras recording while I’m at home. Using home mode makes it easy to change the recording settings of all cameras with a single switch.

synology_home_mode.py:

#!/usr/bin/python3

import sys
import requests
import json


PROTOCOL = "http"
IP_ADDRESS = "XXX.XXX.XXX.XXX"    #your Synology NAS ip address
PORT = "XXXX"                     #the port used by Synology DiskStation Manager
API_PATH = "/webapi/"
BASE_URL = PROTOCOL + "://" + IP_ADDRESS + ":" + PORT + API_PATH
API_AUTH_FILE = "auth.cgi"
API_CAMERA_FILE = "entry.cgi"

HOME_MODE = sys.argv[1]

AUTH_PAYLOAD = {'api': 'SYNO.API.Auth', 'version': '2', 'session': 'SurveillanceStation'}
LOGIN_PAYLOAD = {'method': 'Login', 'account': 'YOUR_ACCOUNT', 'passwd': 'YOUR_PASSWORD', 'format': 'cookie'}
LOGIN_PAYLOAD.update(AUTH_PAYLOAD)
LOGOUT_PAYLOAD = {'method': 'Logout'}
LOGOUT_PAYLOAD.update(AUTH_PAYLOAD)

if HOME_MODE == "status":
    ACTION_PAYLOAD = {'api': 'SYNO.SurveillanceStation.HomeMode', 'version': '1', 'method': 'GetInfo'}	
if HOME_MODE == "home":
    ACTION_PAYLOAD = {'api': 'SYNO.SurveillanceStation.HomeMode', 'version': '1', 'method': 'Switch', 'on': 'true'}	
if HOME_MODE == "away":
    ACTION_PAYLOAD = {'api': 'SYNO.SurveillanceStation.HomeMode', 'version': '1', 'method': 'Switch', 'on': 'false'}	

session = requests.session()

login = session.get(BASE_URL + API_AUTH_FILE, params=LOGIN_PAYLOAD, timeout=5)
action = session.get(BASE_URL + API_CAMERA_FILE, params=ACTION_PAYLOAD, timeout=5)
logout = session.get(BASE_URL + API_AUTH_FILE, params=LOGOUT_PAYLOAD, timeout=5)

session.close()

if HOME_MODE == "status":
    print(action.text)    

And my switch config:

switch:
  - platform: command_line
    switches:
      synology_home_mode:
        command_on: 'python3 path_to_the_script/synology_home_mode.py home'
        command_off: 'python3 path_to_the_script/synology_home_mode.py away'
        command_state: 'python3 path_to_the_script/synology_home_mode.py status'
        value_template: >
          {% if value_json is defined %}
            {{ value_json.data.on}}
          {% else %}
            {{ states.switch.synology_home_mode.state == 'on' }}
          {% endif %}
        friendly_name: Surveillance Station Home Mode
3 Likes

Hi JuRy, I am trying to implement this script to disable motion on Surveillance cameras.
This is done by the detection source. e.g. -1:disable,0:by_camera and 1:by_surveillance.
My camera_motion.py script:
`#!/usr/bin/python3

import sys
import requests
import json

PROTOCOL = “http”
IP_ADDRESS = “MY_IP” #your Synology NAS ip address
PORT = “PORT” #the port used by Synology DiskStation Manager
API_PATH = “/webapi/”
BASE_URL = PROTOCOL + “://” + IP_ADDRESS + “:” + PORT + API_PATH
API_AUTH_FILE = “auth.cgi”
API_CAMERA_FILE = “entry.cgi”

CAMERA_ID = sys.argv[1]
CAMERA_SOURCE = sys.argv[2]

AUTH_PAYLOAD = {‘api’: ‘SYNO.API.Auth’, ‘version’: ‘2’, ‘session’: ‘SurveillanceStation’}
LOGIN_PAYLOAD = {‘method’: ‘Login’, ‘account’: ‘USERNAME’, ‘passwd’: ‘PASSWORD’, ‘format’: ‘cookie’}
LOGIN_PAYLOAD.update(AUTH_PAYLOAD)
LOGOUT_PAYLOAD = {‘method’: ‘Logout’}
LOGOUT_PAYLOAD.update(AUTH_PAYLOAD)

if CAMERA_SOURCE == “source”:
ACTION_PAYLOAD = {‘api’: ‘SYNO.SurveillanceStation.Camera.Event’, ‘version’: ‘2’, ‘method’: ‘MotionEnum’, ‘cameraIds’: CAMERA_ID}
else:
ACTION_PAYLOAD = {‘api’: ‘SYNO.SurveillanceStation.Camera.Event’, ‘version’: ‘2’, ‘method’: ‘MDParamSave’, ‘cameraId’: CAMERA_ID, ‘action’: CAMERA_SOURCE, ‘keep’: ‘true’}

session = requests.session()

login = session.get(BASE_URL + API_AUTH_FILE, params=LOGIN_PAYLOAD, timeout=5)
action = session.get(BASE_URL + API_CAMERA_FILE, params=ACTION_PAYLOAD, timeout=5)
logout = session.get(BASE_URL + API_AUTH_FILE, params=LOGOUT_PAYLOAD, timeout=5)

session.close()

if CAMERA_SOURCE == “source”:
print(action.text)`

And this is my switch config:
`switch:

  • platform: command_line
    switches:
    ss_camera_motion:
    command_on: ‘python3 /config/camera_motion.py 8 0’
    command_off: ‘python3 /config/camera_motion.py 8 -1’
    command_state: ‘python3 /config/camera_motion.py 8 source’
    value_template: >
    {% if value_json is defined %}
    {{ value_json.data.0}}}
    {% else %}
    {{ states.switch.ss_camera_motion.state == ‘on’ }}
    {% endif %}
    friendly_name: Front Door`.

When I try to turn on the switch i get below error message
2017-10-16 10:52:24 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'dict object' has no attribute 'data' (value: {"error":{"code":104},"success":false}, template: {% if value_json is defined %} {{ value_json.data.0}}} {% else %} {{ states.switch.ss_camera_motion.state == 'on' }} {% endif %} )

Any ideas where is the mistake?

Can you run your camera_motion.py script independently to make sure that the script works as expected and returns values that your switch is relying on?

Hi JuRy, thanks for reply, when I run the script manually i get:

root@DiskStation:/usr/src/app# python3 /config/camera_motion.py 8 source
{“error”:{“code”:401},“success”:false}.
However when I run this commands in browser I can get status and change the detection parametr to -1 or 0&
IP:5000/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=2&account=USERNAME&passwd=PASSWORD&session=SurveillanceStation&format=cookie
IP:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera.Event&camId=8&version=1&method=MotionEnum
IP:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.Camera.Event&camId=8&method=MDParamSave&source=0&version=1&mode=0&keep=true

Looked again at the script and don’t know where the issue can be.

Update, got the script working, now need to have a look at home-assistant switches :

#!/usr/bin/python3

import sys
import requests
import json

PROTOCOL = “http”
IP_ADDRESS = “IP” #your Synology NAS ip address
PORT = “PORT” #the port used by Synology DiskStation Manager
API_PATH = “/webapi/”
BASE_URL = PROTOCOL + “://” + IP_ADDRESS + “:” + PORT + API_PATH
API_AUTH_FILE = “auth.cgi”
API_CAMERA_FILE = “entry.cgi”

CAMERA_ID = sys.argv[1]
CAMERA_ACTION = sys.argv[2]

AUTH_PAYLOAD = {‘api’: ‘SYNO.API.Auth’, ‘version’: ‘2’, ‘session’: ‘SurveillanceStation’}
LOGIN_PAYLOAD = {‘method’: ‘Login’, ‘account’: ‘USERNAME’, ‘passwd’: ‘PASSWORD’, ‘format’: ‘cookie’}
LOGIN_PAYLOAD.update(AUTH_PAYLOAD)
LOGOUT_PAYLOAD = {‘method’: ‘Logout’}
LOGOUT_PAYLOAD.update(AUTH_PAYLOAD)

if CAMERA_ACTION == “status”:
ACTION_PAYLOAD = {‘api’: ‘SYNO.SurveillanceStation.Camera.Event’, ‘camId’: CAMERA_ID, ‘version’: ‘1’, ‘method’: ‘MotionEnum’}
else:
ACTION_PAYLOAD = {‘api’: ‘SYNO.SurveillanceStation.Camera.Event’, ‘camId’: CAMERA_ID, ‘version’: ‘1’, ‘method’: ‘MDParamSave’, ‘source’: CAMERA_ACTION}

session = requests.session()

login = session.get(BASE_URL + API_AUTH_FILE, params=LOGIN_PAYLOAD, timeout=5)
action = session.get(BASE_URL + API_CAMERA_FILE, params=ACTION_PAYLOAD, timeout=5)
logout = session.get(BASE_URL + API_AUTH_FILE, params=LOGOUT_PAYLOAD, timeout=5)

session.close()

if CAMERA_ACTION == “status”:
print(action.text)

I’ve 2 synology servers and running have the 2 x Synology camera component integrate to HASS.
Both components are starting up fine but not displaying nicely in the GUI web page.
I received Image not available on either of the camera source.

I.e. All cameras load up fine from source A but not source B. Sometime, all cameras load up fine from source B but not source A.

Anyone encounters the same problem and any idea about this?

Same setup here, and same problem. If you click on the “image not available” then the live cam feed is working fine.
reverted to 0.56.2 and problem is gone.

Error from log

nov 17 07:23:08 raspberrypi hass[30509]: File “/usr/local/lib/python3.4/dist-pacpakages/homeassistant/components/camera/synology.py”, line 115, in update
nov 17 07:23:08 raspberrypi hass[30509]: self._surveillance.update()
nov 17 07:23:08 raspberrypi hass[30509]: File “/home/pi/.homeassistant/deps/lib/python3.4/site-packages/synology/surveillance_station.py”, line 17, in update
nov 17 07:23:08 raspberrypi hass[30509]: cameras = self._api.camera_list()
nov 17 07:23:08 raspberrypi hass[30509]: File “/home/pi/.homeassistant/deps/lib/python3.4/site-packages/synology/api.py”, line 96, in camera_list
nov 17 07:23:08 raspberrypi hass[30509]: response = self._get_json(api[‘url’], payload)
nov 17 07:23:08 raspberrypi hass[30509]: File “/home/pi/.homeassistant/deps/lib/python3.4/site-packages/synology/api.py”, line 192, in _get_json
nov 17 07:23:08 raspberrypi hass[30509]: raise ValueError(‘Invalid or failed response’, content)
nov 17 07:23:08 raspberrypi hass[30509]: ValueError: (‘Invalid or failed response’, {‘error’: {‘code’: 105}, ‘success’: False})

I made a script based on @fanaticDavid’s script to change preset positions:

#!/usr/bin/python3

import sys
import requests
import json


PROTOCOL = "http"
IP_ADDRESS = "xxx.xxx.x.x"    #your Synology NAS ip address
PORT = "xxxx"                 #the port used by Synology DiskStation Manager
API_PATH = "/webapi/"
BASE_URL = PROTOCOL + "://" + IP_ADDRESS + ":" + PORT + API_PATH
API_AUTH_FILE = "auth.cgi"
API_CAMERA_FILE = "entry.cgi"

CAMERA_ID = sys.argv[1]
PRESET = sys.argv[2]

AUTH_PAYLOAD = {'api': 'SYNO.API.Auth', 'version': '2', 'session': 'SurveillanceStation'}
LOGIN_PAYLOAD = {'method': 'Login', 'account': 'ACCOUNT', 'passwd': 'PASSWORD', 'format': 'cookie'}
LOGIN_PAYLOAD.update(AUTH_PAYLOAD)
LOGOUT_PAYLOAD = {'method': 'Logout'}
LOGOUT_PAYLOAD.update(AUTH_PAYLOAD)

ACTION_PAYLOAD = {'api': 'SYNO.SurveillanceStation.PTZ', 'version': '4', 'method': 'GoPreset', 'cameraId': CAMERA_ID, 'position': PRESET, 'speed': '3'}


session = requests.session()

login = session.get(BASE_URL + API_AUTH_FILE, params=LOGIN_PAYLOAD, timeout=5)
action = session.get(BASE_URL + API_CAMERA_FILE, params=ACTION_PAYLOAD, timeout=5)
logout = session.get(BASE_URL + API_AUTH_FILE, params=LOGOUT_PAYLOAD, timeout=5)

session.close()

Your shell commands should look like this:
python3 /home/homeassistant/.homeassistant/python/camera_ptz.py [Camera ID] [Preset #]

To find the camera id and preset position numbers you can use the dev console in chrome or firefox to find that info under the network tab

cheers

Refering to @fanaticDavid example scripts,

Can i run these python scripts in the HASSIO \config\python_scripts ?
will it find API’s if it runs here?

I have NAS running seperate to HASSIO, and want to update home status in Synology Security Centre.

I am new to all of this… But trying to understand… Any help appreciated.

Glen

If you want to enable/disable home-mode I looked here for help: https://forum.synology.com/enu/viewtopic.php?t=134755
Find the response from a litte bit down “franieri”.

Basically you create a “token” that you can then use in a http request.

Quick quide:

Login (use your NAS account and password to login, use 5001 for https or 5000 for http)

http://IP_OF_YOUR_NAS:5000/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=XXXXXXX&passwd=XXXXXXXX&session=SurveillanceStation&format=sid"

Then you get a response with something like this:

{"data":{"sid":"Gj.tXLURyrKZg1510MPN674502"},"success":true}

To then set Home Mode on you use:

http://IP_OF_YOR_NAS:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=true&_sid=Gj.tXLURyrKZg1510MPN674502

Home Mode OFF

http://IP_OF_YOUR_NAS:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=false&_sid=Gj.tXLURyrKZg1510MPN674502

In your configuration.yaml you enter something like this:

rest_command:
  ss_enable_home:
    url: !secret home_mode_on
    method: get
  ss_disable_home:
    url: !secret home_mode_off
    method: get

And in your secrets.yaml you enter the urls from above:

#Home mode on
home_mode_on: http://..........................

#Home mode off
home_mode_off: http://........................

To call this from automations or scripts you use:

- service: rest_command.ss_disable_home
and
- service: rest_command.ss_enable_home

I put mine in the scripts that I call but you can also enter it directly in the automation if you want:

- id: cottage_arm
  alias: 'Arm Cottage'
  initial_state: 'on'
  condition:
    - condition: state
      entity_id: input_boolean.cottage_update
      state: 'on'
  trigger:
    - platform: state
      entity_id: group.household
      to: 'not_home'
      for:
        minutes: 5
  action:
    - service: homeassistant.turn_on
      entity_id: script.cottage_away
    - service: rest_command.ss_disable_home
3 Likes

Thankyou. Thats perfect.

Implemented as you suggested. Very clear.

Hmm just realized my own version of this is currently not working… not sure why but It worked before the upgrade to 0.64… Maybe I need to create a new token.

Hello, have been able to find a solution to this new issue ?
Thanks,
Fabio

FYI, I had this component setup for 6 months, but eventually got fed up of it. Low refresh rates and frequent disconnections made it unbearable. The lack of responsively when opening the image full screen was also unbearable.
I gave up on this and am now using the camera generic component and that works way better. As a bonus, that’s less traffic to & from my NAS…

I too seem to loose the token, if i reboot my NAS the token seems to drop and i have to run the login url again and get a new token. Maybe there is some way to run the login url first then pass the token to the on or off command url by variable but i have yet to figure out how to do this.

No unfortunately as some others also have suggested the token seems to be lost every now and then so I am not currently using this setup at all. Just a generic camera and records everything no matter if we are at home or not.

Found this on https://github.com/mschippr/AVMFritz-Box7490-SynologySurveillance-Automation/blob/master/switch_homemode.sh

I am still waiting for my new Diskstaion. May be you could try running this as shell script?

wget -q --keep-session-cookies --save-cookies $COOKIESFILE -O- "http://${SYNO_URL}//webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=${SYNO_SS_USER}&passwd=${SYNO_SS_PASS}&session=SurveillanceStation";
wget -q --load-cookies $COOKIESFILE -O- "http://${SYNO_URL}//webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=${1}";
wget -q --load-cookies $COOKIESFILE -O- "http://${SYNO_URL}/webapi/auth.cgi?api=SYNO.API.Auth&method=Logout&version=1";
rm $COOKIESFILE;
3 Likes

Hello,

I have 2 DiskStations, (long story), but using Surveillance station on both with 2 cameras each. I’d like to incorporate both DSMs in Home Assistant and bring in their cameras. I’ve had the first one working for awhile, but when I added the second camera platform with the new IP address. The cameras load up, but the frontend cards have broken thumbnails for only 1 of the DSM’s. It appears to be random upon HA start as well as to which DSM will show it’s cameras. When I click on the card, the live view of the camera shows up fine. Any thoughts on this? Anyone ever try working with multiple DSMs running Surveillance station?

Thanks!

Hello,

I have exactly same situation.
Did you manage to solve these problem?
It would be nice to be able to show all cameras from both DSMs.

Can any one please share their solution if there is any?

Thanks!