Core: Plex media_player clients not recognized

Created on 16 Mar 2018  Â·  78Comments  Â·  Source: home-assistant/core

Home Assistant release (hass --version):
0.65.5

Python release (python3 --version):
Python 3.6.3

Component/platform:
plex (media_player)

Description of problem:
Clients/players not created as entities.

Problem-relevant configuration.yaml entries and steps to reproduce:

- platform: plex
  include_non_clients: true
  use_custom_entity_ids: true

Client entities are never added. Attempted to start hass with clients actively playing media, and nothing is logged or displayed.

Have tested with homeassistant.components.media_player.plex set to debug under logger config.

Additional info:
Only log entries shown for the component:

2018-03-15 17:57:06 INFO (SyncWorker_14) [homeassistant.components.media_player.plex] Discovery configuration done (no token needed)
2018-03-15 17:57:06 INFO (SyncWorker_14) [homeassistant.components.media_player.plex] Connected to: http://<my_plex_server>:32400
plex

Most helpful comment

Is there any update on this?

It is still an issue for me, using Plex client on an NVidia Shield TV. The Plex Server is on a QNAP NAS.

Home-Assistant is 0.69.1.

If there's anything I can do to help diagnose the issue, or test a solution, I will happily do what I can.

Nigel

All 78 comments

Note that I do see the active sessions/players if I check from the same host:

curl http://<my_plex_server>:32400/status/sessions?X-Plex-Token=<token>

So I’ve fought with this a bit more and I’ve been able to get web clients to (usually) trigger a media_player entity creation, even adding it to the entity registry. However all of the “native” clients such as on iOS, Apple TV, or Plex Media Player (Mac) appear to create a media_player.unnamed_device# entity which go away after a restart. These of course aren’t very useful.

Digging a bit deeper (and setting all logs to a more verbose level), I see that these players for some reason give the same error as in #11959 where a machineIdentifier can’t be found. When checking manually with the URL in my previous comment, all devices provide a proper unique identifier. Is this component using a different method to query the Plex server for devices?

"Optional cleanup of clients is coming" in this PR: https://github.com/home-assistant/home-assistant/pull/13156

Non-Clients are currently broken, in fact both these issues are described here:
https://github.com/home-assistant/home-assistant/issues/10074

I've just bought an apple tv 3 to try and get to the bottom of the non-clients. But it wont be here until monday and I'm travelling for work till Friday. So it will be next weekend before i get to look at it.

I have the same problem that @jjlawren described.

I started with two iOS devices playing a movie, Plex on a TV playing a movie and a movie playing on the web version (Chrome). The only one that shows up in Home Assistant is the TV media player.

One of the iOS devices showed up as a media player (not in the UI), has been added to the entity_registry.yaml, but when I stopped the movie on it, it kept logging the machineIdentifier errors. The other iOS device doesn't even show up in the entity_registry, but that might be because the machineIdentifier error had already happened.

Even after a restart the client that started the error is still there, although I closed the app. The web version doesn't show up either, but was added to the entity_registry.

The web version not working is strange as i do ~90% of my testing on chrome.

Thinking back to several major releases ago, I _do_ remember iOS devices reporting playback properly and getting entity names with unique IDs.

Ok, so I did another test and somehow everything works now.

The web version, both iOS clients and the Plex client on my TV, they now all show up as media players and they've also shown up in the entity_registry.yaml file. I haven't actually changed anything, so I'm not sure what happened here.

When I stop a video on one of the clients, that client isn't removed from Home Assistant though, so right now I do still have the same issue as described in #10074.

@kevintuhumury #10074 will be fixed in 0.66 (hopefully)

@kevintuhumury, what is your Plex config? Are you using include_non_clients and use_custom_entity_ids?

@jjlawren This is my Plex config:

- platform: plex
  entity_namespace: plex
  include_non_clients: true
  use_episode_art: true

