Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Read, Set and Copy Schedules #9

Merged
merged 4 commits into from Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 97 additions & 5 deletions wiserHeatingAPI/wiserHub.py
Expand Up @@ -12,22 +12,26 @@

import logging
import requests
import json
import os

_LOGGER = logging.getLogger(__name__)

"""
Wiser Data URLS
"""
WISERHUBURL = "http://{}/data/domain/" # SSSS
WISERMODEURL= "http://{}/data/domain/System/RequestOverride"
WISERMODEURL = "http://{}/data/domain/System/RequestOverride"
WISERSETROOMTEMP = "http://{}//data/domain/Room/{}"
WISERROOM ="http://{}//data/domain/Room/{}"
WISERROOM = "http://{}//data/domain/Room/{}"
WISERSCHEDULEURL = "http://{}/data/domain/Schedule/{}"


TEMP_MINIMUM = 5
TEMP_MAXIMUM = 30
TEMP_OFF = -20

TIMEOUT = 10
TIMEOUT = 5

__VERSION__ = "1.0.3"

Expand Down Expand Up @@ -122,6 +126,7 @@ def getRooms(self):
if self.wiserHubData==None:
self.refreshData()
return self.wiserHubData.get("Room")

def getRoom(self,roomId):
"""
Convinience to get data on a single room
Expand Down Expand Up @@ -274,7 +279,7 @@ def setHotwaterMode(self, mode):

def getRoomStatData(self,deviceId):
"""
Gets Roomt Thermostats Data
Gets Room Thermostats Data

param deviceId:
return:
Expand All @@ -287,8 +292,95 @@ def getRoomStatData(self,deviceId):
for roomStat in self.wiserHubData['RoomStat']:
if roomStat.get("id")==deviceId:
return roomStat
return None
return None

def getRoomSchedule(self,roomId):
"""
Gets Room Schedule Data

param roomId:
return: json data
"""
scheduleId = self.getRoom(roomId).get("ScheduleId")

if scheduleId is not None:
for schedule in (self.wiserHubData.get("Schedule")):
if (schedule.get("id")==scheduleId):
return schedule
return None
else:
return None

def setRoomSchedule(self, roomId, scheduleData: dict):
"""
Sets Room Schedule

param roomId:
param scheduleData: json data for schedule
return:
"""
scheduleId = self.getRoom(roomId).get("ScheduleId")

if scheduleId is not None:
self.patchData=scheduleData
self.response = requests.patch(url=WISERSCHEDULEURL.format(self.hubIP, scheduleId), headers=self.headers, json=self.patchData, timeout=TIMEOUT)

if (self.response.status_code!=200):
_LOGGER.debug("Set Schedule Response code = {}".format(self.response.status_code))
raise Exception("Error setting schedule for room {} , error {} {}".format(roomId, self.response.status_code, self.response.text))
else:
raise Exception("No schedule found that matches roomId")

def setRoomScheduleFromFile(self, roomId, scheduleFile: str):
"""
Sets Room Schedule

param roomId:
param scheduleData: json data for schedule
return:
"""
scheduleId = self.getRoom(roomId).get("ScheduleId")
scheduleData = ""

if scheduleId is not None:
if os.path.exists(scheduleFile):
try:
with open(scheduleFile, 'r') as f:
scheduleData = json.load(f)
except:
raise Exception("Error reading file {}".format(scheduleFile))

self.patchData=scheduleData
self.response = requests.patch(url=WISERSCHEDULEURL.format(self.hubIP, scheduleId), headers=self.headers, json=self.patchData, timeout=TIMEOUT)

if (self.response.status_code!=200):
_LOGGER.debug("Set Schedule Response code = {}".format(self.response.status_code))
raise Exception("Error setting schedule for room {} , error {} {}".format(roomId, self.response.status_code, self.response.text))
else:
raise Exception("Schedule file, {}, not found.".format(os.path.abspath(scheduleFile)))
else:
raise Exception("No schedule found that matches roomId")


def copyRoomSchedule(self, fromRoomId, toRoomId):
"""
Copies Room Schedule from one room to another

param fromRoomId:
param toRoomId:
return: boolean
"""
scheduleData = self.getRoomSchedule(fromRoomId)

print(json.dumps(scheduleData))

print("TYPE:{}".format(type(scheduleData)))

if scheduleData != None:
self.setRoomSchedule(toRoomId,scheduleData)
else:
raise Exception("Error copying schedule. One of the room Ids is not valid")

def setHomeAwayMode(self,mode,temperature=10):
"""
Sets default Home or Away mode, optionally allows you to set a temperature for away mode
Expand Down
29 changes: 27 additions & 2 deletions wiserapitest.py
Expand Up @@ -20,6 +20,7 @@

print (' Wiser Hub IP= {} , WiserKey= {}'.format(wiserip,wiserkey))

f.close

try:
#
Expand All @@ -30,7 +31,7 @@
# Heating State
print ("Hot water status {} ".format(wh.getHotwaterRelayStatus()))
print ("Roomstat humidity {}".format(wh.getRoomStatData(1).get("MeasuredHumidity")))

print("--------------------------------")
print ("Raw Room Data {} ".format(wh.getRooms()))
print("--------------------------------")
Expand All @@ -39,7 +40,31 @@
dev=wh.getDevices()
print (" Device Data {} ".format(dev))
print ("--------------------------------")


print("--------------------------------")
print ("Schedule output {}".format(wh.getRoomSchedule(4)))
print ("--------------------------------")


# Load schedule file and set
print("--------------------------------")
print("Set room schedule")
with open('dining.json', 'r') as f:
data = json.load(f)
wh.setRoomSchedule(4,data)
f.close
print("--------------------------------")

print("--------------------------------")
print("Set room schedule from file")
wh.setRoomScheduleFromFile(4, "./dining2.json")
print("--------------------------------")

print("--------------------------------")
print("Copy room schedule")
wh.copyRoomSchedule(4,3)
print("--------------------------------")

# List all Rooms

findValve=0
Expand Down