Jump to content

Nvidia Shield + Home Assistant (home-assistant.io)


D34DC3N73R

Recommended Posts

D34DC3N73R

This is a minor annoyance, but still seems worth reporting. I've recently got a bunch of smart lights that I'm using with home-assistant.io. One of my favorite automations is dimming the living room lights when a movie or show is playing after sunset. The way I have my automations set up is by reading the play state of the nvidia shield. On pause, idle, or standby, the lights brighten. When playing, the lights dim. Unfortunately, when using emby, the state changes to "paused" just when opening the app, and continues to stay "paused" even when content is playing. I've attached some screen shots from the dev tools of home assistant for reference. Can the app be updated to correctly report play states on the shield?

post-374015-0-13838400-1584862842_thumb.jpg

post-374015-0-31175600-1584862850_thumb.jpg

Link to comment
Share on other sites

D34DC3N73R

The screens show emby/plex dashboards on the left, and device states from the developer tools of home-assistant.io on the right.

Link to comment
Share on other sites

But where is that "device state" actually coming from is my question...?  Any idea?

Link to comment
Share on other sites

D34DC3N73R

It's coming from the Shield. Home assistant connects via adb to the shield and the device state is polled every 10 seconds.

Link to comment
Share on other sites

D34DC3N73R

I've got a thread open in the home assistant forums as well to see if I can find more information. I just thought it may be a problem with the emby app, because every other app on the shield reports play states correctly.

Link to comment
Share on other sites

I know of no adb command that will get you a general "playback status" from the device.  They have to be querying something specific - perhaps from the built in media player which we don't use. However, neither does Plex so... I'm just not sure at this point.  Please let us know what you find out.

Link to comment
Share on other sites

D34DC3N73R

Ok, So it looks like their androidtv package polls 3 properties to determine the device state.
 

 

Unfortunately, there is no standard API for determining the state of the device to which all apps adhere. Instead, the backend androidtv package uses three of the properties that it collects to determine the state: audio_state, media_session_state, and wake_lock_size.

 

I've figured out how to issue the adb command GET_PROPERTIES from HA and how to read the adb response. So now I'll just have to do some testing to figure out the difference between plex and emby in these 3 parameters during different device states. They also have implemented custom state detection. So I should be able to get this working without any changes in the emby app. But (I think), if it was possible to set these parameters to match plex/netflix/etc then this type of customization wouldn't be required for future users of HA + emby.

Link to comment
Share on other sites

D34DC3N73R

Here's what got from testing devices states in various apps

Netflix playing
'audio_state': 'paused', 'wake_lock_size': 2, 'current_app': 'com.netflix.ninja', 'media_session_state': 3

Netflix paused
'audio_state': 'paused', 'wake_lock_size': 0, 'current_app': 'com.netflix.ninja', 'media_session_state': 2

netflix idle
'audio_state': 'paused', 'wake_lock_size': 0, 'current_app': 'com.netflix.ninja', 'media_session_state': 1

netflix preview
'audio_state': 'paused', 'wake_lock_size': 2, 'current_app': 'com.netflix.ninja', 'media_session_state': 3


Android TV Launcher
'audio_state': 'paused', 'wake_lock_size': 0, 'current_app': 'com.google.android.tvlauncher', 'media_session_state': None


Plex idle
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'com.plexapp.android', 'media_session_state': None

Plex paused
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'com.plexapp.android', 'media_session_state': 2

Plex playing
'audio_state': 'paused', 'wake_lock_size': 3, 'current_app': 'com.plexapp.android', 'media_session_state': 3


Emby idle
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'tv.emby.embyatv', 'media_session_state': None

Emby paused
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'tv.emby.embyatv', 'media_session_state': None

Emby playing
'audio_state': 'paused', 'wake_lock_size': 3, 'current_app': 'tv.emby.embyatv', 'media_session_state': None


YouTube idle
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'com.google.android.youtube.tv', 'media_session_state': 0

Youtube paused
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'com.google.android.youtube.tv', 'media_session_state': 2

Youtube playing
'audio_state': 'paused', 'wake_lock_size': 3, 'current_app': 'com.google.android.youtube.tv', 'media_session_state': 3

This led me to the following config for the Nvidia Shield

media_player:
  - platform: androidtv
    name: Shield
    host: 192.168.0.152
    apps:
      tv.emby.embyatv: "Emby"
      com.netflix.ninja: "Netflix"
      com.google.android.tvlauncher: "Android TV Launcher"
      com.plexapp.android: "Plex"
      com.google.android.youtube.tv: "YouTube"
    state_detection_rules:
      'tv.emby.embyatv':
        - 'playing':
            'wake_lock_size': 3
        - 'standby':
            'wake_lock_size': 1
      'com.netflix.ninja':
        - 'playing':
            'media_session_state': 3
            'wake_lock_size': 2
        - 'paused':
            'media_session_state': 2
            'wake_lock_size': 0
        - 'standby':
            'media_session_state': 1
            'wake_lock_size': 0
      'com.google.android.tvlauncher':
        - 'standby':
            'wake_lock_size': 0
      'com.plexapp.android':
        - 'playing':
            'media_session_state': 3
            'wake_lock_size': 3
        - 'paused':
            'media_session_state': 2
            'wake_lock_size': 1
        - 'standby':
            'wake_lock_size': 1
      'com.google.android.youtube.tv':
        - 'playing':
            'media_session_state': 3
            'wake_lock_size': 3
        - 'paused':
            'media_session_state': 2
            'wake_lock_size': 1
        - 'standby':
            'media_session_state': 0
            'wake_lock_size': 1

