Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import inspect
- import json
- import os.path
- import socket
- import time
- import urllib2
- import ConfigParser
- from datetime import datetime, timedelta
- class HomeWizard(object):
- hwHost = ''
- hwPort = ''
- hwPassword = ''
- hwBaseURL = ''
- responseHttp = ''
- responseJson = ''
- responseStatus = ''
- responseSuccess = False
- getStatusJson = ''
- getSensorJson = ''
- #Constructor
- def __init__(self):
- pass
- # Date/Time Functions
- # Get DateTime from Time, with the current date
- def todayAt(self, hr, min=0, sec=0, micros=0):
- now = datetime.now()
- return now.replace(hour=hr, minute=min, second=sec, microsecond=micros)
- # Get DateTime from StringTime
- def getDateTimeFromStr(self, stringTime):
- tmpTime = time.strptime(stringTime,'%H:%M')
- tmpDateTime = self.todayAt(tmpTime[3], tmpTime[4])
- if tmpDateTime > datetime.now():
- tmpDateTime = tmpDateTime - timedelta(days=1)
- return tmpDateTime
- # Read Config
- def readconfig(self):
- config = ConfigParser.ConfigParser(allow_no_value=True)
- sourcePath = os.path.dirname(inspect.getsourcefile(self.readconfig))
- hwConfigFile = os.path.join(sourcePath,'homewizard.cfg')
- #Check if ConfigFile exists
- if not os.path.isfile(hwConfigFile):
- configExists = False
- print '\nERROR: Missing HomeWizard configfile\n'
- else:
- configExists = True
- #Read Config
- if configExists:
- config.read(hwConfigFile)
- try:
- self.hwHost = config.get('HomeWizard', 'hwHost')
- self.hwPort = config.get('HomeWizard', 'hwPort')
- self.hwPassword = config.get('HomeWizard', 'hwPassword')
- self.hwBaseURL = 'http://' + self.hwHost + ':' + self.hwPort + '/' + self.hwPassword
- except ConfigParser.NoOptionError, err:
- print '\nERROR: Could not parse config file\n'
- if self.hwHost and self.hwPort and self.hwPassword:
- configRead = True
- else:
- configRead = False
- return configExists and configRead
- # Handshake
- def handshake(self):
- try:
- self.responseHttp = urllib2.urlopen('http://' + self.hwHost + ':' + self.hwPort + '/handshake/html5/1/0', None, 5)
- except urllib2.HTTPError, err:
- print '\nERROR: Handshake HTTP Error'
- print 'Code : ' + str(err.code)
- print 'Reason: ' + err.reason + '\n'
- except urllib2.URLError, err:
- print '\nERROR: Handshake URL Error'
- print 'Reason: ' + str(err.reason) + '\n'
- except socket.timeout, err:
- print '\nERROR: Handshake Socket Timeout'
- if self.responseHttp:
- self.responseJson = json.load(self.responseHttp)
- self.responseStatus = self.responseJson['status']
- homewizard = self.responseJson['response']['homewizard']
- handshakeok = self.responseStatus == 'ok' and homewizard == 'yes'
- if not handshakeok:
- print '\nERROR: HomeWizard handshake failure\n'
- return handshakeok
- else:
- return False
- # Send cmd to HomeWizard
- def hwCmd (self, cmd):
- try:
- self.responseHttp = urllib2.urlopen(self.hwBaseURL + cmd, None, 5)
- except urllib2.HTTPError, err:
- print '\nERROR: hwCmd HTTP Error'
- print 'Code : ' + str(err.code)
- print 'Reason: ' + err.reason + '\n'
- except urllib2.URLError, err:
- print '\nERROR: hwCmd URL Error'
- print 'Reason: ' + str(err.reason) + '\n'
- except socket.timeout, err:
- print '\nERROR: hwCmd Socket Timeout'
- if self.responseHttp:
- self.responseJson = json.load(self.responseHttp)
- self.responseStatus = self.responseJson['status']
- self.responseSuccess = self.responseStatus == 'ok'
- return self.responseSuccess
- else:
- return False
- # Get Status
- def getStatus (self):
- cmd = '/get-status'
- success = self.hwCmd(cmd)
- if success:
- self.getStatusJson = self.responseJson
- else:
- self.getStatusJson = ''
- return success
- # Check Status
- def checkStatus (self):
- # Execute getStatus if statusinfo isn't present
- if not self.getStatusJson:
- success = self.getStatus()
- else:
- success = True
- return success
- # Get Sensors
- def getSensors (self):
- cmd = '/get-sensor'
- success = self.hwCmd(cmd)
- if success:
- self.getSensorJson = self.responseJson
- else:
- self.getSensorJson = ''
- return success
- # Switch Status
- def switchStatus (self, switchID):
- # Check if statusinfo is present
- success = self.checkStatus()
- # Get Switch Status
- if success:
- switches = self.responseJson['response']['switches']
- switch = [sw for sw in switches if sw['id']==switchID][0]
- switchStatus = switch['status']
- switchOn = switchStatus == 'on'
- return success, switchOn
- else:
- return False, False
- # SwitchOn a HomeWizard Switch
- def switchOn (self, switchID):
- cmd = '/sw/' + str(switchID) + '/on'
- success = self.hwCmd(cmd)
- return success
- # SwitchOff a HomeWizard Switch
- def switchOff (self, switchID):
- cmd = '/sw/' + str(switchID) + '/off'
- success = self.hwCmd(cmd)
- return success
- # Switch Status
- def sensorStatus (self, sensorID):
- # Check if statusinfo is present
- success = self.checkStatus()
- # Get Sensor Status
- if success:
- sensors = self.responseJson['response']['kakusensors']
- sensor = [sw for sw in sensors if sw['id']==sensorID][0]
- sensorStatus = sensor['status']
- sensorTimeStr = sensor['timestamp']
- sensorOn = sensorStatus == 'yes'
- sensorDateTime = self.getDateTimeFromStr(sensorTimeStr)
- return success, sensorOn, sensorTimeStr, sensorDateTime
- else:
- return False, False, '00:00', datetime(1901,1,1,0,0,0,0)
- # EnergyLink Day Values
- def energylinkDay (self):
- cmd = '/el/graph/0/day'
- success = self.hwCmd(cmd)
- if success:
- return success, self.responseJson['response']
- else:
- return success, ''
- # Get Thermometer Temperature
- def thermoGetTemp (self, thermometerID):
- # Check if statusinfo is present
- success = self.checkStatus()
- # Get Temp
- if success:
- thermometers = self.responseJson['response']['thermometers']
- thermometer = [sw for sw in thermometers if sw['id']==thermometerID][0]
- thermoTemp = thermometer['te']
- return success, thermoTemp
- else:
- return success, 0
- # HeatLink Set Temp
- def heatlinkSetTemp (self, temperature, minutes):
- cmd = '/hl/0/settarget/' + str(temperature)
- if minutes > 0:
- cmd = cmd + '/' + str(minutes)
- success = self.hwCmd(cmd)
- return success
- # HeatLink Get Temp
- def heatlinkGetTemp (self):
- # Check if statusinfo is present
- success = self.checkStatus()
- # Get Sensor Status
- if success:
- heatlinks = self.responseJson['response']['heatlinks']
- heatlink = [sw for sw in heatlinks if sw['id']==0][0]
- hlRoomTemp = heatlink['rte']
- hlTempHeatlink = heatlink['tte']
- hlTempThermostat = heatlink['rsp']
- return success, hlRoomTemp, hlTempHeatlink, hlTempThermostat
- else:
- return success, 0, 0, 0
Advertisement
Add Comment
Please, Sign In to add comment