-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromecast.py
74 lines (64 loc) · 1.97 KB
/
chromecast.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pychromecast
import os
import time
import kasa
IP_ADDRESS_PLUG = 'INSERT_IP_ADDRESS'
CHROMECAST_NAME = ['INSERT_CHROMECAST_NAMES'] # If the relevant chromecast is part of speaker groups, you should list both the name of the chromecast and the name(s) of the group(s).
def get_chromecasts(friendly_names):
"""
Searches the network for chromecast devices matching a list of friendly
names or a list of UUIDs.
"""
chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=friendly_names)
return chromecasts
def is_chromecast_connected(cast):
"""
Checks if the inputted chromecast is connected to or not
"""
cast.wait()
status = cast.status
return status.app_id != None
def is_any_cast_connected(casts):
"""
Checks if any of the inputted chromecasts are connected to or not.
"""
if casts == []:
raise Exception("No chromecasts are found")
y = [is_chromecast_connected(x) for x in casts]
return any(y)
def turn_plug_on(ip_address_plug):
"""
Turns the smart plug on
"""
status = os.system(f'kasa --type plug --host "{ip_address_plug}" on')
if status != 0:
raise Exception("Can't turn plug on")
def turn_plug_off(ip_address_plug):
"""
Turns the smart plug off
"""
status = os.system(f'kasa --type plug --host "{ip_address_plug}" off')
if status != 0:
raise Exception("Can't turn plug off")
def update_plug_state(state, ip_address_plug):
"""
Updates the smart plug state
"""
if state == True:
turn_plug_on(ip_address_plug)
else:
turn_plug_off(ip_address_plug)
if __name__ == "__main__":
casts = get_chromecasts(friendly_names=CHROMECAST_NAME)
cast_state = None
while(True):
try:
time.sleep(1)
new_cast_state = is_any_cast_connected(casts)
if cast_state != new_cast_state:
update_plug_state(new_cast_state, IP_ADDRESS_PLUG)
cast_state = new_cast_state
except Exception as e:
print(f'Step failed: {e}')
time.sleep(10)
continue