Ejemplo n.º 1
0
  def update_timer_data(self,weather):
    logger.debug('Updating timer data for %s running in mode: %s' % (self.get_type(),self.get_mode()))
    if self.in_weather_mode() or self.in_weather_inverse_mode():

      # Upate times based on weather
      if self.in_weather_inverse_mode():
        self.config['alarm_min']['timer_start']  = datetime.datetime.fromtimestamp(weather.get_sun_set())
        self.config['alarm_min']['timer_stop']   = datetime.datetime.fromtimestamp(weather.get_sun_rise())

      else:
        self.config['alarm_min']['timer_start']  = datetime.datetime.fromtimestamp(weather.get_sun_rise())
        self.config['alarm_min']['timer_stop']   = datetime.datetime.fromtimestamp(weather.get_sun_set())

      self.config['alarm_min']['timer_on'] = None
      self.config['alarm_min']['timer_off'] = None
      self.config['alarm_max']['timer_on'] = None
      self.config['alarm_max']['timer_off'] = None

      if self.get_type() == 'light':
        # Duration check
        duration = self.config['alarm_min']['timer_stop'] - self.config['alarm_min']['timer_start']
        # Reduce the amount of hours if to much
        if self.config['max_hours'] > 0 and duration > datetime.timedelta(hours=self.config['max_hours']):
          duration -= datetime.timedelta(hours=self.config['max_hours'])
          self.config['alarm_min']['timer_start'] += datetime.timedelta(seconds=duration.total_seconds()/2)
          self.config['alarm_min']['timer_stop'] -= datetime.timedelta(seconds=duration.total_seconds()/2)
        # Increase the amount of hours if to little
        elif self.config['min_hours'] > 0 and duration < datetime.timedelta(hours=self.config['min_hours']):
          duration = datetime.timedelta(hours=self.config['min_hours']) - duration
          self.config['alarm_min']['timer_start'] -= datetime.timedelta(seconds=duration.total_seconds()/2)
          self.config['alarm_min']['timer_stop'] += datetime.timedelta(seconds=duration.total_seconds()/2)

        # Shift hours
        self.config['alarm_min']['timer_start'] += datetime.timedelta(hours=self.config['hours_shift'])
        self.config['alarm_min']['timer_stop'] += datetime.timedelta(hours=self.config['hours_shift'])


      self.config['alarm_min']['timer_start'] = self.config['alarm_min']['timer_start'].strftime('%H:%M')
      self.config['alarm_min']['timer_stop'] = self.config['alarm_min']['timer_stop'].strftime('%H:%M')

      # Swap weather times..... for night period
      self.config['alarm_max']['timer_start'] = self.config['alarm_min']['timer_stop'] if self.get_type() == 'light' else self.config['alarm_min']['timer_start']
      self.config['alarm_max']['timer_stop'] = self.config['alarm_min']['timer_start'] if self.get_type() == 'light' else self.config['alarm_min']['timer_stop']

    if not self.in_sensor_mode() and len(list(self.config['alarm_min'].keys())) > 0:
      self.timer_min_data['time_table'] = terrariumUtils.calculate_time_table(self.config['alarm_min']['timer_start'],
                                                                              self.config['alarm_min']['timer_stop'],
                                                                              self.config['alarm_min']['timer_on'],
                                                                              self.config['alarm_min']['timer_off'])

      self.timer_min_data['duration'] = terrariumUtils.duration(self.timer_min_data['time_table'])

    if not self.in_sensor_mode() and len(list(self.config['alarm_max'].keys())) > 0:
      self.timer_max_data['time_table'] = terrariumUtils.calculate_time_table(self.config['alarm_max']['timer_start'],
                                                                              self.config['alarm_max']['timer_stop'],
                                                                              self.config['alarm_max']['timer_on'],
                                                                              self.config['alarm_max']['timer_off'])

      self.timer_max_data['duration'] = terrariumUtils.duration(self.timer_max_data['time_table'])
Ejemplo n.º 2
0
  def __calculate_time_table(self):
    self.__timer_time_table = []
    if self.get_start() is None or self.get_stop() is None:
      return

    logger.info('Calculating autioplaylist \'%s\' with timer data: start = %s, stop = %s',
      self.get_name(),
      self.get_start(),
      self.get_stop())

    duration_on = None
    if not self.get_repeat():
      duration_on = int(self.get_songs_duration()/60)

    self.__timer_time_table = terrariumUtils.calculate_time_table(self.get_start(),
                                                                  self.get_stop(),
                                                                  duration_on)
    logger.info('Timer time table loaded for autioplaylist \'%s\' with %s entries.', self.get_name(),len(self.__timer_time_table))
