A clone of: https://github.com/nutechsoftware/alarmdecoder This is requires as they dropped support for older firmware releases w/o building in backward compatibility code, and they had previously hardcoded pyserial to a python2 only version.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

  1. import time
  2. import smtplib
  3. from email.mime.text import MIMEText
  4. from pyad2 import AD2
  5. from pyad2.devices import USBDevice
  6. # Configuration values
  7. SUBJECT = "Alarm Decoder - ALARM"
  8. FROM_ADDRESS = "root@localhost"
  9. TO_ADDRESS = "root@localhost" # NOTE: Sending an SMS is as easy as looking
  10. # up the email address format for your provider.
  11. SMTP_SERVER = "localhost"
  12. SMTP_USERNAME = None
  13. SMTP_PASSWORD = None
  14. def main():
  15. """
  16. Example application that sends an email when an alarm event is
  17. detected.
  18. """
  19. try:
  20. # Retrieve the first USB device
  21. device = AD2(USBDevice.find())
  22. # Set up an event handler and open the device
  23. device.on_alarm += handle_alarm
  24. with device.open():
  25. while True:
  26. time.sleep(1)
  27. except Exception, ex:
  28. print 'Exception:', ex
  29. def handle_alarm(sender, *args, **kwargs):
  30. """
  31. Handles alarm events from the AD2.
  32. """
  33. status = kwargs['status']
  34. text = "Alarm status: {0}".format(status)
  35. # Build the email message
  36. msg = MIMEText(text)
  37. msg['Subject'] = SUBJECT
  38. msg['From'] = FROM_ADDRESS
  39. msg['To'] = TO_ADDRESS
  40. s = smtplib.SMTP(SMTP_SERVER)
  41. # Authenticate if needed
  42. if SMTP_USERNAME is not None:
  43. s.login(SMTP_USERNAME, SMTP_PASSWORD)
  44. # Send the email
  45. s.sendmail(FROM_ADDRESS, TO_ADDRESS, msg.as_string())
  46. s.quit()
  47. print 'sent alarm email:', text
  48. if __name__ == '__main__':
  49. main()