+ 2

pychromecast - how to get started?

i want to be able to interact with google chromecast devices in my home. python has a library called pychromecast. it seems kind of straight forward for a beginner, but i can't get it to show me what chromecasts are on my network. if I use: import pychromecast import zeroconf import time import logging # Enable debug logging logging.basicConfig(level=logging.DEBUG) def discover_chromecasts(): castDevice = pychromecast.Chromecast('192.168.86.222') print(dir(castDevice)) if __name__ == "__main__": discover_chromecasts() i get the output: Error connecting to Chromecast: 'str' object has no attribute 'cast_type' i have played around got the debugging code to display feedback that it can actually find devices on my network, but I don't know, and can't find any information on, how to access the info it finds. A I understand the code (python is a language I don't really know, so I'm just reading it as pseudocode) the castDevice object/variable *should* contain something as long as I point it to a valid IP address of a chromecast device, but it always tells me it's invalid. How can I inspect what the castDevice object is? edit: so i tried using the interactive python shell instead and input the lines one by one like this: >>> import pychromecast >>> import time >>> import zeroconf >>> zconf = zeroconf.Zeroconf() >>> browser = pychromecast.CastBrowser(pychromecast.SimpleCastListener(lambda uuid, service: print(browser.devices[uuid].friendly_name)), zconf) >>> browser.start_discovery() >>> Patio Speaker SHIELD Patio TV browser.stop_discovery() >>> and indeed! it finds the devices on my network and shows the names of the devices! why does it work in the python shell, but not in a script?

9th Nov 2024, 8:02 AM
Nathan Stanley
Nathan Stanley - avatar
3 Answers
+ 4
Nathan Stanley there are several reasons why and why not certain codes work in one shell or another or on one device or another đŸ€” I use a bash shell with the a shebang line (e.g., #!/usr/bin/env python3) to specify the Python interpreter when running from the command line.
9th Nov 2024, 9:03 AM
BroFar
BroFar - avatar
+ 2
i worked out that start_discovery is running asynchronously, so the script is terminating before the output is shown. i am wanting to use the script found here: https://github.com/rikyiso01/mutechromecastads/blob/main/mutechromecastads.py as a standard python script, but am not having much luck where it uses the Chromecast class. I've looked at the pychromecast code and the Chromecast class is still in there (it hasn't updated and changed as far as I can tell) but I can't work out why the script fails at that line.
9th Nov 2024, 10:08 AM
Nathan Stanley
Nathan Stanley - avatar
0
Without studying your code ... one tip is ... get the sample code working with as few modifications as possible. Just get it working. Do that before you start changing it. Then when you change it, you will have a better idea where things went wrong. Study the sample code. Every line. Figure out why they are doing what they are doing. If the issue is with async programming, you'll have to sort out what you didn't do that they did in the sample code. That's why it is helpful to get the sample code working first. Plug in your IP address of your ChromeCast device and see if you can get it to run successfully.
9th Nov 2024, 5:03 PM
Jerry Hobby
Jerry Hobby - avatar