Ejemplo n.º 3
0
    def __calculate_time_table(self):
        self.__timer_time_table = []
        if self.state is None or \
           not self.get_timer_enabled():

            return False

        logger.info(
            'Calculating timer \'%s\' with timer data: enabled = %s, start = %s, stop = %s, on duration = %s, off duration = %s',
            self.get_name(), self.get_timer_enabled(), self.get_timer_start(),
            self.get_timer_stop(), self.get_timer_on_duration(),
            self.get_timer_off_duration())

        self.__timer_time_table = terrariumUtils.calculate_time_table(
            self.get_timer_start(), self.get_timer_stop(),
            self.get_timer_on_duration(), self.get_timer_off_duration())
        logger.info(
            'Timer time table loaded for switch \'%s\' with %s entries.',
            self.get_name(), len(self.__timer_time_table))
Ejemplo n.º 4
0
    def __update_timing(self, part=None):
        if part is None or part == 'light':
            on_duration = self.light['on_duration']
            off_duration = self.light['off_duration']

            if self.light['mode'] == 'weather':
                on_duration = off_duration = None

                # Upate times based on weather
                self.light['on'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['rise'])
                self.light['off'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['set'])

                # Duration check
                duration = self.light['off'] - self.light['on']
                # Reduce the amount of hours if to much
                if self.light[
                        'max_hours'] > 0 and duration > datetime.timedelta(
                            hours=self.light['max_hours']):
                    duration -= datetime.timedelta(
                        hours=self.light['max_hours'])
                    self.light['on'] += datetime.timedelta(
                        seconds=duration.total_seconds() / 2)
                    self.light['off'] -= datetime.timedelta(
                        seconds=duration.total_seconds() / 2)
                # Increase the amount of hours if to little
                elif self.light[
                        'min_hours'] > 0 and duration < datetime.timedelta(
                            hours=self.light['min_hours']):
                    duration = datetime.timedelta(
                        hours=self.light['min_hours']) - duration
                    self.light['on'] -= datetime.timedelta(
                        seconds=duration.total_seconds() / 2)
                    self.light['off'] += datetime.timedelta(
                        seconds=duration.total_seconds() / 2)

                # Shift hours
                self.light['on'] += datetime.timedelta(
                    hours=self.light['hours_shift'])
                self.light['off'] += datetime.timedelta(
                    hours=self.light['hours_shift'])

                self.light['on'] = self.light['on'].strftime('%H:%M')
                self.light['off'] = self.light['off'].strftime('%H:%M')

            self.light['time_table'] = terrariumUtils.calculate_time_table(
                self.light['on'], self.light['off'], on_duration, off_duration)

            self.light['duration'] = terrariumUtils.duration(
                self.light['time_table'])

        if part is None or part == 'sprayer':
            self.sprayer['time_table'] = terrariumUtils.calculate_time_table(
                self.sprayer['on'], self.sprayer['off'],
                self.sprayer['on_duration'], self.sprayer['off_duration'])

            self.sprayer['duration'] = terrariumUtils.duration(
                self.sprayer['time_table'])

        if part is None or part == 'watertank':
            self.watertank['time_table'] = terrariumUtils.calculate_time_table(
                self.watertank['on'], self.watertank['off'],
                self.watertank['on_duration'], self.watertank['off_duration'])

            self.watertank['duration'] = terrariumUtils.duration(
                self.watertank['time_table'])

        if part is None or part == 'heater':
            if self.heater['mode'] == 'weather':
                self.heater['on'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['set']).strftime('%H:%M')
                self.heater['off'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['rise']).strftime('%H:%M')

            self.heater['time_table'] = terrariumUtils.calculate_time_table(
                self.heater['on'], self.heater['off'],
                self.heater['on_duration'], self.heater['off_duration'])

            self.heater['duration'] = terrariumUtils.duration(
                self.heater['time_table'])

        if part is None or part == 'cooler':
            if self.cooler['mode'] == 'weather':
                self.cooler['on'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['rise']).strftime('%H:%M')
                self.cooler['off'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['set']).strftime('%H:%M')

            self.cooler['time_table'] = terrariumUtils.calculate_time_table(
                self.cooler['on'], self.cooler['off'],
                self.cooler['on_duration'], self.cooler['off_duration'])

            self.cooler['duration'] = terrariumUtils.duration(
                self.cooler['time_table'])