backend.py 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# -*- coding: utf-8 -*-

# Copyright(C) 2010-2013 Romain Bignon, Florent Fourcot
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from .browser import DresdenWetterBrowser
22
from weboob.capabilities.gauge import CapGauge, GaugeSensor, Gauge,\
23
        SensorNotFound
24
from weboob.capabilities.base import find_object
25 26 27 28 29 30
from weboob.tools.backend import BaseBackend


__all__ = ['DresdenWetterBackend']


31
class DresdenWetterBackend(BaseBackend, CapGauge):
32 33 34
    NAME = 'dresdenwetter'
    MAINTAINER = u'Florent Fourcot'
    EMAIL = 'weboob@flo.fourcot.fr'
Florent Fourcot's avatar
Florent Fourcot committed
35
    VERSION = '1.0'
36 37 38 39 40
    LICENSE = 'AGPLv3+'
    DESCRIPTION = u"Private wetter station Dresden"
    BROWSER = DresdenWetterBrowser

    def iter_gauges(self, pattern=None):
Florent Fourcot's avatar
Florent Fourcot committed
41 42
        if pattern is None or pattern.lower() in u"dresden"\
                or pattern.lower() in "weather":
Florent Fourcot's avatar
Florent Fourcot committed
43
            gauge = Gauge("wetter")
44 45 46
            gauge.name = u"Private Wetterstation Dresden"
            gauge.city = u"Dresden"
            gauge.object = u"Weather"
47
            gauge.sensors = list(self.browser.get_sensors_list())
48 49 50 51 52 53 54
            yield gauge

    def _get_sensor_by_id(self, id):
        for gauge in self.iter_gauges():
            for sensor in gauge.sensors:
                if id == sensor.id:
                    return sensor
Florent Fourcot's avatar
Florent Fourcot committed
55
        raise SensorNotFound()
56 57 58

    def iter_sensors(self, gauge, pattern=None):
        if not isinstance(gauge, Gauge):
59
            gauge = find_object(self.iter_gauges(), id=gauge, error=SensorNotFound)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        if pattern is None:
            for sensor in gauge.sensors:
                yield sensor
        else:
            lowpattern = pattern.lower()
            for sensor in gauge.sensors:
                if lowpattern in sensor.name.lower():
                    yield sensor

    # Not in the website
    def iter_gauge_history(self, sensor):
        raise NotImplementedError()

    def get_last_measure(self, sensor):
        if not isinstance(sensor, GaugeSensor):
            sensor = self._get_sensor_by_id(sensor)
        return sensor.lastvalue