Ejemplo n.º 1
0
    def __init__(self):
        self._programs = []
        self.run_now_program = None

        i = 0
        while options.available(_Program, i):
            self._programs.append(_Program(self, i))
            i += 1

        options.add_callback('output_count', self._option_cb)
        weather.add_callback(self._weather_cb)
Ejemplo n.º 2
0
    def __init__(self):
        self._programs = []
        self.run_now_program = None

        i = 0
        while options.available(_Program, i):
            self._programs.append(_Program(self, i))
            i += 1

        options.add_callback('output_count', self._option_cb)
        weather.add_callback(self._weather_cb)
Ejemplo n.º 3
0
    def run(self):
        weather.add_callback(self.update)
        self._sleep(10)  # Wait for weather callback before starting
        while not self._stop_event.is_set():
            try:
                log.clear(NAME)
                if plugin_options['enabled']:
                    log.debug(NAME, _(u'Checking weather status') + '...')

                    info = []
                    days = 0
                    total_info = {'rain_mm': 0.0}
                    for day in range(-plugin_options['days_history'],
                                     plugin_options['days_forecast'] + 1):
                        check_date = datetime.date.today(
                        ) + datetime.timedelta(days=day)
                        hourly_data = weather.get_hourly_data(check_date)
                        if hourly_data:
                            days += 1
                        info += hourly_data

                        total_info['rain_mm'] += weather.get_rain(check_date)

                    log.info(
                        NAME,
                        _(u'Using') + ' %d ' % days +
                        _(u'days of information.'))

                    total_info.update({
                        'temp_c':
                        sum([val['temperature'] for val in info]) / len(info),
                        'wind_ms':
                        sum([val['windSpeed'] for val in info]) / len(info),
                        'humidity':
                        sum([val['humidity'] for val in info]) / len(info)
                    })

                    # We assume that the default 100% provides 4mm water per day (normal need)
                    # We calculate what we will need to provide using the mean data of X days around today

                    water_needed = 4 * days  # 4mm per day
                    water_needed *= 1 + (total_info['temp_c'] -
                                         20) / 15  # 5 => 0%, 35 => 200%
                    water_needed *= 1 + (total_info['wind_ms'] / 100
                                         )  # 0 => 100%, 20 => 120%
                    water_needed *= 1 - (total_info['humidity'] -
                                         50) / 200  # 0 => 125%, 100 => 75%
                    water_needed = round(water_needed, 1)

                    water_left = water_needed - total_info['rain_mm']
                    water_left = round(max(0, min(100, water_left)), 1)

                    water_adjustment = round((water_left / (4 * days)) * 100,
                                             1)

                    water_adjustment = float(
                        max(plugin_options['wl_min'],
                            min(plugin_options['wl_max'], water_adjustment)))

                    log.info(
                        NAME,
                        _(u'Water needed') + ' %d ' % days + _('days') +
                        ': %.1fmm' % water_needed)
                    log.info(
                        NAME,
                        _(u'Total rainfall') +
                        ': %.1fmm' % total_info['rain_mm'])
                    log.info(NAME, u'_______________________________')
                    log.info(NAME,
                             _(u'Irrigation needed') + ': %.1fmm' % water_left)
                    log.info(
                        NAME,
                        _(u'Weather Adjustment') +
                        ': %.1f%%' % water_adjustment)

                    level_adjustments[NAME] = water_adjustment / 100

                    if plugin_options['protect_enabled']:
                        current_data = weather.get_current_data()
                        temp_local_unit = current_data[
                            'temperature'] if options.temp_unit == "C" else 32.0 + 9.0 / 5.0 * current_data[
                                'temperature']
                        log.debug(
                            NAME,
                            _(u'Temperature') + ': %.1f %s' %
                            (temp_local_unit, options.temp_unit))
                        month = time.localtime().tm_mon  # Current month.
                        if temp_local_unit < plugin_options[
                                'protect_temp'] and month in plugin_options[
                                    'protect_months']:
                            station_seconds = {}
                            for station in stations.enabled_stations():
                                if station.index in plugin_options[
                                        'protect_stations']:
                                    station_seconds[
                                        station.index] = plugin_options[
                                            'protect_minutes'] * 60
                                else:
                                    station_seconds[station.index] = 0

                            for station in stations.enabled_stations():
                                if run_once.is_active(datetime.datetime.now(),
                                                      station.index):
                                    break
                            else:
                                log.debug(NAME, _(u'Protection activated.'))
                                run_once.set(station_seconds)

                    self._sleep(3600)

                else:
                    log.clear(NAME)
                    log.info(NAME, _(u'Plug-in is disabled.'))
                    if NAME in level_adjustments:
                        del level_adjustments[NAME]
                    self._sleep(24 * 3600)

            except Exception:
                log.error(
                    NAME,
                    _(u'Weather-based water level plug-in') + ':\n' +
                    traceback.format_exc())
                self._sleep(3600)
        weather.remove_callback(self.update)
