Files
bin/sec_light.py

48 lines
1.5 KiB
Python
Executable File

#!/usr/bin/python3
import sys
import requests
import json
from urllib.parse import quote_plus
def set_netatmo_light(mode):
payload = {'grant_type': 'password',
'username': "house+netatmo@depaoli.id.au",
'password': "rCC$SEaut4%uVjnZ",
'client_id': "5dbd517c41a113807a58028d",
'client_secret': "pQbSB7n0oNgInEnv0l89mLxaiGkiImln",
'scope': 'read_presence access_presence write_presence'}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
access_token=response.json()["access_token"]
refresh_token=response.json()["refresh_token"]
scope=response.json()["scope"]
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
try:
params = { 'access_token': access_token }
response = requests.post("https://api.netatmo.com/api/gethomedata", params=params)
response.raise_for_status()
data = response.json()["body"]
homes = data["homes"]
cameras = homes[0]["cameras"]
name = cameras[0]["name"]
camera_vpn = cameras[0]["vpn_url"]
config = '{"mode":"'+mode+'"}'
command = '/command/floodlight_set_config?config='
set_url = camera_vpn + command + quote_plus(config)
rsp = requests.post(set_url, data=payload)
print(set_url)
print(rsp)
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
set_netatmo_light( sys.argv[1] )