Example #1
0
	def validateAlarmTimes (self, dev, startTime = None, endTime = None):
		if dev.states["isActive"]: 
			dev.updateStateOnServer("statedisplay", "Active")	
			self.debugLog(u"%s is currently in an active state, skipping calculation" % dev.name)
			return # If the alarm is active don't recalc anything, it'll throw off the stop time (like if they reload the plugin)
	
		if startTime is None: startTime = datetime.datetime.strptime (dev.states["startTime"], "%Y-%m-%d %H:%M:%S")
		if endTime is None: endTime = datetime.datetime.strptime (dev.states["endTime"], "%Y-%m-%d %H:%M:%S")
		
		self.debugLog (u"Checking if the alarm %s to %s is valid" % (unicode(startTime), unicode(endTime)))
		
		d = indigo.server.getTime()
		minutesUntilStart = dtutil.DateDiff ("minutes", startTime, d)
		lastCalc = dtutil.DateAdd ("days", -1, d) # if we update anything roll back lastcalc to force it to update immediately
		
		self.auditDays (dev)
		
		if eps.stateValid (dev, "isOn"):
			if dev.states["isOn"]:
				dev.updateStateImageOnServer(indigo.kStateImageSel.TimerOn)
				
				if minutesUntilStart > 0:
					self.debugLog(u"\tthe alarm is currently enabled and in the future, verifying validity")
					if self.dayIsValid (dev, startTime):
						self.debugLog(u"\t\tthe alarm is valid")
						
						# Set the end time for good measure and to be safe
						endTime = self.setAlarmEndTime (dev, startTime)
						dev.updateStateOnServer("startTime", startTime.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(startTime, dev.pluginProps["militaryTime"]))
						dev.updateStateOnServer("endTime", endTime.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(endTime, dev.pluginProps["militaryTime"]))
						dev.updateStateOnServer("lastCalc", lastCalc.strftime("%Y-%m-%d %H:%M:%S"))
						return
						
					else:
						self.debugLog(u"\t\tthe alarm is not valid, recalculating alarm start date/time")
						startTime = self.getNextStartTime (dev, startTime)
						
				else:
					# The alarm is in the past, at best it cannot happen again until tomorrow, so try that
					self.debugLog(u"\tthe alarm is in the past, validating for tomorrow")
					self.validateAlarmTimes (dev, dtutil.DateAdd("days", 1, startTime))
					return

			else:
				dev.updateStateImageOnServer(indigo.kStateImageSel.TimerOff)
				dev.updateStateOnServer("timeUntilOn", "00:00") # Failsafe
				dev.updateStateOnServer("timeUntilOff", "00:00") # Failsafe
				dev.updateStateOnServer("statedisplay", "Off") # Failsafe
				dev.updateStateOnServer("startTime", startTime.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(startTime, dev.pluginProps["militaryTime"]))
				dev.updateStateOnServer("endTime", endTime.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(endTime, dev.pluginProps["militaryTime"]))	
				
				self.debugLog(u"\tthe alarm is currently not enabled, not verifying since it will be verified when it is turned on")
				return

		else:
			self.debugLog(u"\tthe isOn state is missing, this could be a new device")		
			return
						
					
		# If we get here then we are ready to finish up, start with adding our end time
		endTime = self.setAlarmEndTime (dev, startTime)
		self.debugLog (u"\tthe alarm will now be %s to %s" % (unicode(startTime), unicode(endTime)))
		
		# Write all of our settings to the device and set the last check time to yesterday to insure an immediate update
		dev.updateStateOnServer("startTime", startTime.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(startTime, dev.pluginProps["militaryTime"]))
		dev.updateStateOnServer("endTime", endTime.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(endTime, dev.pluginProps["militaryTime"]))
		
		dev.updateStateOnServer("lastCalc", lastCalc.strftime("%Y-%m-%d %H:%M:%S"))
		
		return
Example #2
0
	def deviceStartComm(self, dev):
		self.debugLog(u"%s starting communication" % dev.name)
		dev.stateListOrDisplayStateIdChanged() # Force plugin to refresh states from devices.xml
		if self.cache is None: return
		
		if "lastreset" in dev.states:
			d = indigo.server.getTime()
			if dev.states["lastreset"] == "": dev.updateStateOnServer("lastreset", d.strftime("%Y-%m-%d "))
			
		if "lastCalc" in dev.states:
			d = indigo.server.getTime()
			if dev.states["lastCalc"] == "": dev.updateStateOnServer("lastCalc", d.strftime("%Y-%m-%d %H:%M:%S"))
			
		if eps.valueValid (dev.states, "statedisplay", True):
			self.debugLog("Device doesn't have a valid device state, setting the state now")
			
			# There is no state display, set it now - 1.1.1
			if dev.states["isOn"]:
				dev.updateStateOnServer("statedisplay", "On")
				dev.updateStateImageOnServer(indigo.kStateImageSel.TimerOn)
			else:
				dev.updateStateOnServer("statedisplay", "Off")
				dev.updateStateImageOnServer(indigo.kStateImageSel.TimerOff)
			
		if eps.stateValid (dev, "startTime", True) == False: 
			self.debugLog("Device doesn't have a start time, creating a default alarm for 8:00 AM")
			
			# added in 1.1.1 or creating the start date will fail because there are no days
			self.auditDays (dev)
			resetday = dev.states["isSaturday"]
			dev.updateStateOnServer ("isSaturday", True) # failsafe			
			
			d = indigo.server.getTime()
			t = datetime.datetime.strptime(d.strftime("%Y-%m-%d") + " 08:00:00", "%Y-%m-%d %H:%M:%S")
			#t = self.proposedFutureTime (t, dev)
			t = self.getNextStartTime (dev, t) # 1.1.1
			
			dev.updateStateOnServer("startTime", t.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(t, dev.pluginProps["militaryTime"]))		
			
			dev.updateStateOnServer ("isSaturday", resetday) # failsafe	
			
		if eps.stateValid (dev, "endTime", True) == False: 
			self.debugLog("Device doesn't have an end time, calculating the next available end time from the start time")
			d = datetime.datetime.strptime (dev.states["startTime"], "%Y-%m-%d %H:%M:%S")
			#t = dtutil.DateAdd("minutes", int(dev.pluginProps["defaultDuration"]), d)			
			#t = self.proposedFutureTime (t, dev, t.strftime("%Y-%m-%d %H:%M:%S"))
			t = self.setAlarmEndTime (dev, d) # 1.1.1
			
			dev.updateStateOnServer("endTime", t.strftime ("%Y-%m-%d %H:%M:%S"), uiValue=self.dtToString(t, dev.pluginProps["militaryTime"]))	
			
		if "durationMinutes" in dev.states:
			if dev.states["durationMinutes"] == 0: dev.updateStateOnServer("durationMinutes", int(dev.pluginProps["defaultDuration"]))	
				
		if self.cache.deviceInCache (dev.id) == False:
			self.debugLog(u"%s not in cache, appears to be a new device or plugin was just started" % dev.name)
			self.cache.cacheDevices() # Failsafe
		
		
		
		#dev.updateStateOnServer("startTime", "2016-06-08 17:00:00") # Debug testing
		self.validateAlarmTimes(dev)
			
		self.addWatchedStates("*", dev.deviceTypeId, dev.id) # Failsafe
		#self.cache.dictDump (self.cache.devices[dev.id])
		#indigo.server.log(unicode(dev.states))
		X = 1 # placeholder
			
		return