예제 #1
0
    def trigger(self, *args, **kwargs):
        commands = kwargs.get(Attribute.COMMAND, None)
        sources = kwargs.get(Attribute.SOURCE, None)
        mapped = kwargs.get(Attribute.MAPPED, None)
        secs = kwargs.get(Attribute.SECS, None)
        start = kwargs.get(Attribute.START, None)
        end = kwargs.get(Attribute.END, None)

        if not isinstance(commands, tuple):
            commands = (commands, )
        if not isinstance(sources, tuple):
            sources = (sources, )

        for command in commands:
            for source in sources:
                m = None
                if not mapped:
                    m = command
                else:
                    m = mapped
                self._triggers.update({(command, source): {Attribute.SECS: secs,
                                                           Attribute.MAPPED: m,
                                                           Attribute.START: CronTimer.to_cron(start),
                                                           Attribute.END: CronTimer.to_cron(end),
                                                           }})
예제 #2
0
    def restriction(self, *args, **kwargs):
        states = kwargs.get(Attribute.STATE, None)
        sources = kwargs.get(Attribute.SOURCE, None)
        targets = kwargs.get(Attribute.TARGET, None)
        start = kwargs.get(Attribute.START, None)
        end = kwargs.get(Attribute.END, None)

        if not isinstance(states, tuple):
            states = (states, )
        if not isinstance(sources, tuple):
            sources = (sources, )
        if not isinstance(targets, tuple):
            targets = (targets, )

        for state in states:
            for source in sources:
                for target in targets:
                    self._restrictions.update({
                                          (state, source, target): {
                                                              Attribute.START: CronTimer.to_cron(start),
                                                             Attribute.END: CronTimer.to_cron(end),
                                                         }
                                          })
                    self._logger.debug("{name} add restriction for {state} from {source} on {target}".format(
                                                    name=self.name,
                                                    state=state,
                                                    target=target,
                                                    source=source.name if source else None,
                                                    ));
예제 #3
0
    def _recalc(self):
        self.obs.horizon = self._horizon

        #        midnight = self.tz.localize(local_time.replace(hour=12, minute=0, second=0, microsecond=0, tzinfo=None),
        #                               is_dst=None)
        #        self.obs.date = midnight.astimezone(pytz.utc)

        self.obs.date = self.local_time.astimezone(pytz.utc)

        prev_rising = self._utc2tz(
            self.obs.previous_rising(self.sun, use_center=True).datetime())
        prev_setting = self._utc2tz(
            self.obs.previous_setting(self.sun, use_center=True).datetime())
        self._sunrise = self._utc2tz(
            self.obs.next_rising(self.sun, use_center=True).datetime())
        self._sunset = self._utc2tz(
            self.obs.next_setting(self.sun, use_center=True).datetime())
        self._sunrise = self._sunrise.replace(second=0, microsecond=0)
        self._sunset = self._sunset.replace(second=0, microsecond=0)
        self._logger.info(
            '{name} Location sunset: {sunset} sunrise: {sunrise}'.format(
                name=self.name,
                sunset=str(self._sunset),
                sunrise=str(self._sunrise),
            ))
        time_now = self.local_time.replace(second=0, microsecond=0)
        if (self._sunrise > self._sunset and self._sunset != time_now) or \
            self._sunrise == time_now:
            if self.state != Command.LIGHT:
                self.light()
            else:
                self._logger.info(
                    "{name} Location did not flip state as it already is light"
                    .format(name=self.name))
        else:
            if self.state != Command.DARK:
                self.dark()
            else:
                self._logger.info(
                    "{name} Location did not flip state as it already is dark".
                    format(name=self.name))

        # Setup trigger for next transition
        self._sunset_timer.interval(
            *CronTimer.to_cron(strftime("%H:%M:%S", self.sunset.timetuple())))
        self._sunset_timer.action(self._recalc)
        self._sunset_timer.start()

        self._sunrise_timer.interval(
            *CronTimer.to_cron(strftime("%H:%M:%S", self.sunrise.timetuple())))
        self._sunrise_timer.action(self._recalc)
        self._sunrise_timer.start()
예제 #4
0
    def _recalc(self):
        self.obs.horizon = self._horizon

#        midnight = self.tz.localize(local_time.replace(hour=12, minute=0, second=0, microsecond=0, tzinfo=None), 
#                               is_dst=None)
#        self.obs.date = midnight.astimezone(pytz.utc) 

        self.obs.date = self.local_time.astimezone(pytz.utc)

        prev_rising = self._utc2tz(
                                  self.obs.previous_rising(self.sun, use_center=True).datetime())
        prev_setting = self._utc2tz(
                                  self.obs.previous_setting(self.sun, use_center=True).datetime())
        self._sunrise = self._utc2tz(
                                     self.obs.next_rising(self.sun, use_center=True).datetime())
        self._sunset = self._utc2tz(
                                     self.obs.next_setting(self.sun, use_center=True).datetime())
        self._sunrise = self._sunrise.replace(second=0, microsecond=0)
        self._sunset = self._sunset.replace(second=0, microsecond=0)
        self._logger.info('{name} Location sunset: {sunset} sunrise: {sunrise}'.format(
                                                                                       name=self.name,
                                                                                       sunset=str(self._sunset),
                                                                                       sunrise=str(self._sunrise),
                                                                                       ))
        time_now = self.local_time.replace(second=0, microsecond=0)
        if (self._sunrise > self._sunset and self._sunset != time_now) or \
            self._sunrise == time_now:
            if self.state <> Command.LIGHT:
                self.light()
            else:
                self._logger.info("{name} Location did not flip state as it already is light".format(
                                                                                                     name=self.name
                                                                                                     ))
        else:
            if self.state <> Command.DARK:
                self.dark()
            else:
                self._logger.info("{name} Location did not flip state as it already is dark".format(
                                                                                                     name=self.name
                                                                                                     ))

                         
        # Setup trigger for next transition
        self._sunset_timer.interval(*CronTimer.to_cron(strftime("%H:%M:%S", self.sunset.timetuple())))
        self._sunset_timer.action(self._recalc)
        self._sunset_timer.start()

        self._sunrise_timer.interval(*CronTimer.to_cron(strftime("%H:%M:%S", self.sunrise.timetuple())))
        self._sunrise_timer.action(self._recalc)
        self._sunrise_timer.start()
