i run the chromecast read script on a raspberry and run domoticz on a NAS and this kind of scripts I dont want to run on the same system.
So I updated this old script to Phyton3.7
I'm not a Phyton guy, but if someone like to changes the light when netflix starts, maby it's helpfull
Changes in the URL the Idx for a youtube switch or a netflix switch and changes the friendlyname Tv in your google cast name.
And if some-one is better in phyton then me? Please make ajustments to help
Code: Select all
import pychromecast
import pychromecast.controllers
import json
import time
import re
import subprocess
import urllib
print("Script started")
#
# Variables to set.
#
# IP address of the chromecast to control
chromecast_ip = "192.168.178.13"
#Address of domoticz
domoticz = "http://192.168.178.25:8084"
# Prefix for all URLs to be handled. E.g. if the application runs on http://localhost/chromecast,
# the prefix should be "/chromecast"
url_prefix = "/chromecast"
# Timeout to wait for the status (ms)
status_timeout = 5000
#Domoticz DeviceID
IDX="3078"
#Variable for old state
OldStatus="idle"
"""
Handles status updates
"""
class StatusHandler(object):
def __init__(self, cast):
"""
Initialize the handler
cast: the chromecast object to work with
"""
self.cast = cast
self.cast_status = False
self.media_status = False
def new_cast_status(self, status):
self.cast_status = True
def new_media_status(self,status):
self.media_status = True
"""
Returns a chromecast object filled with the status
"""
def get_cast_with_status(chromecast_ip):
chromecasts = pychromecast.get_chromecasts()
[cc.device.friendly_name for cc in chromecasts]
['Tv', 'Living Room', 'Den', 'Bedroom']
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Tv")
# Start worker thread and wait for cast device to be ready
cast.start()
# We can only use the controller when a status has been retrieved
# For that reason, we wait for that event
status_handler = StatusHandler(cast)
cast.socket_client.receiver_controller.register_status_listener(status_handler)
cast.media_controller.register_status_listener(status_handler)
# Wait for the status, although not forever
starttime = time.time() * 1000
wait_until = starttime + status_timeout
while ( time.time() * 1000 ) < wait_until and not (status_handler.cast_status and status_handler.media_status):
pass
return cast
def get_current_status():
global OldStatus
cast = get_cast_with_status(chromecast_ip) #have to do this each time otherwise netflix will not work
# print(dir(cast))
# print(cast.app_display_name)
if (cast.status.status_text == "Netflix"):
CurrentStatus=cast.media_controller.status.player_state
print ("Currenstatus",CurrentStatus)
if (OldStatus != CurrentStatus):
if CurrentStatus == "PLAYING" or CurrentStatus == "BUFFERING":
print("Turning on")
data = urllib.request.urlopen(domoticz+"/json.htm?type=command¶m=switchlight&idx="+IDX+"&switchcmd=On")
else :
print("Turning off")
data = urllib.request.urlopen(domoticz+"/json.htm?type=command¶m=switchlight&idx="+IDX+"&switchcmd=Off")
print("Old status is " + OldStatus)
OldStatus = CurrentStatus
elif (cast.status.status_text == "YouTube"):
CurrentStatus=cast.media_controller.status.player_state
print ("Currenstatus",CurrentStatus)
if (OldStatus != CurrentStatus):
if CurrentStatus == "PLAYING" or CurrentStatus == "BUFFERING":
print("Turning on")
data = urllib.request.urlopen(domoticz+"/json.htm?type=command¶m=switchlight&idx="+IDX+"&switchcmd=On")
else :
print("Turning off")
data = urllib.request.urlopen(domoticz+"/json.htm?type=command¶m=switchlight&idx="+IDX+"&switchcmd=Off")
print("Old status is " + OldStatus)
OldStatus = CurrentStatus
while (1):
get_current_status()
time.sleep(1) # Delay for 1 second
print("Script stopped")