Ejemplo n.º 4
0
    def run(self):
        weather.add_callback(self.update)
        self._sleep(10)  # Wait for weather callback before starting
        while not self._stop_event.is_set():
            try:
                log.clear(NAME)
                if plugin_options['enabled']:
                    log.debug(NAME, _(u'Checking weather status') + '...')

                    if plugin_options['use_netatmo']:
                        authorization = ClientAuth()
                        devList = WeatherStationData(authorization)
                        now = time.time()
                        begin = now - (
                            plugin_options['days_history']) * 24 * 3600
                        mac2 = plugin_options['netatmomac']
                        rainmac2 = plugin_options['netatmorain']
                        resp = (devList.getMeasure(mac2,
                                                   '1hour',
                                                   'sum_rain',
                                                   rainmac2,
                                                   date_begin=begin,
                                                   date_end=now,
                                                   limit=None,
                                                   optimize=False))
                        result = [(time.ctime(int(k)), v[0])
                                  for k, v in resp['body'].items()]
                        result.sort()
                        xdate, xrain = zip(*result)
                        zrain = 0
                        for yrain in xrain:
                            zrain = zrain + yrain
                    else:
                        zrain = 0

                    info = []
                    days = 0
                    total_info = {'rain_mm': 0.0}
                    for day in range(-plugin_options['days_history'],
                                     plugin_options['days_forecast'] + 1):
                        check_date = datetime.date.today(
                        ) + datetime.timedelta(days=day)
                        hourly_data = weather.get_hourly_data(check_date)
                        if hourly_data:
                            days += 1
                        info += hourly_data

                        total_info['rain_mm'] += weather.get_rain(check_date)

                    log.info(
                        NAME,
                        _(u'Using') + ' %d ' % days +
                        _(u'days of information.'))

                    total_info.update({
                        'temp_c':
                        sum([val['temperature'] for val in info]) / len(info),
                        'wind_ms':
                        sum([val['windSpeed'] for val in info]) / len(info),
                        'humidity':
                        sum([val['humidity'] for val in info]) / len(info)
                    })

                    # We assume that the default 100% provides 4mm water per day (normal need)
                    # We calculate what we will need to provide using the mean data of X days around today

                    water_needed = 4 * days  # 4mm per day
                    water_needed *= 1 + (total_info['temp_c'] -
                                         20) / 15  # 5 => 0%, 35 => 200%
                    water_needed *= 1 + (total_info['wind_ms'] / 100
                                         )  # 0 => 100%, 20 => 120%
                    water_needed *= 1 - (total_info['humidity'] -
                                         50) / 200  # 0 => 125%, 100 => 75%
                    water_needed = round(water_needed, 1)

                    water_left = water_needed - total_info['rain_mm']
                    water_left = round(max(0, min(100, water_left)), 1)

                    water_adjustment = round((water_left / (4 * days)) * 100,
                                             1)

                    level_adjustments[NAME] = water_adjustment / 100

                    if plugin_options['use_netatmo']:
                        water_left = water_needed - zrain - total_info[
                            'rain_mm']
                        water_left = round(max(0, min(100, water_left)), 1)

                    else:
                        water_left = water_needed - total_info['rain_mm']
                        water_left = round(max(0, min(100, water_left)), 1)

                    water_adjustment = round(
                        (water_left /
                         ((plugin_options['netatmo_level']) * days)) * 100, 1)

                    water_adjustment = float(
                        max(plugin_options['wl_min'],
                            min(plugin_options['wl_max'], water_adjustment)))

                    log.info(
                        NAME,
                        _(u'Water needed') + '(%d ' % days + _(u'days') +
                        '): %.1fmm' % water_needed)
                    log.info(
                        NAME,
                        _(u'Total rainfall') +
                        ': %.1fmm' % total_info['rain_mm'])
                    log.info(NAME, _(u'_______________________________'))
                    log.info(NAME,
                             _(u'Irrigation needed') + ': %.1fmm' % water_left)
                    log.info(
                        NAME,
                        _(u'Weather Adjustment') +
                        ': %.1f%%' % water_adjustment)
                    log.info(NAME, _(u'_______________________________'))
                    log.info(
                        NAME,
                        _(u'History rain by Netatmo') + ': %.1fmm' % zrain)

                    self._sleep(3600)

                else:
                    log.clear(NAME)
                    log.info(NAME, _(u'Plug-in is disabled.'))
                    if NAME in level_adjustments:
                        del level_adjustments[NAME]
                    self._sleep(24 * 3600)

            except Exception:
                log.error(
                    NAME,
                    _(u'Weather-based water level plug-in') + ':\n' +
                    traceback.format_exc())
                self._sleep(3600)
        weather.remove_callback(self.update)
