| 1 | # _*_ coding: utf-8 _*_ |
| 2 | # Description: AM2320 netdata module |
| 3 | # Author: tommybuck |
| 4 | # SPDX-License-Identifier: GPL-3.0-or-Later |
| 5 | |
| 6 | try: |
| 7 | import board |
| 8 | import busio |
| 9 | import adafruit_am2320 |
| 10 | |
| 11 | HAS_AM2320 = True |
| 12 | except ImportError: |
| 13 | HAS_AM2320 = False |
| 14 | |
| 15 | from bases.FrameworkServices.SimpleService import SimpleService |
| 16 | |
| 17 | ORDER = [ |
| 18 | 'temperature', |
| 19 | 'humidity', |
| 20 | ] |
| 21 | |
| 22 | CHARTS = { |
| 23 | 'temperature': { |
| 24 | 'options': [None, 'Temperature', 'celsius', 'temperature', 'am2320.temperature', 'line'], |
| 25 | 'lines': [ |
| 26 | ['temperature'] |
| 27 | ] |
| 28 | }, |
| 29 | 'humidity': { |
| 30 | 'options': [None, 'Relative Humidity', 'percentage', 'humidity', 'am2320.humidity', 'line'], |
| 31 | 'lines': [ |
| 32 | ['humidity'] |
| 33 | ] |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | class Service(SimpleService): |
| 39 | def __init__(self, configuration=None, name=None): |
| 40 | SimpleService.__init__(self, configuration=configuration, name=name) |
| 41 | self.order = ORDER |
| 42 | self.definitions = CHARTS |
| 43 | self.am = None |
| 44 | |
| 45 | def check(self): |
| 46 | if not HAS_AM2320: |
| 47 | self.error("Could not find the adafruit-circuitpython-am2320 package.") |
| 48 | return False |
| 49 | |
| 50 | try: |
| 51 | i2c = busio.I2C(board.SCL, board.SDA) |
| 52 | self.am = adafruit_am2320.AM2320(i2c) |
| 53 | except ValueError as error: |
| 54 | self.error("error on creating I2C shared bus : {0}".format(error)) |
| 55 | return False |
| 56 | |
| 57 | return True |
| 58 | |
| 59 | def get_data(self): |
| 60 | try: |
| 61 | return { |
| 62 | 'temperature': self.am.temperature, |
| 63 | 'humidity': self.am.relative_humidity, |
| 64 | } |
| 65 | |
| 66 | except (OSError, RuntimeError) as error: |
| 67 | self.error(error) |
| 68 | return None |