aboutsummaryrefslogtreecommitdiffstats
path: root/Common/sensors.py
blob: 36594d2cdccd7c989cc3005b248aa65424521b09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Get Environmental Sensor Data.
"""

import External.lnetatmo as lnetatmo


def get_netatmo_readings(station='Ng58', modules=['Living Room', 'Bedroom']):
    """
    Get Netatmo Readings.

    Epochs are Unix Timestamps for each Modue in Seconds.
    Readings are Netatmo readings with the following units.
    - Noise             (dB)
    - Temperature       (C)
    - Humidity          (%)
    - Pressure          (Pa)
    - CO2               (PPM)

    For example, readings for the defaults args would look likes this:
    > readings = {'Living Room': {u'Pressure': 1011.7, u'Noise': 37, \
                                  u'Temperature': 20.7, u'CO2': 594, \
                                  u'Humidity': 40}, \
                  'Bedroom': {u'Temperature': 18.6, u'Humidity': 38}}

    Epochs would look like:
    > epochs = {'Living Room': 1461231951, 'Bedroom': 1461231922}

    @param: station     - Netatmo station to poll           [String]
    @param: modules     - Netatmo modules to poll           [List of Strings]
    @return: readings   - Netatmo Readings                  [Dict of Dicts]
    @return: epochs     - Timestamps of Readings  (Seconds) [Dict]
    """

    # Load Credentials
    with open('userpass_netatmo', 'r') as f:
        line = f.readline()
        line = line.strip().split(',')
        clientId = line[0]
        clientSecret = line[1]
        username = line[2]
        password = line[3]

    # Auth to Netatmo API
    authorization = lnetatmo.ClientAuth(clientId = clientId, \
                                        clientSecret = clientSecret, \
                                        username = username, \
                                        password = password)
    devList = lnetatmo.WeatherStationData(authorization, station=station)

    # Get Data
    epochs = {}
    readings = {}
    for module in modules:
        readings_raw = devList.lastData()[module]
        readings_sane = {}
        for key, value in zip(readings_raw.keys(), readings_raw.values()):
            if key in [ 'Noise', 'Temperature', 'Humidity', \
                        'Pressure', 'CO2']:
                readings_sane[key] = value
        epochs[module] = readings_raw['When']
        readings[module] = readings_sane

    # Return
    return readings, epochs