Ejemplo n.º 5
0
    def run(self):
        weather.add_callback(self.update)
        self._sleep(10)  # Wait for weather callback before starting
        while not self._stop.is_set():
            try:
                log.clear(NAME)
                if plugin_options['enabled']:
                    log.debug(NAME, _('Checking weather status') + '...')

                    history = weather.get_wunderground_history(
                        plugin_options['days_history'])
                    forecast = weather.get_wunderground_forecast(
                        plugin_options['days_forecast'])
                    today = weather.get_wunderground_conditions()

                    info = {}

                    for day in range(-20, 20):
                        if day in history:
                            day_info = history[day]
                        elif day in forecast:
                            day_info = forecast[day]
                        else:
                            continue

                        info[day] = day_info

                    if 0 in info and 'rain_mm' in today:
                        day_time = datetime.datetime.now().time()
                        day_left = 1.0 - (day_time.hour * 60 +
                                          day_time.minute) / 24.0 / 60
                        info[0]['rain_mm'] = info[0][
                            'rain_mm'] * day_left + today['rain_mm']

                    if not info:
                        log.info(NAME, str(history))
                        log.info(NAME, str(today))
                        log.info(NAME, str(forecast))
                        raise Exception(_('No information available!'))

                    log.info(
                        NAME,
                        _('Using') + ' %d ' % len(info) +
                        _('days of information.'))

                    total_info = {
                        'temp_c':
                        sum([val['temp_c']
                             for val in info.values()]) / len(info),
                        'rain_mm':
                        sum([val['rain_mm'] for val in info.values()]),
                        'wind_ms':
                        sum([val['wind_ms']
                             for val in info.values()]) / len(info),
                        'humidity':
                        sum([val['humidity']
                             for val in info.values()]) / len(info)
                    }

                    # We assume that the default 100% provides 4mm water per day (normal need)
                    # We calculate what we will need to provide using the mean data of X days around today

                    water_needed = 4 * len(info)  # 4mm per day
                    water_needed *= 1 + (total_info['temp_c'] -
                                         20) / 15  # 5 => 0%, 35 => 200%
                    water_needed *= 1 + (total_info['wind_ms'] / 100
                                         )  # 0 => 100%, 20 => 120%
                    water_needed *= 1 - (total_info['humidity'] -
                                         50) / 200  # 0 => 125%, 100 => 75%
                    water_needed = round(water_needed, 1)

                    water_left = water_needed - total_info['rain_mm']
                    water_left = round(max(0, min(100, water_left)), 1)

                    water_adjustment = round(
                        (water_left / (4 * len(info))) * 100, 1)

                    water_adjustment = float(
                        max(plugin_options['wl_min'],
                            min(plugin_options['wl_max'], water_adjustment)))

                    log.info(
                        NAME,
                        _('Water needed') + ' %d ' % len(info) + _('days') +
                        ': %.1fmm' % water_needed)
                    log.info(
                        NAME,
                        _('Total rainfall') +
                        ': %.1fmm' % total_info['rain_mm'])
                    log.info(NAME, '_______________________________')
                    log.info(NAME,
                             _('Irrigation needed') + ': %.1fmm' % water_left)
                    log.info(
                        NAME,
                        _('Weather Adjustment') +
                        ': %.1f%%' % water_adjustment)

                    level_adjustments[NAME] = water_adjustment / 100

                    if plugin_options['protect_enabled']:
                        log.debug(
                            NAME,
                            _('Temperature') +
                            ': %s' % today['temperature_string'])
                        month = time.localtime().tm_mon  # Current month.
                        cold = (today['temp_c'] < plugin_options['protect_temp']) if options.temp_unit == "C" else \
                            (today['temp_f'] < plugin_options['protect_temp'])
                        if cold and month in plugin_options['protect_months']:
                            station_seconds = {}
                            for station in stations.enabled_stations():
                                if station.index in plugin_options[
                                        'protect_stations']:
                                    station_seconds[
                                        station.index] = plugin_options[
                                            'protect_minutes'] * 60
                                else:
                                    station_seconds[station.index] = 0

                            for station in stations.enabled_stations():
                                if run_once.is_active(datetime.datetime.now(),
                                                      station.index):
                                    break
                            else:
                                log.debug(NAME, _('Protection activated.'))
                                run_once.set(station_seconds)

                    self._sleep(3600)

                else:
                    log.clear(NAME)
                    log.info(NAME, _('Plug-in is disabled.'))
                    if NAME in level_adjustments:
                        del level_adjustments[NAME]
                    self._sleep(24 * 3600)

            except Exception:
                log.error(
                    NAME,
                    _('Weather-based water level plug-in') + ':\n' +
                    traceback.format_exc())
                self._sleep(3600)
        weather.remove_callback(self.update)
