Example #1
0
def exampleSolarTimeOfDay(event):
    time.sleep(2)  # wait for Items to update
    dawn_start = DateTime(items['V_CivilDawn'].toString())
    day_start = DateTime(items['V_Sunrise'].toString())
    dusk_start = DateTime(items['V_CivilDuskStart'].toString())
    night_start = DateTime(items['V_CivilDuskEnd'].toString())

    curr = None
    now = DateTime.now()
    exampleSolarTimeOfDay.log.debug("dawn_start  [{}]".format(dawn_start))
    exampleSolarTimeOfDay.log.debug("day_start   [{}]".format(day_start))
    exampleSolarTimeOfDay.log.debug("dusk_start  [{}]".format(dusk_start))
    exampleSolarTimeOfDay.log.debug("night_start [{}]".format(night_start))
    exampleSolarTimeOfDay.log.debug("now         [{}]".format(now))

    if now.isAfter(dawn_start) and now.isBefore(day_start):
        curr = SOLARTIME['DAWN']
    elif now.isAfter(day_start) and now.isBefore(dusk_start):
        curr = SOLARTIME['DAY']
    elif now.isAfter(dusk_start) and now.isBefore(night_start):
        curr = SOLARTIME['DUSK']
    else:
        curr = SOLARTIME['NIGHT']

    if postUpdateCheckFirst('V_SolarTime', curr):
        exampleSolarTimeOfDay.log.info("Solar time is now [{}]".format(
            kw(SOLARTIME, curr)))
Example #2
0
def exampleTimeOfDay(event):
    # Get the time period start times for today
    now = DateTime.now()
    morningStart = now.withTime(timeOfDay['morningStart']['Hour'],
                                timeOfDay['morningStart']['Minute'], 0,
                                0).toInstant()
    dayStart = now.withTime(timeOfDay['dayStart']['Hour'],
                            timeOfDay['dayStart']['Minute'], 0, 0).toInstant()
    eveningStart = now.withTime(timeOfDay['eveningStart']['Hour'],
                                timeOfDay['eveningStart']['Minute'], 0,
                                0).toInstant()
    nightStart = now.withTime(timeOfDay['nightStart']['Hour'],
                              timeOfDay['nightStart']['Minute'], 0,
                              0).toInstant()

    timeOfDay = TIMEOFDAY['NIGHT']
    if (now.isAfter(morningStart) and now.isBefore(dayStart)):
        timeOfDay = TIMEOFDAY['MORNING']
    elif (now.isAfter(dayStart) and now.isBefore(eveningStart)):
        timeOfDay = TIMEOFDAY['DAY']
    elif (now.isAfter(eveningStart) and now.isBefore(nightStart)):
        timeOfDay = TIMEOFDAY['EVENING']

    if postUpdateCheckFirst('V_TimeOfDay', timeOfDay):
        exampleTimeOfDay.log.debug("Time of day now: [{}]".format(
            kw(TIMEOFDAY, timeOfDay)))
    def setArmingMode(self, newArmingMode, sendCommand=False):
        '''
        Sets the zones current arming mode
        '''
        oldArmingMode = self._armingMode

        if newArmingMode not in [
                ARMINGMODE['DISARMED'], ARMINGMODE['ARMED_HOME'],
                ARMINGMODE['ARMED_AWAY']
        ]:
            raise IdeAlarmError(
                "Trying to set an invalid arming mode: {}".format(
                    newArmingMode))

        # There might be open sensors when trying to arm. If so, the custom function onArmingWithOpenSensors
        # gets called. (That doesn't necessarily need to be an error condition).
        # However if the zone has been configured not to allow opened sensors during arming,
        # the zone status will be set to ERROR and onZoneStatusChange will be able to trap track it down.
        if newArmingMode in [ARMINGMODE['ARMED_AWAY'], ARMINGMODE['ARMED_HOME']] \
        and self.getZoneStatus() != ZONESTATUS['ARMING'] and self.getZoneStatus() is not None \
        and self.openSections > 0:
            if 'onArmingWithOpenSensors' in dir(custom):
                custom.onArmingWithOpenSensors(self, newArmingMode)
            if not self.canArmWithTrippedSensors:
                self.setZoneStatus(
                    ZONESTATUS['ERROR'],
                    errorMessage='Arming is not allowed with open sensors')
                self.log.warn(
                    u"Zone \'{}'\' can not be set to new arming mode: {} due to that there are open sensors!"
                    .format(self.name.decode('utf8'),
                            kw(ARMINGMODE, newArmingMode)))
                import time
                time.sleep(1)
                self.setZoneStatus(ZONESTATUS['NORMAL'])
                return

        # Don't set arming mode to 'ARMED_AWAY' immediately, we need to wait for the exit timer
        # self.getZoneStatus() returns None when initializing
        if newArmingMode == ARMINGMODE['ARMED_AWAY'] \
        and self.getZoneStatus() is not None and self.getZoneStatus() != ZONESTATUS['ARMING']:
            self.setZoneStatus(ZONESTATUS['ARMING'])
            post_update_if_different("Z{}_Exit_Timer".format(self.zoneNumber),
                                     scope.ON)
            return
        self._armingMode = newArmingMode

        # Sync the Item
        post_update_if_different(self.armingModeItem, newArmingMode,
                                 sendCommand)

        # Call custom function if available
        if 'onArmingModeChange' in dir(custom):
            custom.onArmingModeChange(self, newArmingMode, oldArmingMode)

        # Whenever the arming mode is set, reset the zones status to NORMAL
        self.setZoneStatus(ZONESTATUS['NORMAL'])
    def onToggleSwitch(self, itemName):
        '''
        Called whenever an alarm arming mode toggle switch has been switched.
        '''
        newArmingMode = None
        if itemName == self.armAwayToggleSwitch:
            if self.getArmingMode() in [ARMINGMODE['DISARMED']]:
                newArmingMode = ARMINGMODE['ARMED_AWAY']
            else:
                newArmingMode = ARMINGMODE['DISARMED']
        else:
            if self.getArmingMode() in [ARMINGMODE['DISARMED']]:
                newArmingMode = ARMINGMODE['ARMED_HOME']
            else:
                newArmingMode = ARMINGMODE['DISARMED']

        self.log.debug(u"Toggling zone [{}] to new arming mode: [{}]".format(
            self.name.decode('utf8'), kw(ARMINGMODE, newArmingMode)))
        self.setArmingMode(newArmingMode)