summaryrefslogtreecommitdiffstats
path: root/src/collectors/python.d.plugin/am2320/am2320.chart.py
blob: 8e66544bd503428082355abbf8d5942865bc8d31 (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
66
67
68
# _*_ coding: utf-8 _*_
# Description: AM2320 netdata module
# Author: tommybuck
# SPDX-License-Identifier: GPL-3.0-or-Later

try:
    import board
    import busio
    import adafruit_am2320

    HAS_AM2320 = True
except ImportError:
    HAS_AM2320 = False

from bases.FrameworkServices.SimpleService import SimpleService

ORDER = [
    'temperature',
    'humidity',
]

CHARTS = {
    'temperature': {
        'options': [None, 'Temperature', 'celsius', 'temperature', 'am2320.temperature', 'line'],
        'lines': [
            ['temperature']
        ]
    },
    'humidity': {
        'options': [None, 'Relative Humidity', 'percentage', 'humidity', 'am2320.humidity', 'line'],
        'lines': [
            ['humidity']
        ]
    }
}


class Service(SimpleService):
    def __init__(self, configuration=None, name=None):
        SimpleService.__init__(self, configuration=configuration, name=name)
        self.order = ORDER
        self.definitions = CHARTS
        self.am = None

    def check(self):
        if not HAS_AM2320:
            self.error("Could not find the adafruit-circuitpython-am2320 package.")
            return False

        try:
            i2c = busio.I2C(board.SCL, board.SDA)
            self.am = adafruit_am2320.AM2320(i2c)
        except ValueError as error:
            self.error("error on creating I2C shared bus : {0}".format(error))
            return False

        return True

    def get_data(self):
        try:
            return {
                'temperature': self.am.temperature,
                'humidity': self.am.relative_humidity,
            }

        except (OSError, RuntimeError) as error:
            self.error(error)
            return None