Ejemplo n.º 6
0
    def run(self):
        weather.add_callback(self.update)
        self._sleep(10)  # Wait for weather callback before starting
        while not self._stop.is_set():
            try:
                log.clear(NAME)
                if plugin_options['enabled']:
                    log.debug(NAME, _('Checking weather status') + '...')

                    if plugin_options['use_netatmo']:
                        authorization = ClientAuth()
                        devList = WeatherStationData(authorization)
                        now = time.time()
                        begin = now - (
                            plugin_options['days_history']) * 24 * 3600
                        mac2 = plugin_options['netatmomac']
                        rainmac2 = plugin_options['netatmorain']
                        resp = (devList.getMeasure(mac2,
                                                   '1hour',
                                                   'sum_rain',
                                                   rainmac2,
                                                   date_begin=begin,
                                                   date_end=now,
                                                   limit=None,
                                                   optimize=False))
                        result = [(time.ctime(int(k)), v[0])
                                  for k, v in resp['body'].items()]
                        result.sort()
                        xdate, xrain = zip(*result)
                        zrain = 0
                        for yrain in xrain:
                            zrain = zrain + yrain
                    else:
                        zrain = 0

                    history = weather.get_wunderground_history(
                        plugin_options['days_history'])
                    forecast = weather.get_wunderground_forecast(
                        plugin_options['days_forecast'])
                    today = weather.get_wunderground_conditions()

                    info = {}

                    for day in range(-20, 20):
                        if day in history:
                            day_info = history[day]
                        elif day in forecast:
                            day_info = forecast[day]
                        else:
                            continue

                        info[day] = day_info

                    if 0 in info and 'rain_mm' in today:
                        day_time = datetime.datetime.now().time()
                        day_left = 1.0 - (day_time.hour * 60 +
                                          day_time.minute) / 24.0 / 60
                        info[0]['rain_mm'] = info[0][
                            'rain_mm'] * day_left + today['rain_mm']

                    if not info:
                        log.info(NAME, str(history))
                        log.info(NAME, str(today))
                        log.info(NAME, str(forecast))
                        raise Exception(_('No information available!'))

                    log.info(
                        NAME,
                        _('Using') + ' %d ' % len(info) +
                        _('days of information.'))

                    total_info = {
                        'temp_c':
                        sum([val['temp_c']
                             for val in info.values()]) / len(info),
                        'rain_mm':
                        sum([val['rain_mm'] for val in info.values()]),
                        'wind_ms':
                        sum([val['wind_ms']
                             for val in info.values()]) / len(info),
                        'humidity':
                        sum([val['humidity']
                             for val in info.values()]) / len(info)
                    }

                    # We assume that the default 100% provides 4mm water per day (normal need)
                    # We calculate what we will need to provide using the mean data of X days around today

                    water_needed = 4 * len(info)  # 4mm per day
                    water_needed *= 1 + (total_info['temp_c'] -
                                         20) / 15  # 5 => 0%, 35 => 200%
                    water_needed *= 1 + (total_info['wind_ms'] / 100
                                         )  # 0 => 100%, 20 => 120%
                    water_needed *= 1 - (total_info['humidity'] -
                                         50) / 200  # 0 => 125%, 100 => 75%
                    water_needed = round(water_needed, 1)

                    water_left = water_needed - total_info['rain_mm']
                    water_left = round(max(0, min(100, water_left)), 1)

                    water_adjustment = round(
                        (water_left / (4 * len(info))) * 100, 1)

                    level_adjustments[NAME] = water_adjustment / 100

                    if plugin_options['use_netatmo']:
                        water_left = water_needed - zrain - forecast_info2[
                            'rain_mm']
                        water_left = round(max(0, min(100, water_left)), 1)

                    else:
                        water_left = water_needed - total_info['rain_mm']
                        water_left = round(max(0, min(100, water_left)), 1)

                    water_adjustment = round(
                        (water_left /
                         ((plugin_options['netatmo_level']) * len(info))) *
                        100, 1)

                    water_adjustment = float(
                        max(plugin_options['wl_min'],
                            min(plugin_options['wl_max'], water_adjustment)))

                    log.info(
                        NAME,
                        _('Water needed') + '(%d ' % len(info) + _('days') +
                        '): %.1fmm' % water_needed)
                    log.info(
                        NAME,
                        _('Total rainfall') +
                        ': %.1fmm' % total_info['rain_mm'])
                    log.info(NAME, _('_______________________________'))
                    log.info(NAME,
                             _('Irrigation needed') + ': %.1fmm' % water_left)
                    log.info(
                        NAME,
                        _('Weather Adjustment') +
                        ': %.1f%%' % water_adjustment)
                    log.info(NAME, _('_______________________________'))
                    log.info(NAME,
                             _('History rain by Netatmo') + ': %.1fmm' % zrain)

                    self._sleep(3600)

                else:
                    log.clear(NAME)
                    log.info(NAME, _('Plug-in is disabled.'))
                    if NAME in level_adjustments:
                        del level_adjustments[NAME]
                    self._sleep(24 * 3600)

            except Exception:
                log.error(
                    NAME,
                    _('Weather-based water level plug-in') + ':\n' +
                    traceback.format_exc())
                self._sleep(3600)
        weather.remove_callback(self.update)