wolffshots / hass-audiobookshelf

Adds sensors for an Audiobookshelf server to Home Assistant to show connection and active sessions
MIT License
38 stars 2 forks source link

Adding "Now playing" data a la spotify/sonos #3

Open ianhyzy opened 12 months ago

ianhyzy commented 12 months ago

This is awesome integration, thank you. If it's possible I would love to be able to pull the cover of the audiobook I'm listening to, like how Spotify or Sonos show the album art when an entity is playing.

wolffshots commented 11 months ago

I’m not actually sure how to do this but I’ll do some digging when I have some time! Would be cool to add

sdholden28 commented 5 months ago

This is probably not the most efficient way to do this, but I've accomplished this through a couple of rest sensors and a template sensor for each user. I have person cards on dashboards and whenever a user is using audiobookshelf or spotify, they get headphones on their avatar and the book-author or title-track scrolls across the bottom of their person card in marquee fashion.

#Rest sensor to get all online users from ABS via api
  - platform: rest
    name: ABS Online Users
    resource: http://(ABS SERVER:PORT)/api/users/online?token=(ROOT USER API TOKEN)
    value_template: "{{ value_json.usersOnline | length }}"
    json_attributes:
      - usersOnline
    scan_interval: 15

#Rest sensor to get listenings sessions info for all users from ABS via api    
  - platform: rest
    name: ABS User Listening Sessions
    resource: http://(ABS SERVER:PORT)/api/users/(USER ID)/listening-sessions?token=(ROOT USER API TOKEN)
    value_template: "{{ value_json.sessions | length }}"
    json_attributes:
      - sessions
    scan_interval: 15

# AudioBookShelf active session sensors
    - name: "ABS user Active Session"
      state: >
          {% set user_id = '(USER ID)' %}
          {% set users_online = state_attr('sensor.abs_online_users', 'usersOnline') %}
          {% set sessions = state_attr('sensor.abs_user_listening_sessions', 'sessions') %}

          {% if users_online is not none and sessions is not none %}
            {% set user_online = users_online | selectattr('id', 'equalto', user_id) | list %}

            {% if user_online | length > 0 %}
              {% if sessions | length > 0 %}
                {% set session = sessions[0] %}

                {% if session.mediaMetadata and session.mediaMetadata.authors %}
                  📖 "{{ session.mediaMetadata.title }}" by {{ session.mediaMetadata.authors[0].name }}
                {% else %}
                  Active session with missing media metadata
                {% endif %}
              {% else %}
                No sessions found
              {% endif %}
            {% else %}
              Not Listening
            {% endif %}
          {% else %}
            No data
          {% endif %}
      unique_id: "abs_user_active_session"

# Sensor for name of media being played by user
    - name: "user Media Playing"
      state: >
          {% set abs_state = states('sensor.abs_user_active_session') %}
          {% set spotify_state = states('media_player.spotify_user') %}
          {% set spotify_title = state_attr('media_player.spotify_user', 'media_title') %}
          {% set spotify_artist = state_attr('media_player.spotify_user', 'media_artist') %}
          {% if abs_state != 'Not Listening' and abs_state != 'unknown' %}
             {{ abs_state }}
          {% elif spotify_state == 'playing' and spotify_title != none and spotify_artist != none %}
            🎵 {{ spotify_title }} - {{ spotify_artist }}
          {% else %}
            Not Listening
          {% endif %}
      unique_id: "user_media_playing"

Sorry for the poor quality gif conversion, but you get the idea. personcard

wolffshots commented 5 months ago

Thanks @sdholden28, that will be a great help! Sorry that I haven't gotten this added sooner, I've been trying to redo the code to be generally better but am not a Python wizard so haven't made much progress