Skip to content

Commit

Permalink
feat: sensor tracking open sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jadon committed Aug 9, 2023
1 parent aba2e92 commit e19fb73
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
11 changes: 10 additions & 1 deletion custom_components/audiobookshelf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(

async def _async_update_data(self):
"""Update data via library."""
update = {"connectivity": None, "users": None}
update = {"connectivity": None, "users": None, "sessions": None}
try:
connectivity_update = await self.api.api_wrapper(
method="get", url=self.api.get_host() + "/ping"
Expand All @@ -58,6 +58,15 @@ async def _async_update_data(self):
update["users"] = num_users
except Exception as exception: # pylint: disable=broad-exception-caught
update["users"] = exception
try:
online_users_update = await self.api.api_wrapper(
method="get", url=self.api.get_host() + "/api/users/online"
)
open_sessions = self.api.count_open_sessions(online_users_update)
_LOGGER.info("""async_update open_sessions: %s""", open_sessions)
update["sessions"] = open_sessions
except Exception as exception: # pylint: disable=broad-exception-caught
update["sessions"] = exception
return update


Expand Down
6 changes: 6 additions & 0 deletions custom_components/audiobookshelf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def count_active_users(self, data):
count += 1
return count

def count_open_sessions(self, data):
"""
Counts the number of open stream sessions
"""
return len(data["openSessions"])

async def api_wrapper(
self, method: str, url: str, data: dict = None, headers: dict = None
) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/audiobookshelf/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AudiobookshelfBinarySensor(AudiobookshelfEntity, BinarySensorEntity):
@property
def name(self):
"""Return the name of the binary_sensor."""
return f"{DOMAIN}_binary_sensor"
return f"{DOMAIN}_connected"

@property
def device_class(self):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/audiobookshelf/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@

SCAN_INTERVAL = timedelta(seconds=30)

CONF_ACCESS_TOKEN = "default token"
CONF_HOST = "default host"
CONF_ACCESS_TOKEN = "access_token"
CONF_HOST = "host"
4 changes: 2 additions & 2 deletions custom_components/audiobookshelf/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class AudiobookshelfSensor(AudiobookshelfEntity):
@property
def name(self):
"""Return the name of the sensor."""
return f"{DOMAIN}_sensor"
return f"{DOMAIN}_sessions"

@property
def state(self):
"""Return the state of the sensor."""
try:
coordinator_get = self.coordinator.data.get(
"users", ""
"sessions", ""
) # need to work out how to add functionality to the coordinator to fetch /api/users
_LOGGER.info("""sensor coordinator got: %s""", coordinator_get)
if isinstance(coordinator_get, int):
Expand Down

0 comments on commit e19fb73

Please sign in to comment.