wolffshots / hass-audiobookshelf

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

Session Count Not Correct #15

Closed 25garfield closed 3 months ago

25garfield commented 4 months ago

Even when no one is actively listening to the server, the session count is showing 2 sessions.

Where can I start to look to fix this?

I've removed and re-added the integration via HACs

this is the error I'm getting

AudioBookShelf version is 2.9.0

Logger: custom_components.audiobookshelf Source: helpers/update_coordinator.py:315 integration: Audiobookshelf (documentation, issues) First occurred: May 2, 2024 at 6:07:53 PM (2 occurrences) Last logged: 10:57:58 AM

Unexpected error fetching audiobookshelf data Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 315, in _async_refresh self.data = await self._async_update_data() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/audiobookshelf/init.py", line 66, in _async_update_data num_users = self.api.count_active_users(users_update) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/audiobookshelf/api.py", line 41, in count_active_users for user in data["users"]:


TypeError: 'NoneType' object is not subscriptable
wolffshots commented 3 months ago

I can't quite work out how to recreate this, can you let me know some details about your setup? The error it's reporting there is actually {host}/api/users call and if it fails there it won't do the subsequent call to update sessions

25garfield commented 3 months ago

my home-assistant is running as a vm on proxmox, and my audio bookshelf is a docker, the count is incrementing and going up, it just doesn't look like its going down, its always showing as connected? Is there anything you can point me at on the audiobookshelf server I can look at of a wget I can issue form a machine to see what it returns?

wolffshots commented 3 months ago

So the session sensor should be counting the openSessions entries on this endpoint: http://<audiobookshelf ip>:13378/api/users/online The unexpected error above is based on the http://<audiobookshelf ip>:13378/api/users endpoint where it is trying to do this on the result:

def count_active_users(data: dict) -> int:
    """
    Takes in an object with an array of users
    and counts the active ones minus
    the dummy hass one
    """
    count = 0
    for user in data["users"]:
        if user["isActive"] and user["username"] != "hass":
            if ("token" in user and user["token"] == API_KEY):
                continue  # Skip user with provided access_token
            count += 1
    return count
25garfield commented 3 months ago

ok so getting closer, at least I now now that the api is working

https://audiobooks.xxxxxx.co.uk/api/users/online?token=xxxxxxxxxx

grrrrrr not a happy bunny, for some reason the server had held open some disconnected sessions and so was always showing them in the results, I've kicked the server a few times and now it showing correctly.

I sorry for the confusion, but on a plus point I now know how to work with the api 🙄🤦‍♀️

wolffshots commented 3 months ago

Ah cool cool, no stress. I'm in the process of redoing the code so hopefully I can surface some of these things when I do