예제 #5
0
    def ignore(self, *args, **kwargs):
        commands = kwargs.get('command', None)
        sources = kwargs.get('source', None)
        start = kwargs.get(Attribute.START, None)
        end = kwargs.get(Attribute.END, None)

        if not isinstance(commands, tuple):
            commands = (commands, )
        if not isinstance(sources, tuple):
            sources = (sources, )

        for command in commands:
            for source in sources:
                self._ignores.update({
                                      (command, source): {
                                                          Attribute.START: CronTimer.to_cron(start),
                                                         Attribute.END: CronTimer.to_cron(end),
                                                     }
                                      })
                self._logger.debug("{name} add ignore for {command} from {source}".format(
                                                                                        name=self.name,
                                                                                        command=command,
                                                                                        source=source.name if source else None,
                                                                                        ));
예제 #6
0
    def time(self, *args, **kwargs):
        # time, command
        times = kwargs.get('time', None)
        command = kwargs.get('command', State.UNKNOWN)

        if times:
            if not isinstance( times, tuple) or (isinstance(times, tuple) and isinstance(times[0], int)):
                times = (times, )
            for time in times:
                timer = CronTimer()
                if isinstance(time, tuple):
                    timer.interval(*time)
                else:
                    timer.interval(*CronTimer.to_cron(time))
                timer.action(self.command, (command))
                timer.start()
                self._times.append((command, timer))
예제 #7
0
    def test_datetime_to_cron(self):
        cron = CronTimer.to_cron('5:34pm')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 34)
        self.assertEqual(cron[2], 17)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('6:52 pm')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 52)
        self.assertEqual(cron[2], 18)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('5:13 AM')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 13)
        self.assertEqual(cron[2], 5)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('5:13:34 AM')
        self.assertEqual(cron[0], 34)
        self.assertEqual(cron[1], 13)
        self.assertEqual(cron[2], 5)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('3:14')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 14)
        self.assertEqual(cron[2], 3)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('18:42')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 42)
        self.assertEqual(cron[2], 18)
        self.assertEqual(cron[4], AllMatch())
예제 #8
0
    def test_datetime_to_cron(self):
        cron = CronTimer.to_cron('5:34pm')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 34)
        self.assertEqual(cron[2], 17)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('6:52 pm')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 52)
        self.assertEqual(cron[2], 18)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('5:13 AM')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 13)
        self.assertEqual(cron[2], 5)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('5:13:34 AM')
        self.assertEqual(cron[0], 34)
        self.assertEqual(cron[1], 13)
        self.assertEqual(cron[2], 5)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('3:14')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 14)
        self.assertEqual(cron[2], 3)
        self.assertEqual(cron[4], AllMatch())

        cron = CronTimer.to_cron('18:42')
        self.assertEqual(cron[0], 0)
        self.assertEqual(cron[1], 42)
        self.assertEqual(cron[2], 18)
        self.assertEqual(cron[4], AllMatch())
예제 #9
0
 def test_timefuncs_in_range(self):
     start = CronTimer.to_cron("4:52pm")
     end = CronTimer.to_cron("4:55pm")
     now = CronTimer.to_cron("4:54pm")
     self.assertTrue(crontime_in_range(now, start, end))
예제 #10
0
 def test_timefuncs_out_range_flip(self):
     start = CronTimer.to_cron("10:03pm")
     end = CronTimer.to_cron("4:55am")
     now = CronTimer.to_cron("2:54pm")
     self.assertFalse(crontime_in_range(now, start, end))
예제 #11
0
 def test_timefuncs_out_range2(self):
     start = CronTimer.to_cron("12:01am")
     end = CronTimer.to_cron("8:00am")
     now = CronTimer.to_cron("5:54pm")
     self.assertFalse(crontime_in_range(now, start, end))
예제 #12
0
 def test_timefuncs_in_range(self):
     start = CronTimer.to_cron("4:52pm")
     end = CronTimer.to_cron("4:55pm")
     now = CronTimer.to_cron("4:54pm")
     self.assertTrue(crontime_in_range(now, start, end))
예제 #13
0
 def test_timefuncs_out_range_flip(self):
     start = CronTimer.to_cron("10:03pm")
     end = CronTimer.to_cron("4:55am")
     now = CronTimer.to_cron("2:54pm")
     self.assertFalse(crontime_in_range(now, start, end))
예제 #14
0
 def test_timefuncs_out_range2(self):
     start = CronTimer.to_cron("12:01am")
     end = CronTimer.to_cron("8:00am")
     now = CronTimer.to_cron("5:54pm")
     self.assertFalse(crontime_in_range(now, start, end))