Advertisement
Guest User

Untitled

a guest
Aug 5th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. import time
  2. import random
  3. import datetime
  4. import telepot
  5. import json
  6. from requests import get
  7.  
  8. headers = {'x-ha-access': 'password',
  9.            'content-type': 'application/json'}
  10.  
  11. # Devices to load info from
  12. devices = ['binary_sensor.thermostaat_current_operation','thermostat.thermostaat','sensor.netatmo_buiten_temperature','sensor.netatmo_binnen_co2', 'sensor.netatmo_binnen_noise','sensor.netatmo_binnen_co2','sensor.netatmo_binnen_temperature','sensor.netatmo_binnen_humidity','sensor.netatmo_binnen2_co2','sensor.netatmo_binnen2_temperature','sensor.netatmo_binnen2_humidity','sensor.slaapkamer_eva','sensor.slaapkamer_eva_2']
  13.  
  14. #Get info from device
  15. def get_info (device_id):
  16.   url = 'http://10.0.1.59:8124/api/states/' + str(device_id)
  17.   response = get(url, headers=headers)
  18.   json_response = json.loads(response.text)
  19.   return json_response
  20.  
  21. def handle(msg):
  22.     chat_id = msg['chat']['id']
  23.     command = msg['text']
  24.  
  25.     print 'Got command: %s' % command
  26.  
  27.     if command == '/roll':
  28.         bot.sendMessage(chat_id, random.randint(1,6))
  29.     elif command == '/time':
  30.         bot.sendMessage(chat_id, str(datetime.datetime.now()))
  31.     elif command == '/start':
  32.         bot.sendMessage(chat_id, 'hola!')
  33.     elif command == '/states':
  34.         states = []
  35.         for s in devices:
  36.           device_data = get_info(s)
  37.           state= str(device_data['attributes']['friendly_name'] + ": " + device_data['state'])
  38.           states.append(str(state))
  39.           joined_states = "\n".join(states)
  40.         bot.sendMessage(chat_id, str(joined_states))
  41.  
  42. bot = telepot.Bot('bottokenidthing')
  43. bot.message_loop(handle)
  44. print 'I am listening ...'
  45.  
  46. while 1:
  47.     time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement