Emby and Nvidia Shield device states

Ok, I set that up and the entity state works with the emby integration. I was just hoping to use the the media_player platform so I don’t have to duplicate every automation just for emby. Is there a reason every other app works with the media_player platform but emby doesn’t?

No idea sorry, couldn’t you just add the extra triggers to your automations ?

As I’m thinking about this more, I’ll still have a problem because of conflicting entity states. If I add the emby integration entity_id to my current automations as a trigger, the media_player entitty_id will still read as paused when emby is playing. The same would be true for a completely separate automation. So, right now I’m stuck with either emby working, or every other app working, but not both.

Maybe use a condition in the automations to check the app_id attribute of the shield and only enable the emby automation if it is tv.emby.embyatv and the other to check if it is not. Does that make sense ? just thinking on the fly, can’t test

As I’m reading more about this, I’m thinking it’s likely emby needs custom state detection. So far, I’ve figured out how to issue the ADB command GET_PROPERTIES, and read the adb_response. However, I’m a little lost on how to implement custom state detection rules.

So, after testing device states on various apps these were my results

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 to my config

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

I haven’t tested thoroughly but it worked with a brief run-through. I’ll follow up if I notice any issues.

2 Likes

For anyone who happens to come across this, these emby settings don’t work without fail. I’ve had my lights brighten and dim a few seconds later a couple times during a movie. I’m trying to get the emby devs to add media_session_state to their app to more reliably determine the device play state.

Out of curiosity how did you setup your integration? I did not see references to adb_server_ip or port in yours so was curious. Just a reference to the shield IP.

I’m just using the default values so they’re not specified in the yaml.

Thanks, I have another topic with my setup and issues, however when you have:

    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"

Do you find if you select the pulldown for the media_player ONLY those items show? Also does the TV Launcher actually work for you? When I select it nothing happens and doesn’t go back to the main screen. Also YouTube doesn’t show on the list either so have some weirdness. I also have it set to only show those apps but I get other things that I didn’t add in that apps list.

No, there are plenty of other items in the pull down and the apps listed in the yaml config aren’t necessarily in that list. I only have those since it matched the examples I found for custom state detection. That section may not even be required for custom state detection to work.

Which makes sense as you do not use the exclude_unnamed_apps. For me I do but still get apps not in the list. When you select if you have “Android TV Launcher” does it go to the home page?

No, it doesn’t but I don’t really use the cards and rarely log into the interface. Not sure exactly what you’re trying to do, but you might have to use the adb_command service to issue a home command.

Do your automations work quickly? For me the playing and stopping work great but the pause usually doesn’t report back for about 15 seconds or so and then it shows ‘paused’ and so the transition to turning lights back on if you pause doesn’t really work that great with the Emby integration for myself.

Not sure about the emby integration specifically, but using the android tv integration with custom state detection works as fast as any other app on my shield.

After you plug in your custom states, how are you triggering light automations off of that? Maybe I’m over thinking it but I’m not following how you do that piece.

I went through to verify those items with the adb get_properties but I dont see where it’s helping anything really.

Regular media players you just set based on the state it’s reporting but if I used my shield as the media player to trigger it, I would imagine I’d have to have it check if it’s Emby and then do the lower/raising of lights.

For me it would be triggering the lights off the android tv and if the app is Jellyfin (which is a fork of Emby, so the Emby integration works)… not just if the Android TV is playing anything.

I read the device state of the shield as a trigger, use after sunset as a condition, and then call a service to change the light level.

- alias: Dim lights when Shield is playing
  trigger:
  - entity_id: media_player.shield_2
    platform: state
    to: playing
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      brightness: 55
      transition: 2
    entity_id: light.lamp, light.other_lamp
    service: light.turn_on

- alias: Brighten lights when Shield is idle
  trigger:
  - entity_id: media_player.shield_2
    platform: state
    to: standby
  - entity_id: media_player.shield_2
    platform: state
    to: paused
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data:
      brightness: 155
      transition: 2
    entity_id: light.lamp, light.other_lamp
    service: light.turn_on

I’m still pretty new with home assistant so maybe there’s a better way. These were some of my first automations.

Ahh I see. Yeah I have the same automation basically but I wouldn’t want it to auto dim lights if things are playing on the shield, only if it’s Jellyfin/Emby playing.

Otherwise I don’t want it to dim for YouTubeTV, YouTube, etc.

I’m guessing I’ll just have to stick with the emby media player as the entity ID then… just a pain in the butt because the start/stop triggers are instant but the pause/unpause information that is relayed takes a solid 15-20 seconds sometimes before it reports back to HA and therefore making the pause/unpause automations for the lights very slow.

Might be a little hacky, but I think you could use custom states to override the apps play states when you don’t want the lights to change.

Yeah maybe. I was also just noticing that when I start something on Android TV, it isn’t updating to ‘playing’ for a solid 6-8 seconds, same goes for paused/stopping too.

I previously used Plex with Tautulli with MQTT and it was instant and so nice but wanted to abandon that platform due to trust issued and move to an open-source solution in Jellyfin.

The start/stop work perfectly so maybe I’ll just have to deal with the slow response when pausing.