Example #1
0
    def run(self):
        while not self._stop_event.is_set():
            try:
                if plugin_options[
                        'enabled']:  # if Weather-based Rain Delay plug-in is enabled
                    log.clear(NAME)
                    log.info(NAME, 'Checking rain status...')

                    current_data = weather.get_current_data()

                    if 'precipProbability' in current_data:
                        if current_data['precipProbability'] > 0.75:
                            log.info(
                                NAME,
                                'Rain detected: ' + current_data['summary'] +
                                '. Adding delay of ' +
                                str(plugin_options['delay_duration']))
                            rain_blocks[NAME] = datetime.datetime.now(
                            ) + datetime.timedelta(
                                hours=float(plugin_options['delay_duration']))
                            stop_onrain()

                        elif current_data['precipProbability'] > 0.1:
                            log.info(
                                NAME, 'No rain detected: ' +
                                current_data['summary'] + '. No action.')

                        else:
                            log.info(
                                NAME, 'Good weather detected: ' +
                                current_data['summary'] +
                                '. Removing rain delay.')
                            if NAME in rain_blocks:
                                del rain_blocks[NAME]

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

            except Exception:
                log.error(
                    NAME, 'Weather-based Rain Delay plug-in:\n' +
                    traceback.format_exc())
                self._sleep(3600)
Example #2
0
    def run(self):
        weather_mon = None

        if plugin_options['use_footer']:
            weather_mon = showInFooter(
            )  #  instantiate class to enable data in footer
            weather_mon.label = _(u'Weather')  # label on footer
            weather_mon.button = "weather_based_rain_delay/settings"  # button redirect on footer
            weather_mon.val = '---'  # value on footer

        while not self._stop_event.is_set():
            try:
                if plugin_options[
                        'enabled']:  # if Weather-based Rain Delay plug-in is enabled
                    if plugin_options['use_netatmo']:
                        authorization = ClientAuth()
                        devList = WeatherStationData(authorization)
                        now = time.time()
                        begin = now - (plugin_options['netatmo_hour']) * 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

                    log.clear(NAME)
                    log.info(NAME, _(u'Checking rain status') + '...')
                    current_data = weather.get_current_data()

                    delaytime = int(plugin_options['delay_duration'])
                    delaytimeAtmo = int(
                        plugin_options['netatmo_interval']) * 60

                    if zrain > 0:
                        log.info(
                            NAME,
                            _(u'Netatmo detected Rain') + ': %.1f ' % zrain +
                            _(u'mm') + '. ' + _(u'Adding delay of') + ' ' +
                            str(delaytime) + ' ' + _(u'hours') + '.')
                        tempText = ""
                        tempText += _(
                            u'Netatmo detected Rain') + u' %.1f ' % zrain + _(
                                u'mm') + '. ' + _(
                                    u'Adding delay of') + ' ' + str(
                                        delaytime) + ' ' + _(u'hours')
                        if plugin_options['use_footer']:
                            if weather_mon is not None:
                                weather_mon.val = tempText.encode(
                                    'utf8').decode('utf8')  # value on footer
                        rain_blocks[NAME] = datetime.datetime.now(
                        ) + datetime.timedelta(hours=float(delaytime))
                        stop_onrain()
                        self._sleep(delaytimeAtmo)

                    else:
                        if 'precipProbability' in current_data:
                            if current_data['precipProbability'] > 0.75:
                                log.info(
                                    NAME,
                                    _(u'Weather detected Rain') + ': ' +
                                    current_data['summary'] +
                                    _(u'Adding delay of') + ' ' +
                                    str(plugin_options['delay_duration']) +
                                    '.')
                                rain_blocks[NAME] = datetime.datetime.now(
                                ) + datetime.timedelta(hours=float(
                                    plugin_options['delay_duration']))
                                stop_onrain()
                                tempText = ""
                                tempText += _(u'Weather detected Rain')

                            elif current_data['precipProbability'] > 0.1:
                                log.info(
                                    NAME,
                                    _(u'No rain detected') + ': ' +
                                    current_data['summary'] + '. ' +
                                    _(u'No action.'))
                                tempText = ""
                                tempText += _(u'No rain detected')

                            else:
                                log.info(
                                    NAME,
                                    _(u'Good weather detected') + ': ' +
                                    current_data['summary'] + '. ' +
                                    _(u'Removing rain delay.'))
                                tempText = ""
                                tempText += _(u'Good weather detected')
                                if NAME in rain_blocks:
                                    del rain_blocks[NAME]

                        if plugin_options['use_footer']:
                            if weather_mon is not None:
                                weather_mon.val = tempText.encode(
                                    'utf8').decode('utf8')  # value on footer
                        self._sleep(3600)

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

            except Exception:
                log.error(
                    NAME,
                    _(u'Weather-based Rain Delay plug-in') + ':\n' +
                    traceback.format_exc())
                self._sleep(3600)
Example #3
0
    def run(self):
        while not self._stop.is_set():
            try:
                if plugin_options[
                        'enabled']:  # if Weather-based Rain Delay plug-in is enabled
                    if plugin_options['use_netatmo']:
                        authorization = ClientAuth()
                        devList = WeatherStationData(authorization)
                        now = time.time()
                        begin = now - (plugin_options['netatmo_hour']) * 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

                    log.clear(NAME)
                    log.info(NAME, _('Checking rain status') + '...')
                    current_data = weather.get_current_data()

                    delaytime = int(plugin_options['netatmo_interval']) * 60

                    if zrain > 0:
                        log.info(
                            NAME, 'Rain detected: ' + ': %.1fmm' % zrain +
                            '. Adding delay of ' + str(delay))
                        rain_blocks[NAME] = datetime.datetime.now(
                        ) + datetime.timedelta(hours=float(delay))
                        stop_onrain()

                    else:
                        if 'precipProbability' in current_data:
                            if current_data['precipProbability'] > 0.75:
                                log.info(
                                    NAME,
                                    _('Rain detected') + ': ' +
                                    current_data['summary'] +
                                    '. Adding delay of ' +
                                    str(plugin_options['delay_duration']))
                                rain_blocks[NAME] = datetime.datetime.now(
                                ) + datetime.timedelta(hours=float(
                                    plugin_options['delay_duration']))
                                stop_onrain()

                            elif current_data['precipProbability'] > 0.1:
                                log.info(
                                    NAME,
                                    _('No rain detected') + ': ' +
                                    current_data['summary'] + '. ' +
                                    _('No action.'))

                            else:
                                log.info(
                                    NAME,
                                    _('Good weather detected') + ': ' +
                                    current_data['summary'] + '. ' +
                                    _('Removing rain delay.'))
                                if NAME in rain_blocks:
                                    del rain_blocks[NAME]

                        self._sleep(3600)

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

            except Exception:
                log.error(
                    NAME,
                    _('Weather-based Rain Delay plug-in') + ':\n' +
                    traceback.format_exc())
                self._sleep(3600)
Example #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') + '...')

                    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)