Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run(self):
if not self.args["rhost"] and not self.args["name"]:
print_info("Show options, it's necessary to configure onename or rhost")
return
if str(self.args["timeout"]) == "None":
self.args["timeout"] = 6
try:
chromecasts = pychromecast.get_chromecasts(timeout=self.args["timeout"])
cast = next(cc for cc in chromecasts if (cc.device.friendly_name == self.args["name"] or cc.host == self.args["rhost"]))
cast.wait()
print_info("Device found, sending video")
except:
print_error("Device no found")
return
yt = YouTubeController()
cast.register_handler(yt)
yt.play_video(self.args["video"])
print_ok("Done!")
"""
"""
def main_loop():
def callback(chromecast):
print('found', chromecast)
DashboardLauncher(chromecast, dashboard_url='http://192.168.1.132:8080')
pychromecast.get_chromecasts(blocking=False, callback=callback)
while True:
time.sleep(1)
main_loop()
"""
casts = pychromecast.get_chromecasts()
if len(casts) == 0:
print('No Devices Found')
exit()
cast = next(cc for cc in casts if DISPLAY_NAME in (None, '') or cc.device.friendly_name == DISPLAY_NAME)
if not cast:
print('Chromecast with name', DISPLAY_NAME, 'not found')
exit()
DashboardLauncher(cast, dashboard_url=DASHBOARD_URL)
# Keep running
while True:
time.sleep(1)
#!/usr/bin/python3
from __future__ import print_function
import sys; sys.path.insert(0,'/usr/local/lib/python3.6/dist-packages/')
import time
import pychromecast #https://github.com/balloob/pychromecast/tree/master/pychromecast sudo pip3 install pychromecast --upgrade
from gtts import gTTS #https://github.com/pndurette/gTTS sudo pip3 install gTTS --upgrade
#import sys
URL_DOMOTICZ = 'http://192.168.100.248:8080/' # renseigner l'adresse et le port de votre domoticz
for arg in sys.argv:
print(arg)
tts = gTTS(text=arg, lang='fr', slow=False)
tts.save("/home/pi/domoticz/www/notification.mp3")
chromecasts = pychromecast.get_chromecasts()
[cc.device.friendly_name for cc in chromecasts]
['Salon','Bureau'] # mettre le nom de votre chromecast séparé par une virgule ex: ['douche', 'salon', 'cuisine', 'chambre']
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Salon" )
cast.wait()
mc = cast.media_controller
mc.play_media(URL_DOMOTICZ+'notification.mp3', 'audio/mp3')
mc.block_until_active()
mc.pause()
time.sleep(1)
def get_chromecasts():
devices = pychromecast.get_chromecasts()
devices.sort(key=lambda cc: cc.name)
return devices
def your_main_loop():
t = 1
cast = None
def callback(chromecast):
chromecast.connect()
nonlocal cast
cast = chromecast
stop_discovery()
stop_discovery = pychromecast.get_chromecasts(blocking=False, callback=callback)
while True:
if cast:
polltime = 0.1
can_read, _, _ = select.select([cast.socket_client.get_socket()], [], [], polltime)
if can_read:
#received something on the socket, handle it with run_once()
cast.socket_client.run_once()
do_actions(cast, t)
t += 1
if(t > 50):
break
else:
print("=> Waiting for cast discovery...")
time.sleep(1)
def your_main_loop():
t = 1
cast = None
def callback(chromecast):
chromecast.connect()
nonlocal cast
cast = chromecast
stop_discovery()
stop_discovery = pychromecast.get_chromecasts(blocking=False, callback=callback)
while True:
if cast:
polltime = 0.1
can_read, _, _ = select.select([cast.socket_client.get_socket()], [], [], polltime)
if can_read:
#received something on the socket, handle it with run_once()
cast.socket_client.run_once()
do_actions(cast, t)
t += 1
if(t > 50):
break
else:
print("=> Waiting for cast discovery...")
time.sleep(1)
def new_cast_status(self, status):
print('[',time.ctime(),' - ', self.name,'] status chromecast change:')
print(status)
class StatusMediaListener:
def __init__(self, name, cast):
self.name = name
self.cast= cast
def new_media_status(self, status):
print('[',time.ctime(),' - ', self.name,'] status media change:')
print(status)
chromecasts = pychromecast.get_chromecasts()
chromecast = next(cc for cc in chromecasts
if cc.device.friendly_name == "Living Room Speaker")
chromecast.start()
listenerCast = StatusListener(chromecast.name, chromecast)
chromecast.register_status_listener(listenerCast)
listenerMedia = StatusMediaListener(chromecast.name, chromecast)
chromecast.media_controller.register_status_listener(listenerMedia)
input('Listening for Chromecast events...\n\n')
def _connect_chromecast(self, available_devices=None):
''' Attempt to (re)connect to cast device named in `__init__`. '''
self.cast = None
if not available_devices:
available_devices = pychromecast.get_chromecasts(tries=1)
matching_devices = [
c for c in available_devices
if c.device.friendly_name == self.cast_name
]
if not matching_devices:
click.echo('Could not connect to device "%s"' % self.cast_name)
return
if len(matching_devices) > 1:
click.echo('WARNING: Multiple devices available. Choosing first.')
self.cast = matching_devices[0]
# Wait for the device to be available
return pychromecast._get_chromecast_from_host(
(
cast_info.host,
cast_info.port,
cast_info.uuid,
cast_info.model_name,
cast_info.friendly_name,
)
)
_LOGGER.error(
"Could not find device %s from hass.data, falling back to pychromecast scan",
device_name,
)
# Discover devices manually
chromecasts = pychromecast.get_chromecasts()
for _cast in chromecasts:
if _cast.name == device_name:
_LOGGER.debug("Fallback, found cast device: %s", _cast)
return _cast
raise HomeAssistantError("Could not find device with name {}".format(device_name))