Skip to content

Commit

Permalink
Improve dashcast example (#846)
Browse files Browse the repository at this point in the history
* Improve dashcast example

* Format code

* Satisfy pylint
  • Loading branch information
emontnemery committed Feb 14, 2024
1 parent 31cebb7 commit afc4d9a
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion examples/dashcast_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import argparse
import sys
import time
import threading

import pychromecast
from pychromecast.controllers import dashcast
Expand Down Expand Up @@ -71,15 +72,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

0 comments on commit afc4d9a

Please sign in to comment.