Created
January 5, 2021 22:47
-
-
Save mattgruter/405e1f8c7b7ece26ba027ab98c069f7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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