Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dashcast example #846

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion examples/dashcast_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse
import sys
import time
import threading

import pychromecast
from pychromecast.controllers import dashcast
Expand Down Expand Up @@ -70,15 +71,40 @@

time.sleep(1)

requests_handled = threading.Event()


def _first_request_handled(msg_sent: bool, _response: dict | None) -> None:
"""Request to load first URL handled, load the second URL."""
if not msg_sent:
print("Failed to load first URL")

print("Loaded 1st URL, loading 2nd URL")
d.load_url("https://home-assistant.io/", callback_function=_second_request_handled)


def _second_request_handled(msg_sent: bool, _response: dict | None) -> None:
"""Request to load second URL handled."""
if not msg_sent:
print("Failed to load second URL")
print("Loaded 2nd URL")
requests_handled.set()


# Test that the callback chain works. This should send a message to
# load the first url, but immediately after send a message load the
# second url.
warning_message = "If you see this on your TV then something is broken"

print("Loading 1st URL")
d.load_url(
"https://home-assistant.io/? " + warning_message,
callback_function=lambda msg_sent, resp: d.load_url("https://home-assistant.io/"),
callback_function=_second_request_handled,
)

print("Waiting for callbacks")
requests_handled.wait()

# If debugging, sleep after running so we can see any error messages.
if args.show_debug:
time.sleep(10)
Expand Down