-
Notifications
You must be signed in to change notification settings - Fork 0
/
discover_devices
52 lines (49 loc) · 1.49 KB
/
discover_devices
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
#!/usr/bin/python3
from prettytable import PrettyTable
import pychromecast
# Fetch known devices
pf = open("known_devices.txt", "r")
known_devices = pf.read().strip()
pf.close()
known_devices_array = []
if known_devices:
known_devices_array = known_devices.split(',')
print('Previosly known chromecasts:')
ip_table = PrettyTable()
ip_table.field_names = ["ip"]
for device in known_devices_array:
ip_table.add_row([device])
print(ip_table)
else:
print('No previously known chromecasts')
# Discover devices
print('Searching chromecasts...')
if len(known_devices_array) > 0:
services, browser = pychromecast.discovery.discover_chromecasts(known_hosts=known_devices_array)
else:
services, browser = pychromecast.discovery.discover_chromecasts()
# Stop discovery
pychromecast.discovery.stop_discovery(browser)
# print chromecasts
if services:
ips = []
chromecast_count = len(services)
print('Found ' + str(chromecast_count) + ' chromecasts.')
# print(services)
table = PrettyTable()
table.field_names = ["name", "ip", "port", "uuid", "type"]
for chromecast in services:
ip = chromecast.host
port = chromecast.port
uuid = chromecast.uuid
c_type = chromecast.model_name
name = chromecast.friendly_name
ips.append(ip)
table.add_row([name, ip, port, uuid, c_type])
f = open("known_devices.txt", "w")
f.write(','.join(ips))
f.close()
print(table)
else:
print('No devices found')
quit()