-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromecast.py
56 lines (47 loc) · 1.67 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
from __future__ import print_function
import time
import sys
import logging
import json
import codecs
import urllib
import pychromecast
#Domoticz baseURL
domoticz = "http://localhost"
#Domoticz switch IDX
idx=963
# Your Chromecast device Friendly Name
device_friendly_name = "Cast wk"
chromecasts = pychromecast.get_chromecasts()
# select Chromecast device
cast = next(cc for cc in chromecasts if cc.device.friendly_name == device_friendly_name)
# wait for the device
cast.wait()
print(cast.device)
print(cast.status)
class mediaListener:
domurl=domoticz
device=idx
def __init__(self, domoticzurl, deviceno):
self.domurl = domoticzurl
self.device=str(deviceno)
self.oldPlayerStatus = 'NONE'
def new_media_status(self, status):
print("mediaListener")
print(status.player_state + " vs " + self.oldPlayerStatus)
if (self.oldPlayerStatus != status.player_state):
self.oldPlayerStatus = status.player_state
print("Updating status to Domoticz: " + self.domurl + "/json.htm?type=command¶m=switchlight&idx=" + self.device + "&switchcmd=xx")
if status.player_state == "PLAYING" or status.player_state == "BUFFERING":
data = urllib.urlopen(self.domurl + "/json.htm?type=command¶m=switchlight&idx=" + self.device + "&switchcmd=On")
print(data.geturl())
print(data.read())
else :
data = urllib.urlopen(self.domurl + "/json.htm?type=command¶m=switchlight&idx=" + self.device + "&switchcmd=Off")
print(data.geturl())
print(data.read())
#self.storeVariable('ChromeState', new_state)
listener = mediaListener(domoticz, idx)
cast.media_controller.register_status_listener(listener)
while (1):
pass