#!/usr/bin/python3 # # Script to perform various commands against the thermostats. # # The thermostats are RedLINK capable, and are gateway'd to the internet # via a Honeywell THM6000R1002. # import sys import argparse import configparser import json import requests PORTAL = 'https://www.mytotalconnectcomfort.com/portal' CONFIG_FNAME = '/etc/homesvr/thermo.conf' # Cookie Name when we get logged-in CN_LOGGED_IN = '.ASPXAUTH_TRUEHOME' USER_AGENT = 'homesvr/0.1' class Connection: def __init__(self, creds): self.session = requests.Session() # Set our User-Agent self.session.headers['User-Agent'] = USER_AGENT self.session.headers['X-Requested-With'] = 'XMLHttpRequest' self._login(creds['username'], creds['password']) def _login(self, username, password): # Ping the server to get a sign-in cookie. r = self.session.get(PORTAL) print('COOKIES:', self.session.cookies) # Build a login form response, and use the above cookie to # actually sign in. This will give us a new logged-in cookie. form = { 'UserName': username, 'Password': password, #'RememberMe': False, } r = self.session.post(PORTAL, data=form) print('STATUS:', r.status_code) #print('BODY:', r.text) #print('COOKIES:', self.session.cookies) for c in self.session.cookies: pass #print('COOKIE:', vars(c)) names = set(c.name for c in self.session.cookies) print('NAMES:', names) if CN_LOGGED_IN not in names: raise ValueError('invalid credentials. check thermo.conf') def request(self, path): r = self.session.post(PORTAL + path) print('STATUS:', r.status_code) print('HEADERS:', r.headers) if 'json' in r.headers['Content-Type']: print('JSON:') import pprint pprint.pprint(r.json()) else: print('TEXT:', r.text) if __name__ == '__main__': cfg = configparser.ConfigParser() cfg.read(CONFIG_FNAME) conn = Connection(cfg['credentials']) # page=1 is required #conn.request('/Location/GetLocationListData?page=1') # page=1 is required #conn.request('/Device/GetZoneListData?locationId=%s&page=1' % (cfg.get('defaults', 'location'),)) conn.request('/Device/CheckDataSession/%s' % (cfg.get('names', 'theater'),))