Skip to content

Instantly share code, notes, and snippets.

@mattgruter
Created January 5, 2021 22:47
Show Gist options
  • Save mattgruter/405e1f8c7b7ece26ba027ab98c069f7e to your computer and use it in GitHub Desktop.
Save mattgruter/405e1f8c7b7ece26ba027ab98c069f7e to your computer and use it in GitHub Desktop.
import requests
import sys
from pprint import pprint
url = "http://localhost/api"
# helper funciton to check if API request was successful
def check_response(response):
if not response:
print(f"Error: server returned status code {response.status_code}")
print(response.json())
sys.exit()
else:
return response.json()
# authenticate with Phoscon app and retrieve username
def authenticate():
response = requests.post(url, json={"devicetype":"script"})
data = check_response(response)
#pprint(data)
user = data[0]["success"]["username"]
return user
# fetch all sensors from Phoscon app
def get_sensors(user):
response = requests.get(f"{url}/{user}/sensors")
data = check_response(response)
#pprint(data)
return data
def main():
user = authenticate()
sensors = get_sensors(user)
for sensor in sensors.values():
t = sensor["type"]
name = sensor["name"]
state = sensor["state"]
if t.endswith("Presence"):
presence = state["presence"]
print(f"Motion sensor '{name}': {presence}")
elif t.endswith("OpenClose"):
open = state["open"]
print(f"Door/Window sensor '{name}': {open}")
else:
model = sensor["modelid"]
#print(f"Sensor '{name}' (model '{model}') is of unknown type: {t}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment