This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
/
fire-watch.py
60 lines (49 loc) · 1.79 KB
/
fire-watch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
import requests
import os
from ouimeaux.environment import Environment
# fire-watch.py
# Get a response from slack API that includes channels info
# Using ouimeaux package
# https://ouimeaux.readthedocs.io/en/latest/api.html
# wemo setup via ouimeax package
env = Environment()
env.start()
env.discover()
# Names of current CT switches for reference
# dev_switch = env.get_switch('wemo-dev')
# cs_switch = env.get_switch('wemo-cs')
# get Slack API token
slack_api_token = os.environ['SLACK_API_TOKEN']
firestatus = False
print "Starting Slack API check for fire channel! Switches:"
lan_switches = env.list_switches()
for sw in lan_switches:
print sw
channels_api_url = 'https://slack.com/api/channels.list?token=%s&pretty=1' % slack_api_token
r = requests.get(channels_api_url)
for ch in r.json()['channels']:
print ch
print 40 * "########"
if r.status_code != 200:
print "Couldn't get a valid response from Slack api, maybe check API token"
exit(1)
# get string channel name from encoded response and compare
try:
for channel in r.json()['channels']:
if 'fire-' in str(channel['name']) and str(channel['is_archived']) != 'True':
print "Fire channel found: %s" % channel['name']
print "Channel archived status: %s " % channel['is_archived']
firestatus = True
# Turn on all switches in this lan
for switch in (env.list_switches()):
print("Turning On : " + switch)
env.get_switch(switch).on()
except KeyError:
print "Couldnt get slack channels; is API token env variable correct?"
if not firestatus:
# Turn off all switches
print 'No fires found in slack'
for switch in (env.list_switches()):
print("Turning Off: " + switch)
env.get_switch(switch).off()