custom-components / sensor.plex_recently_added

▶️ Plex component to feed Upcoming Media Card.
Apache License 2.0
90 stars 60 forks source link

[Feature Request] Clicking a button on one of the lists plays selected media on plex #72

Open evanrich opened 8 months ago

evanrich commented 8 months ago

First of all, this custom component is absolutely amazing. I'm thinking of donating just for how awesome this is. I love that clicking a link takes you directly to the media in sonarr/radarr, or the recently added in plex takes you directly to the plex episode/film. It would be amazing however, if this behavior could be customized so that it actually played the clicked media (maybe after a configurable delay) on a configurable player.

I imagine having this dashboard on a tablet near my media center (and one in my bedroom). It would be incredible if I could pick up the tablet, click a button, and the selected movie/tv episode plays on the plex system I have in front of me. It seems like the plex_recently_added card launches the web ui for plex on my computer, so I imagine this would involve calling some action (launch plex on X device, tell plex to play Y episode), etc. It doesn't seem like this component accepts custom actions, so that would have to be added.

I found a post https://www.reddit.com/r/homeautomation/comments/4urhyh/got_my_roku_to_play_plex_movies_via_api/ that indicates that it seems possible to get a specific device to play this, and since the plex integration might provide this, it might be doable. This enhancement would basically turn this UI into a home theater remote.

Thanks!

mkanet commented 8 months ago

@evanrich You're welcome. In theory, this is possible based on the API docs. I need to think of how I would even implement this considering Upcoming Media Card already opens the movie/episode in Plex Web when the item is clicked. I wouldnt want to remove this existing functionality; considering its already possible to playback the episode/movie (or trailer) with just a couple of clicks on the local device via Plex Web.

Also, I dont think its possible for the user to just click on the episode/movie on Upcoming Media card; and, it automatically knows what Plex client is in front of you to playback the media automatically. You would have to either configure ahead of time what default Plex client to playback on (limited to a single device until you change it) OR you would have to manually select from a list of Plex clients which device to playback on every time you click on the item on Upcoming Media Card. There may be a way to do this more easily; however, haven't thought of it yet. I'm open to suggestions on how this would integrate in Upcoming Media card without complicating current functionality.

BTW: This isn't something that I can implement in a matter of days. It would require a huge commitment and motivation on my part. I'm not even sure how attractive this feature would be to the current Upcoming Media Card users (and future users); considering not all Upcoming Media Card users use Plex.'

evanrich commented 7 months ago

I think you could accomplish this a few ways:

1.) add a config option that has the "device" on it. IF you were to use this in a "remote" situation, you might have one page opened for one media center, and another page open for another media center. Another way would be to allow a custom sensor or something, so for users that have presence sensors, or automations ("watch tv") might be able to set some value that the card picks up as the media center to play on. I'm about to head out of town but can think of some more ways when I get back, but this would solve the "which device to play on", not sure if HomeAssistant supports popups, but if so a popup could let you pick the plex device to play on (since the plex integration displays a list of users/players, i bet it has the player ID)

2.) Totally understandable, I'm a devops engineer by day, so I understand that this is a huge ask that can take a long time to implement. I'm not a HA custom component developer, but I can try and poke around when I have some free time over the next week or two maybe to see if i can at least hash out how to do this using plex's api along with some details to see if i can fire off episodes or movies at various players in my house (web, roku, shield, mobile phone). I'll update this request with any findings I have, and by all means I understand that if it's not something a ton of people want, that it shouldn't be a high priority, but I'm open to contributing as much as I can to this =)

Would you mind keeping this open at least for a while to see if i can hash out how much work it is to target players?

mkanet commented 7 months ago

I think you could accomplish this a few ways:

1.) add a config option that has the "device" on it. IF you were to use this in a "remote" situation, you might have one page opened for one media center, and another page open for another media center. Another way would be to allow a custom sensor or something, so for users that have presence sensors, or automations ("watch tv") might be able to set some value that the card picks up as the media center to play on. Would you mind keeping this open at least for a while to see if i can hash out how much work it is to target players?

@evanrich I've been thinking about this. I'm pretty sure I'm not going to have the time to see this all the way through. Initially, I thought after implementing support for this in Plex Recently Added integration, I could just enhance Upcoming Media Card to accommodate this capability (selecting the episode/movie to stream to a Plex client). However, UMC is pretty close to being maxed out; considering it already has multiple clickable elements.

