Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pywemo
- from pyHS100 import SmartPlug, SmartBulb, Discover
- from pprint import pformat as pf
- from influxdb import InfluxDBClient
- import time
- from datetime import datetime, timezone
- ########################### INFLUXDB CONFIG ###########################
- influxdb_url = '192.168.1.20'
- influxdb_port = 8086
- influxdb_username = ''
- influxdb_password = ''
- db_name = 'homelab'
- INFLUX_CLIENT = InfluxDBClient(influxdb_url,
- influxdb_port,
- influxdb_username,
- influxdb_password,
- db_name)
- def kasa_status():
- CURRENT_TIME = datetime.now(timezone.utc).astimezone().isoformat()
- for dev in Discover.discover().values():
- alias = dev.alias
- state = dev.state
- if state == "ON":
- state = 1
- else:
- state = 0
- time = CURRENT_TIME
- INFLUX_PAYLOAD = [
- {
- "measurement": "Homelab",
- "tags": {
- "alias": alias,
- "state": state,
- },
- "time": time,
- "fields": {
- "alias": alias,
- "state": state,
- }
- }
- ]
- #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
- #INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
- INFLUX_QUERY = "SELECT state FROM Homelab WHERE (alias = '"+alias+"') ORDER BY time DESC limit 1"
- results = INFLUX_CLIENT.query(INFLUX_QUERY)
- PREV_STATE = results.raw['series'][0]['values'][0][1]
- if PREV_STATE == state:
- print(alias+' is: '+str(state)+': No State Change')
- else:
- INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
- print(alias+' is: '+str(state)+': State Has Changed')
- def wemo_status():
- CURRENT_TIME = datetime.now(timezone.utc).astimezone().isoformat()
- for dev in pywemo.discover_devices():
- alias = str(dev).replace('<','')
- alias = alias.replace('>','')
- state = dev.get_state()
- time = CURRENT_TIME
- INFLUX_PAYLOAD = [
- {
- "measurement": "Homelab",
- "tags": {
- "alias": alias,
- "state": state,
- },
- "time": time,
- "fields": {
- "alias": alias,
- "state": state,
- }
- }
- ]
- #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
- #INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
- #print(alias+' is: '+str(state)+': Test')
- INFLUX_QUERY = "SELECT state FROM Homelab WHERE (alias = '"+alias+"') ORDER BY time DESC limit 1"
- results = INFLUX_CLIENT.query(INFLUX_QUERY)
- PREV_STATE = results.raw['series'][0]['values'][0][1]
- if PREV_STATE == state:
- print(alias+' is: '+str(state)+': No State Change')
- else:
- INFLUX_CLIENT.write_points(INFLUX_PAYLOAD)
- print(alias+' is: '+str(state)+': State Has Changed')
- while True:
- kasa_status()
- wemo_status()
- time.sleep(3)
Add Comment
Please, Sign In to add comment