am2320.chart.py 1.7 KB

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