Guest User

Untitled

a guest
Nov 2nd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import pywemo
  2. from pyHS100 import SmartPlug, SmartBulb, Discover
  3. from pprint import pformat as pf
  4. from influxdb import InfluxDBClient
  5. import time
  6. from datetime import datetime, timezone
  7.  
  8. ########################### INFLUXDB CONFIG ###########################
  9. influxdb_url = '192.168.1.20'
  10. influxdb_port = 8086
  11. influxdb_username = ''
  12. influxdb_password = ''
  13. db_name = 'homelab'
  14.  
  15. INFLUX_CLIENT = InfluxDBClient(influxdb_url,
  16. influxdb_port,
  17. influxdb_username,
  18. influxdb_password,
  19. db_name)
  20.  
  21. def kasa_status():
  22. CURRENT_TIME = datetime.now(timezone.utc).astimezone().isoformat()
  23. for dev in Discover.discover().values():
  24. alias = dev.alias
  25. state = dev.state
  26. if state == "ON":
  27. state = 1
  28. else:
  29. state = 0
  30. time = CURRENT_TIME
  31. INFLUX_PAYLOAD = [
  32. {
  33. "measurement": "Homelab",
  34. "tags": {
  35. "alias": alias,
  36. "state": state,
  37. },
  38. "time": time,
  39. "fields": {
  40. "alias": alias,
  41. "state": state,
  42.  
  43. }
  44. }
  45. ]
  46. #IF THIS IS YOUR FIRST RUN UNCOMMENT LINE 47 AND COMMENT LINES 48 TO 55 LET IT RUN FOR 10-15 SECONDS AND THEN COMMENT 47 AND UNCOMMENT 48-55
  47. #INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
  48. INFLUX_QUERY = "SELECT state FROM Homelab WHERE (alias = '"+alias+"') ORDER BY time DESC limit 1"
  49. results = INFLUX_CLIENT.query(INFLUX_QUERY)
  50. PREV_STATE = results.raw['series'][0]['values'][0][1]
  51. if PREV_STATE == state:
  52. print(alias+' is: '+str(state)+': No State Change')
  53. else:
  54. INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
  55. print(alias+' is: '+str(state)+': State Has Changed')
  56.  
  57.  
  58. def wemo_status():
  59. CURRENT_TIME = datetime.now(timezone.utc).astimezone().isoformat()
  60. for dev in pywemo.discover_devices():
  61. alias = str(dev).replace('<','')
  62. alias = alias.replace('>','')
  63. state = dev.get_state()
  64. time = CURRENT_TIME
  65. INFLUX_PAYLOAD = [
  66. {
  67. "measurement": "Homelab",
  68. "tags": {
  69. "alias": alias,
  70. "state": state,
  71. },
  72. "time": time,
  73. "fields": {
  74. "alias": alias,
  75. "state": state,
  76.  
  77. }
  78. }
  79. ]
  80. #IF THIS IS YOUR FIRST RUN UNCOMMENT LINE 44 and 45 AND COMMENT LINES 46 TO 53 LET IT RUN FOR 10-15 SECONDS AND THEN COMMENT 44 and 45 AND UNCOMMENT 46-53
  81. #INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
  82. #print(alias+' is: '+str(state)+': Test')
  83. INFLUX_QUERY = "SELECT state FROM Homelab WHERE (alias = '"+alias+"') ORDER BY time DESC limit 1"
  84. results = INFLUX_CLIENT.query(INFLUX_QUERY)
  85. PREV_STATE = results.raw['series'][0]['values'][0][1]
  86. if PREV_STATE == state:
  87. print(alias+' is: '+str(state)+': No State Change')
  88. else:
  89. INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
  90. print(alias+' is: '+str(state)+': State Has Changed')
  91.  
  92. while True:
  93. kasa_status()
  94. wemo_status()
  95. time.sleep(3)
Add Comment
Please, Sign In to add comment