@kevintuhumury that's very odd. I've matched your config but I can only get web clients to show up with proper identifiers. All my iOS and PMP apps create 'unnamed_device' entity names but report an unavailable status with null attributes.

Can you think of any other variables in your setup? For example, are you signed into your Plex account on all devices? Are all devices on the local network?

What kind of unique IDs are the iOS apps getting in the entity registry? Are they UUID-style?

@jjlawren I am signed into all of the devices with a single account. I don't have Plex Pass. All devices are on the local network.

Yes, all of the iOS apps have unique ID's in the form of an UUID. The characters aren't lowercase though.

Matched your config yet my Apple TV, iPhone, and smart TV apps all create “unnamed” entities, unavailable status, and empty attributes. Some clients triggered creation of multiple entities as there’s no identifying info available.

I _do_ have a Plex Pass, and I use Plex Home with a primary/admin account and one managed account. More info on Plex Home is here if that seems relevant: https://support.plex.tv/articles/203960236-consequences-of-being-in-a-plex-home/

@ryanm101, I just realized that I applied your diff from #10074 during these tests above. Now that I've reverted and went back to the component as it's provided in 0.65.5, the non-web clients no longer get a media_player.unnamed_device# entity and the machineIdentifer errors mentioned in #10074 show in the log once a minute while playing.

I don't see a fix for this issue in #10074 or #10450, which both have similar backtraces. Can I help you get to the bottom of this issue here?

I'm just back from a work trip and my apple tv has arrived so I've now something to test against.
I'll spend some time on this today hopefully.

I think i see the issue.

I'll have some test code up later today

I think there are two issues in this:

  1. Non Clients -> There is no machineID
  2. Custom Entities -> We use the machineID as the unique_id but where we have custom entities we should use the entity_id.

fixing 2 should solve issue 1.

@ryanm101, I’m not sure that’s the problem. All the “problem” clients I have report a proper machineIdentifier when I request this URL while playing:
http://<my_plex_server>:32400/status/sessions?X-Plex-Token=<token>

Does this component use a different Plex endpoint?

@jjlawren I'm confused:

the non-web clients no longer get a media_player.unnamed_device# entity and the machineIdentifer errors mentioned in #10074 show in the log once a minute while playing.