Having said that, if you can implement a better-suited frontend component for this. I could help to add the support for this in Plex Recently Added integration.

evanrich commented 7 months ago

@mkanet This might be DOA. I spent about 2 hours today trying to make this work with an Nvidia shield, and it was a non starter. To begin with, plex no longer seems to return a list of clients. according to https://www.reddit.com/r/PleX/comments/8leq5k/plex_api_clients_request_empty_making_remote_api/, it seems that this has been broken for a year. This prevents returning a list of clients, IPs and ports. I was able to obviously figure out my client's IP from DHCP, but I couldn't find what port it was using or it's device ID. I tried getting the Device ID via:

 curl http://<IP OF PLEX SERVER>:32400/devices -H "X-Plex-Token: <API TOKEN>" 

This returns a list such as

<Device id="1" name="Plex Web (Chrome)" platform="" clientIdentifier="pn.........xr" createdAt="1473091598" />
<Device id="2" name="Plex Web (Chrome)" platform="" clientIdentifier="pfo..............vi" createdAt="1473092311" />
<Device id="3" name="Plex Web (Chrome)" platform="" clientIdentifier="e4i...........di" createdAt="1473092940" />
<Device id="4" name="Plex Web (Chrome)" platform="" clientIdentifier="hw.........di" createdAt="1473093271" />

and then I ran nmap against the Shield and got a list of about 12 ports.

PORT      STATE SERVICE
5555/tcp  open  freeciv
6466/tcp  open  unknown
6467/tcp  open  unknown
8008/tcp  open  http
8009/tcp  open  ajp13
8443/tcp  open  https-alt
8987/tcp  open  unknown
8988/tcp  open  unknown
9000/tcp  open  cslistener
10001/tcp open  scp-config
10101/tcp open  ezmeeting-2
32500/tcp open  unknown
45669/tcp open  unknown
45885/tcp open  unknown

thats all that shows up when scanning from 1024-60000

None of them seemed to work when I fed it into the POST via a curl command. It might be possible that the shield doesn't actually support this, so I can try with a Roku, my android device, and an ipad (as well as a web client if necessary) this week, but this is a lot more work than I had anticipated. For starters, Users using this would need to be able to get the following:

  1. device ID of the target device(s)
  2. IP of the target device(s)
  3. player port of the target device(s)
  4. XML library ID of the item to be played
  5. machine identifier of the plex server (looks like a UUID)
  6. Plex API token

Some of these are easy to get (like API Token, IP addresses, etc) but the device id, Machine ID and port are harder (like the roku port seems to be something like 8346, but on the Shield, no such port shows open)

I'll keep digging away at this but I'm kind of stuck at the moment.

mkanet commented 7 months ago

Thanks for trying. Yes, please keep me in the loop if you make any progress.

evanrich commented 7 months ago

Will do! I found another guide that's even more complex, that says you first have to create a play queue to be sent to the player

https://www.reddit.com/r/shortcuts/comments/dmydww/controlling_your_tv_with_shortcuts_and_a_plex/

https://www.reddit.com/r/PleX/comments/dox9a5/creating_a_play_queue_and_sending_it_to_a_player/

This got me a bit further, but the level of effort involved her is rather high, and depends on knowing a LOT of things. (a play queue ID, meta id of the library item, api token, server id (UUID) and a few other things. I'm pretty sure these don't get exposed in HA, although they could be stored as secrets or something I guess, but This is a rather convuluted process that probably can't be automated easily. I was hoping that plex's API would literally have something like "Play X media id on the player Y" since plex has all this info, but it turns out to not be as easy as I had hoped. I'll poke at it some more this week/weekend, but I may likely close this in the next few days.

maykar commented 7 months ago

This project may be of interest: https://github.com/pkkid/python-plexapi

Edit: Just noticed that your reddit link mentions it in the comments. I used it for Plex Assistant, it's fantastic.

mkanet commented 6 months ago

Hi @evanrich I'm curious how far you got with this?

evanrich commented 5 months ago

hey @mkanet haven't made much progress since my initial testing, one thing of notice though is that in the logs, Home Assistant seems to be aware of each individual player that is presented, I just haven't had a chance yet to dig into this due to work and life priorities. i should have some free time next week though to dig back into this. The fact that a player address/endpoint shows up in the logs and the UI is somewhat promising, just need to figure out how to send events to it.