In my initial testing it seems to be working fine, but I haven't tested thoroughly. I'm guessing emby has trouble because media_session_state is always 'None', and paused and idle have the same wake_lock_size. I dealt with this by just removing the paused state for emby. I'll follow up if I notice and issues.

Edited by D34DC3N73R
Link to comment
Share on other sites

No problem. Any thoughts on setting a media_session_state in the emby atv app?

 

We'll look at it.

Link to comment
Share on other sites

seanbuff

I would also be interested in this, bring Emby inline with the other media apps.

Link to comment
Share on other sites

D34DC3N73R

Just an update on this, the way I have it currently set up isn't without fail. A couple times throughout a movie, the lights would brighten when content was still playing and dim back down a second or two later. So, while what I have is much better than not working at all, I'd still really appreciate media_session_state being added to the app.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
MRobi

@@D34DC3N73R what are you using to test the device states? I'd like to set this up with TiViMate for when I'm watching IPTV.

 

Also, have you checked out HA's emby integration?

https://www.home-assistant.io/integrations/emby/

It creates an entity for each emby device and you can use the play states as triggers. It would require adding a second trigger to your existing automation or just adding a second automation just for emby.

This is how I've implemented it in Node-Red, does exactly what you're looking to do and has been flawless for me.

https://emby.media/community/index.php?/topic/81794-home-assistant-0104/?p=882114

Link to comment
Share on other sites

D34DC3N73R

Yes I have seen the emby integration, but it seems to take longer than the default Android TV integration to recognize pauses. I'd prefer that media_session_state be integrated into the app to avoid a bunch of duplicate or conditional automations, and to conform to other media app standards.

 

To get device states you can use developer tools > services tab in home assistant.  You'll want to use the androidtv.adb_command service, and select the androidtv entity you'll be testing. That will autofill the entity_id in service data, and then you'll want to issue the adb command GET_PROPERTIES. 

 
entity_id: media_player.your_androidtv_device
command: GET_PROPERTIES

After calling the service, check the states tab, find the device and look at the adb_response. You'll have to capture devices states for play, pause, and standby and then compare the properties audio_state, wake_lock_size, and media_session_state.
Link to comment
Share on other sites

MRobi

 

Yes I have seen the emby integration, but it seems to take longer than the default Android TV integration to recognize pauses. I'd prefer that media_session_state be integrated into the app to avoid a bunch of duplicate or conditional automations, and to conform to other media app standards.

 

That's interesting. I find the emby integration to be the fastest I've used on any home automation platform. Including faster than the androidtv integration which seems to poll the state instead of receiving it. With the emby integration, my finger is still pressed down on the button and it's already firing the automation.

 

Your method to pull states works. Interestingly TiviMate, probably the most popular IPTV app on Android TV, also doesn't use media_session_state. It just says "None".

Edited by MRobi
Link to comment
Share on other sites

D34DC3N73R
Emby idle
'audio_state': 'paused', 'wake_lock_size': 0, 'current_app': 'tv.emby.embyatv', 'media_session_state': 0

Emby paused
'audio_state': 'paused', 'wake_lock_size': 1, 'current_app': 'tv.emby.embyatv', 'media_session_state': 2

Emby playing
'audio_state': 'paused', 'wake_lock_size': 2, 'current_app': 'tv.emby.embyatv', 'media_session_state': 3

Looks good! I'll update my automations and test it out some more when the sun sets. Thanks for taking the time to implement this!

  • Like 1
Link to comment
Share on other sites

D34DC3N73R

Update: Working great with updated custom state detection rules. Also, seems to be working just fine with no custom state detection rules, although I haven't tested it this way quite as long.

Link to comment
Share on other sites

This will change again in the next beta so you'll want to re-test then.

 

Thanks.

Link to comment
Share on other sites

Okay, there is a new beta (.06).  Please test that these are still working for you.

 

Thanks.

Link to comment
Share on other sites

D34DC3N73R

Here's what I got with the latest beta (1.8.07)

emby idle
'audio_state': 'paused', 'wake_lock_size': 2, 'current_app': 'tv.emby.embyatv', 'media_session_state': 0

emby paused
'audio_state': 'paused', 'wake_lock_size': 3, 'current_app': 'tv.emby.embyatv', 'media_session_state': 3

emby playing
'audio_state': 'paused', 'wake_lock_size': 4, 'current_app': 'tv.emby.embyatv', 'media_session_state': 3

Without using custom state detection rules, it's enough for Home Assistant to determine the difference between idle and playing, but not enough to determine the difference between playing and paused. With that said, it would be better if media_session_state had a different value for playing vs paused. Thanks again for taking the time to look into this.

Link to comment
Share on other sites

Here's what I got with the latest beta (1.8.07)

emby idle
'audio_state': 'paused', 'wake_lock_size': 2, 'current_app': 'tv.emby.embyatv', 'media_session_state': 0

emby paused
'audio_state': 'paused', 'wake_lock_size': 3, 'current_app': 'tv.emby.embyatv', 'media_session_state': 3

emby playing
'audio_state': 'paused', 'wake_lock_size': 4, 'current_app': 'tv.emby.embyatv', 'media_session_state': 3

Without using custom state detection rules, it's enough for Home Assistant to determine the difference between idle and playing, but not enough to determine the difference between playing and paused. With that said, it would be better if media_session_state had a different value for playing vs paused. Thanks again for taking the time to look into this.

 

That was just an oversight.  Should get fixed with the next one.  Thanks for testing.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...