Skip to content

Commit

Permalink
Fixed session auto-json checks, and added support for manual system m…
Browse files Browse the repository at this point in the history
…ode change.
  • Loading branch information
bmenchaca committed May 9, 2018
1 parent d0ef2da commit 288b62d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
39 changes: 20 additions & 19 deletions icomfort3-scraper/lcc_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class IComfort3Zone(object):
CHANGE_SET_POINT = 'Dashboard/ChangeSetPoint'
MODE_SCHED_PATH = 'ModesSchedules/ModesSchedulesMenu'
CHANGE_ZONE_SCHED = 'ModesSchedules/ChangeZoneScheduleId'
SYSMODE_MANUAL = 'modesSchedules/ChangeSystemModeManual'

def __init__(self, home_id, lcc_id, zone_id):
# static, pre-configured entries
Expand All @@ -113,8 +114,9 @@ def __send_update_request(self, session):
('lccid', self.lcc_id), ('_', str(current_millis)) )
up_url = IC3Session.create_url(IComfort3Zone.DETAILS_PATH,
details_query)
update = session.request_json(up_url, self.hd_url)
return update
resp = session.request_json(up_url, self.hd_url)
resp_json = session.process_as_json(resp)
return resp_json


# The requestor validated that the session has not Failed
Expand Down Expand Up @@ -177,7 +179,8 @@ def set_away_mode(self, session):
"""
set_away_url = IC3Session.create_url(IComfort3Zone.SET_AWAY_PATH)
payload = [('lccId', self.lcc_id), ('currentzoneId', self. zone_id)]
resp_json = session.post_url_json(set_away_url, payload, self.hd_url)
resp = session.post_url_json(set_away_url, payload, self.hd_url)
rsep_json = session.process_as_json(resp)
if not resp_json:
return False
return self.__parse_update(resp_json)
Expand All @@ -189,7 +192,8 @@ def cancel_away_mode(self, session):
cancel_away_url = IC3Session.create_url(IComfort3Zone.CANCEL_AWAY_PATH)
payload = [('lccId', self.lcc_id), ('currentzoneId', self. zone_id),
('smartawayS', 'false')]
resp_json = session.post_url_json(cancel_away_url, payload, self.hd_url)
resp = session.post_url_json(cancel_away_url, payload, self.hd_url)
resp_json = session.process_as_json(resp)
if not resp_json:
return False
return self.__parse_update(resp_json)
Expand All @@ -216,34 +220,31 @@ def change_set_point(self, session, cool, heat):
('isPerfectTempOn', 'false'), ('_', str(current_millis))]
change_url = IC3Session.create_url(IComfort3Zone.CHANGE_SET_POINT,
query)
resp_json = session.request_json(change_url, referer_url=self.hd_url)
resp = session.request_json(change_url, referer_url=self.hd_url)
resp_json = session.process_as_json(resp)
return self.__parse_update(resp_json)


def change_zone_schedule_id(self, session, schedule_id):
""" Change the current zone Schedule by ID.
"""
session.request_url(self.ms_url, self.hd_url)
payload = [('lccId', self.lcc_id), ('zoneId', self.zone_id),
('scheduleId', schedule_id)]
heads = {}
heads['ADRUM'] = 'isAjax:true'
heads['Accept'] = 'application/json, text/javascript, */*; q=0.01'
heads['Accept-Language'] = 'en-US,en;q=0.9'
heads['Content-Type']='application/x-www-form-urlencoded; charset=UTF-8'
heads['Host'] = 'www.lennoxicomfort.com'
heads['X-Requested-With'] = 'XMLHttpRequest'
change_zs_url = IC3Session.create_url(IComfort3Zone.CHANGE_ZONE_SCHED)
resp = session.post_url(change_zs_url, post_data=payload, headers=heads,
referer_url=self.ms_url)
#print(resp.request.url)
#print(resp.request.headers)
#print(resp.status_code)
#print(resp.text)
resp = session.post_url_json(change_zs_url, post_data=payload,
referer_url=self.ms_url)
resp.raise_for_status
return resp.status_code == 200

def change_system_mode_manual(self, session, schedule_id, period_id, mode):
""" Change to a manually controlled mode rather than a schedule.
"""
payload = [('zoneId', self.zone_id), ('lccId', self.lcc_id),
('scheduleId', schedule_id), ('periodId', period_id),
('mode', mode)]
sysmode_url = IC3Session.create_url(IComfort3Zone.SYSMODE_MANUAL)
resp = session.post_url_json(sysmode_url, post_data=payload,
referer_url=self.ms_url)
resp.raise_for_status
return resp.status_code == 200

6 changes: 4 additions & 2 deletions icomfort3-scraper/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def request_json(self, url, referer_url=''):
#print(response.request.headers)
#print(response.headers)
#print(response.text)
return self.__process_as_json(response)
return response


def post_url_json(self, url, post_data=[], referer_url=''):
Expand All @@ -169,8 +169,10 @@ def post_url_json(self, url, post_data=[], referer_url=''):
post_heads['Origin'] = "https://" + IComfort3Session.DOMAIN
post_heads['X-Requested-With'] = 'XMLHttpRequest'
post_heads['Accept'] = 'application/json, text/javascript, */*; q=0.01'
post_heads['ADRUM'] = 'isAjax:true'
post_heads['Accept-Language'] = 'en-US,en;q=0.9'
resp = self.session.post(url, headers=post_heads, data=post_data)
return self.__process_as_json(resp)
return resp


def process_as_json(self, response):
Expand Down
35 changes: 35 additions & 0 deletions icomfort3-scraper/test_change_system_mode_manual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python

import secrets
import time
from session import IComfort3Session
from lcc_zone import IComfort3Zone

s = IComfort3Session()
s.login(secrets.icomfort_username, secrets.icomfort_password)
homes = s.fetch_home_zones()

#mode: heat = 1, off = 4, cool = 0, Heat_And_Cool = 2
#schedule 16 = manual mode
for home in homes:
for (lcc, zone) in homes[home]:
z = IComfort3Zone(home, lcc, zone)
s.set_context(home, lcc, zone)
print ("Home %s, lcc %s, zone %s" % (home, lcc, zone))
update = z.fetch_update(s)
sch_id = update['ScheduleId']
per_id = update['PeriodId']
if update['ScheduleId'] == 16:
mode_text = "Manual"
else:
mode_text = "Schedule %s" % update['ScheduleId']
print("Before Change Manual Mode: %s, mode %s" % (mode_text, update['SystemMode']))
change = z.change_system_mode_manual(s, sch_id, per_id, 'Heat_And_Cool')
after = z.fetch_update(s)
if update['ScheduleId'] == 16:
mode_text = "Manual"
else:
mode_text = "Schedule %s" % update['ScheduleId']
print("After Change Manual Mode: %s, mode %s" % (mode_text, update['SystemMode']))

s.logout()

0 comments on commit 288b62d

Please sign in to comment.