After removing your fix (https://github.com/ryanm101/home-assistant/commit/3fe45239feb6eb7c5ffe3a7c7f6e07716442532c), the component no longer created _any_ entities for the problem devices. In addition, I get a backtrace like the one from https://github.com/home-assistant/home-assistant/issues/10074#issuecomment-358098076 every minute as a problem client is playing.

Ok so the errors are because self._device is None which is where it is looking for the machine_identifier

Which is what that "patch" corrected, I was just stopping it looking in an object that didnt exist

Right, but when the patch was applied, those players never got a machineIdentifier nor an entry in the entity registry. I’m saying they definitely do have a machineIdentifier as reported by the Plex server, but this component doesn’t pick it up.

+1 with Android TV client on Nvidia Shield

+1 same error here...

@jjlawren yes but reported by the server may be different to how the plexapi presents it. (Not saying it is just that may be the case.)

Sorry for delay in this but Chrome & amazon tablet work fine.
Not setup ATV yet.
Will try firestick as well

I was using Android and Chrome and got that...

@joaoasilva can you post your cfg please? and Device types
Also what OS was chrome on?

Hi @ryanm101
Config:

  - platform: plex
    host: <host>
    port: <port>
    entity_namespace: 'plex'
    include_non_clients: true
    scan_interval: 10
    use_custom_entity_ids: false
    use_episode_art: true
    show_all_controls: true

Curl:

<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="1">
<Video addedAt="1521564200" art="/library/metadata/1206/art/1521570672" duration="2572926" grandparentArt="/library/metadata/1206/art/1521570672" grandparentKey="/library/metadata/1206" grandparentRatingKey="1206" grandparentThumb="/library/metadata/1206/thumb/1521570672" grandparentTitle="Ride with Norman Reedus" guid="com.plexapp.agents.thetvdb://310633/1/1?lang=en" index="1" key="/library/metadata/1208" lastViewedAt="1523895228" librarySectionID="2" librarySectionKey="/library/sections/2" librarySectionTitle="TV Shows" originallyAvailableAt="2016-06-12" parentIndex="1" parentKey="/library/metadata/1207" parentRatingKey="1207" parentTitle="Season 1" ratingKey="1208" sessionKey="14" summary="Norman and his friend Imogen Lehtonen ride up the California Pacific Coast Highway to check out some dune buggies, ride a futuristic bike and do a podcast." thumb="/library/metadata/1208/thumb/1521570668" title="California: Pacific Coast Highway" type="episode" updatedAt="1521570668" viewOffset="0" year="2016">
<Media audioProfile="lc" id="8713" videoProfile="main" audioChannels="2" audioCodec="aac" bitrate="10047" container="mp4" duration="2572926" height="1080" optimizedForStreaming="1" protocol="dash" videoCodec="h264" videoFrameRate="PAL" videoResolution="1080p" width="1920" selected="1">
<Part audioProfile="lc" id="8713" videoProfile="main" bitrate="10047" container="mp4" duration="2572926" height="1080" optimizedForStreaming="1" protocol="dash" width="1920" decision="transcode" selected="1">
<Stream bitrate="9823" codec="h264" default="1" frameRate="25" height="1080" id="18001" streamType="1" width="1920" decision="transcode" location="segments-video" />
<Stream bitrate="224" bitrateMode="cbr" channels="2" codec="aac" default="1" id="18002" selected="1" streamType="2" decision="copy" location="segments-audio" />
<Stream codec="ass" container="ass" format="ass" id="18006" key="/library/streams/18006" language="English" languageCode="eng" selected="1" streamType="3" decision="transcode" location="segments-subs" />
</Part>
</Media>
<User id="1" thumb="https://plex.tv/users/$$myuser$$/avatar?c=24234234" title="$$myuser$$" />
<Player address="$$IPADDRESS$$" device="OSX" machineIdentifier="$$mymachine$$" model="" platform="Chrome" platformVersion="65.0" product="Plex Web" profile="Web" remotePublicAddress="$$IPADDRESS$$" state="playing" title="Chrome" vendor="" version="3.47.1" local="0" userID="1" />
<Session id="$$SESSIONID$$" bandwidth="10517" location="wan" />
<TranscodeSession key="/transcode/sessions/$$KEY$$" throttled="0" complete="0" progress="1.2000000476837158" speed="3.5" duration="2572926" remaining="686" context="streaming" sourceVideoCodec="hevc" sourceAudioCodec="aac" videoDecision="transcode" audioDecision="copy" subtitleDecision="transcode" protocol="dash" container="mp4" videoCodec="h264" audioCodec="aac" audioChannels="2" transcodeHwRequested="0" timeStamp="1523957016.7774587" maxOffsetAvailable="29" minOffsetAvailable="1" />
</Video>
</MediaContainer>

Error:

2018-04-17 10:55:36 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform plex
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 82, in async_setup
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/media_player/plex.py", line 101, in setup_platform
    hass, config, add_devices_callback
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/media_player/plex.py", line 238, in setup_plexserver
    update_devices()
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/util/__init__.py", line 319, in wrapper
    result = method(*args, **kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/util/__init__.py", line 319, in wrapper
    result = method(*args, **kwargs)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/media_player/plex.py", line 189, in update_devices
    update_sessions)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/media_player/plex.py", line 328, in __init__
    self.refresh(device, session)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/media_player/plex.py", line 394, in refresh
    if self._device.machineIdentifier is not None and \
AttributeError: 'NoneType' object has no attribute 'machineIdentifier'

Thanks

what device is that?

It's on the curl response. Chrome on Mac

are you connecting by webclient to the server directly or are you going via plex.tv?

Hi @ryanm101 , going via plex.tv. But the same error happens when I connect using the Plex Android APP.

does it work if you access the PMS server directly VIA IP? x.x.x.x:32400/web?

Just tried, the same error.

Did some debug and devices = plexserver.clients() line 155 doesn't return any devices, any ideas? Thanks

If you set include_non_clients to false do you still get the error..

also you can clean up your config by removing:
use_custom_entity_ids: false
entity_namespace: 'plex'

I've just used chrome on a macbook pro and went direct via IP to teh PMS server and via plex.tv and everything works as expected

What Plex URL(s) is this component querying to retrieve clients and activity? Is it http://<my_plex_server>:32400/status/sessions?

we use plexapi to handle it all

What can we gather to help troubleshoot this since we have multiple similar reports? Maybe a trivial script that uses the plexapi library to dump interesting data to compare?

In some basic testing, I can't get my clients to show with a .clients() call, but .sessions() reports active playbacks:

Example script:

from plexapi.server import PlexServer
baseurl = 'http://<plexserver>:32400
token = 'secrettoken'
plex = PlexServer(baseurl, token)

print(plex.account())
print(plex.myPlex)
print(plex.sessions())
print(plex.clients())

Output:

<Account:[email protected]>
True
[<Movie:197105:The-Lion-King>]
[]

@jjlawren You've hit the 1st problem.. Clients not populating (which should only be true for non-clients, i also think the shield is affected).. We can do it via sessions() and in fact some of the code does reference that, I'm trying to reproduce so i can walk though code with real data.

I'll take another look tonight as I should have some time.

I'm really confused by chrome not working on peoples setups as I've used chrome on 3 different OSes and it works.. I was hoping for it to be a constant. I'm sure its the non-clients setting that is some different affect.

I think I'm going to have to try and write some pytest mocks for this component as there are a fair few configs to test.

@ryanm101, my example above was when playing through Plex Web, which should be considered a native client.

I checked my logs this morning and I'm seeing the AttributeError: 'NoneType' object has no attribute 'machineIdentifier' backtraces once a minute for hours when there have been no clients playing. Same backtrace, but perhaps a different root issue.

It should be but if you disable non-clients do you still get the error?

No, but then all clients come in as media_player.unnamed_device# and report an unknown status.

Is there any update on this?

It is still an issue for me, using Plex client on an NVidia Shield TV. The Plex Server is on a QNAP NAS.

Home-Assistant is 0.69.1.

If there's anything I can do to help diagnose the issue, or test a solution, I will happily do what I can.

Nigel

No update yet, had a few things irl that have distracted me. Will start looking at this again.

Well, if I can help, let me know what you need

Is this component broken completely. I'm not getting any entities but the sensor works fine

I'm currently trying to build out a proper test env before i tackle this. So it will be a bit longer due to work travel.

It's working fine for me, but it seems that there are a number of cases where it is broken if you use the include_non_clients feature.

@ryanm101 what is your config like? I am having the same issue

@ryanm101 I don't think that the include_non_clients feature is the issue. Here's my current plex platform section.

  - platform: plex
    entity_namespace: 'plex'
#    include_non_clients: false
#    scan_interval: 5
#    show_all_controls: true
    use_custom_entity_ids: true
#    use_episode_art: true
#    remove_unavailable_clients: true
#    client_remove_interval: 600

yes, but i want home assistant to show plex status, i want include_non_clients set to true so i can see if other people are connected to my plex server

I was playing around with the plex component last week and at this point I think I got it to work like I would expect, in the meantime I also played around with the python code and got the non_clients to properly show(they still show the full controls tho and that is not entirely right since you can't control a non-client, just get the session info, but I have to find out where I can change that).

First a few words about the Shield TV client. At first it was not showing at all as an entity in home assistant, while for example a chrome web player was being properly registered as an entity. After finding this thread I thought that the shield tv client is not a real client like the apple connect, so I tried the include_non_clients: true option which lead me to the
AttributeError: 'NoneType' object has no attribute 'machineIdentifier
exception, so tried to fix that part of the code and I think I got it to properly work. The shield tv now showed up as an entity(keep in mind that entity_namespace is needed if you have the discovery or chromecast component enabled because there is also a chromecast with the same name as the plex client on the shield)

If someone wants to test this, i just pushed the updated plex.py in this repo:
https://github.com/etheralm/home-assistant/blob/plex_fixes/homeassistant/components/media_player/plex.py

Anyway at this point I dug around a bit more and found out from the official plex documentation that the android tv client should be a fully featured client that should also be remotely controllable:
https://support.plex.tv/articles/203082707-supported-plex-companion-apps/

So based on this info I thought that I should be able to fling content at least from the plex server web interface to the shield tv client, but the client was not showing in the the list of devices.

screen shot 2018-06-11 at 14 24 24

After some more testing I figured out that the reason for this was that the firewall on the centos server where I'm running the plex server docker image was blocking the multicast communication needed to register the shield tv client properly, when I fixed my firewall rules the shield tv popped up as a client in the web interface and I was able to fling content to it and to control it. At this point I fired up home assistant again and the fully featured plex entity popped up too :). I'm now able to see what's playing on that client and to control it.

I'm just putting this info here in hopes that it will help you too. Let me know if I could be of any other assistance.

So to try this, I would just download your plex.py and copy into my custom_components folder?

yes, just remember to preserve the folder structure, so it should go in custom_components/media_player/plex.py

Btw I noticed that you mentioned earlier that you are trying to get a shield tv client to work. Normally you wouldn't need to run this fix since this is a fix for the "include_non_clients" and the shield tv client works as normal client when properly configured (eg. you can pause it and etc from the web interface).
Do you see your shield tv client listed in the plex server web interface (in the cast devices list, see the screenshot above)?

P.S. wouldn't hurt to test it as a non client in the meantime tho. It should show up as an entity with this fix even if the client is not available in plex itself as a controllable client.

For whatever reason, I'm not seeing the SHIELD client showing up at all.

I can get Chrome clients to show up no problem, but if I pause playback on chrome, and switch to the plex app on an android phone, or the Shield, I see nothing.

I'll download this and give it a try, and let you know in a little bit how it works for me.

And unfortunately, I'm not seeing any difference.

I wonder if this is being coomplicated on my network because I have two Plex servers running. I've got the Plex server I actually use running on my NAS, but there's also a PLEX server instance on the Shield.

Oh well, I'll probably see if I can dig into this in the near future.

Having 2 servers on the network shouldn't be a problem, but I'm not sure if it will be if one of them is also the shield tv.

Btw did you try it with the option: include_non_clients: true? it won't work otherwise.

Otherwise I had exactly the same problem as you, chrome and other web plex clients were showing up, but not the shield tv and for me it was the firewall where the plex server was running. I wasn't even able to figure out exactly what was being blocked, but the moment I turned off the firewall, the shield tv client showed up in the plex web interface and also in home assistant, so in the end I just whitelisted the shield tv ip on the firewall and now everything is running fine.

I’ve had better luck with @etheralm’s custom component so far. I haven’t tested for controls and other details, but new players appear and log errors are gone.

The component doesn't seem to be available anymore? I'd like to give a try as it's still not working for me.

For some reason the URL in the link above is bogus. Copy/paste it or try this one: https://github.com/etheralm/home-assistant/blob/plex_fixes/homeassistant/components/media_player/plex.py

Thank you very much @jjlawren, that is working beautifully for me! Dropped into the custom_components/media_player folder, a quick restart and I see my Shield again! :)

(yet, it wasn't working for me a month ago apparently)

Thank @etheralm for the fixes.

Any plans to make a pull request or is there a need to dig deeper. @ryanm101, have you made any progress or reviewed @etheralm’s changes?

Unfortunately, this didn't fix anything for me.. My plex chrome goes unavailable after a while and won't reappear until I restart HA..

the fix I did is for "non controllable" clients like the apple one (haven't tested it with it tho, if someone can it would be great).

@AbysFlo I have no idea why this may be happening, I never had a problem with the chrome players and for them you don't need the include_non_clients option. Can you paste your ha config for plex here? Also when the player goes unavailable can you check if its still available in in the plex ui?

I've tried with and without including non_clients without change. It goes unavailable when I shut down my computer, which is kind of normal as the player is not available, but it doesn't get back to available once I get back to my computer and go to plex.

It used to work well before...

media_player:
  - platform: plex
    scan_interval: 1
    show_all_controls: true
    include_non_clients: true
    use_custom_entity_ids: true
    use_episode_art: true

I'll try out the scenario you described when I get back home, lets hope that I'll be able to reproduce it.

Thanks for helping, to clarify as my memory is coming back to me, before it never went to unavailable and was always "Idle" if it was off or not playing anything. I'll do more testing tonight as well to provide more details if I can.

@etheralm, thanks for the custom component. Its got my samsung tv working for me which in the last few updates of hass have some how been broken. Running on 0.76.0

@etheralm, thanks for the fixes in plex.py. Using your file caused my clients to show when I set include_non_clients to true (instead of getting the 'machineIdentifier' error in this issue).

That said, the thumbs weren't being pulled in, and the limited controls that appeared didn't work, so I decided to revert back to the original file (and set include_non_clients to false) for now.

Problem still exists in 0.82.3? Having the same issue here.. Another SHIELD user..

Same here, doesn't work on mine using NVIDIA SHIELD.

All of @etheralm links to his plex.py are 404. I'm having this problem too and would love to test that script.

The issue still occurring in 0.84.6 (using HASS.io). I don't see ANY media_player.plex...service or any media_player.unnamed_device. Really ALL I see is my autodetected Apple TVs on my local network (which is unrelated to plex component). My plex server is non-local btw.

Logs on boot:

2018-12-22 11:46:59 INFO (MainThread) [homeassistant.components.media_player] Setting up media_player.plex
2018-12-22 11:47:00 INFO (SyncWorker_8) [homeassistant.components.media_player.plex] Discovery configuration done (no token needed)
2018-12-22 11:47:00 INFO (SyncWorker_8) [homeassistant.components.media_player.plex] Connected to: http://[my-plex-server-url]

configuration.yaml

media_player:
  - platform: plex
    entity_namespace: plex
    include_non_clients: true
    use_custom_entity_ids: true
    scan_interval: 1
    remove_unavaliable_clients: false
    client_remove_interval: 60

plex.conf

{
    "url:port": {
        "ssl": false,
        "token": "Correct_token",
        "verify": true
    }
}

Errors I see in logs:

018-12-22 11:39:30 ERROR (MainThread) [homeassistant.core] Error doing job: Future exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/plex.py", line 142, in 
    track_utc_time_change(hass, lambda now: update_devices(), second=30)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/util/__init__.py", line 324, in wrapper
    result = method(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/util/__init__.py", line 324, in wrapper
    result = method(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/plex.py", line 182, in update_devices
    update_sessions)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/plex.py", line 321, in __init__
    self.refresh(device, session)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/media_player/plex.py", line 387, in refresh
    if self._device.machineIdentifier is not None and \
AttributeError: 'NoneType' object has no attribute 'machineIdentifier'

@balloob this appears to be related to https://github.com/home-assistant/home-assistant/issues/11959 which you dug into a while back. Just FYI.

Hmm, looks like I deleted that branch by mistake, let me see if I can dig it out.

Anyway just keep in mind that while you can get it working with my changes, you won't be able to control the player in anyway, just see basic information about what it is playing. Its how the apple client works, nothing we can do about it.

edit:
@that1guy found it and uploaded it again, here you go:
https://github.com/etheralm/home-assistant/blob/plex_fixes/homeassistant/components/media_player/plex.py

I've submitted a PR to fix this in Home Assistant itself #20074.

i have this issue too.

Was this page helpful?
0 / 5 - 0 ratings