Skip to content

Commit

Permalink
Bump pytautulli (home-assistant#54594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Aug 16, 2021
1 parent 2eba633 commit a892605
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 32 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/tautulli/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"domain": "tautulli",
"name": "Tautulli",
"documentation": "https://www.home-assistant.io/integrations/tautulli",
"requirements": ["pytautulli==0.5.0"],
"requirements": ["pytautulli==21.8.1"],
"codeowners": ["@ludeeus"],
"iot_class": "local_polling"
}
97 changes: 67 additions & 30 deletions homeassistant/components/tautulli/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A platform which allows you to get information from Tautulli."""
from datetime import timedelta

from pytautulli import Tautulli
from pytautulli import PyTautulli
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
Expand Down Expand Up @@ -60,10 +60,19 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

session = async_get_clientsession(hass, verify_ssl)
tautulli = TautulliData(
Tautulli(host, port, api_key, hass.loop, session, use_ssl, path)
PyTautulli(
api_token=api_key,
hostname=host,
session=session,
verify_ssl=verify_ssl,
port=port,
ssl=use_ssl,
base_api_path=path,
)
)

if not await tautulli.test_connection():
await tautulli.async_update()
if not tautulli.activity or not tautulli.home_stats or not tautulli.users:
raise PlatformNotReady

sensor = [TautulliSensor(tautulli, name, monitored_conditions, user)]
Expand All @@ -88,25 +97,52 @@ def __init__(self, tautulli, name, monitored_conditions, users):
async def async_update(self):
"""Get the latest data from the Tautulli API."""
await self.tautulli.async_update()
self.home = self.tautulli.api.home_data
self.sessions = self.tautulli.api.session_data
self._attributes["Top Movie"] = self.home.get("movie")
self._attributes["Top TV Show"] = self.home.get("tv")
self._attributes["Top User"] = self.home.get("user")
for key in self.sessions:
if "sessions" not in key:
self._attributes[key] = self.sessions[key]
for user in self.tautulli.api.users:
if self.usernames is None or user in self.usernames:
userdata = self.tautulli.api.user_data
self._attributes[user] = {}
self._attributes[user]["Activity"] = userdata[user]["Activity"]
if self.monitored_conditions:
for key in self.monitored_conditions:
try:
self._attributes[user][key] = userdata[user][key]
except (KeyError, TypeError):
self._attributes[user][key] = ""
if (
not self.tautulli.activity
or not self.tautulli.home_stats
or not self.tautulli.users
):
return

self._attributes = {
"stream_count": self.tautulli.activity.stream_count,
"stream_count_direct_play": self.tautulli.activity.stream_count_direct_play,
"stream_count_direct_stream": self.tautulli.activity.stream_count_direct_stream,
"stream_count_transcode": self.tautulli.activity.stream_count_transcode,
"total_bandwidth": self.tautulli.activity.total_bandwidth,
"lan_bandwidth": self.tautulli.activity.lan_bandwidth,
"wan_bandwidth": self.tautulli.activity.wan_bandwidth,
}

for stat in self.tautulli.home_stats:
if stat.stat_id == "top_movies":
self._attributes["Top Movie"] = (
stat.rows[0].title if stat.rows else None
)
elif stat.stat_id == "top_tv":
self._attributes["Top TV Show"] = (
stat.rows[0].title if stat.rows else None
)
elif stat.stat_id == "top_users":
self._attributes["Top User"] = stat.rows[0].user if stat.rows else None

for user in self.tautulli.users:
if (
self.usernames
and user.username not in self.usernames
or user.username == "Local"
):
continue
self._attributes.setdefault(user.username, {})["Activity"] = None

for session in self.tautulli.activity.sessions:
if not self._attributes.get(session.username):
continue

self._attributes[session.username]["Activity"] = session.state
if self.monitored_conditions:
for key in self.monitored_conditions:
self._attributes[session.username][key] = getattr(session, key)

@property
def name(self):
Expand All @@ -116,7 +152,9 @@ def name(self):
@property
def native_value(self):
"""Return the state of the sensor."""
return self.sessions.get("stream_count")
if not self.tautulli.activity:
return 0
return self.tautulli.activity.stream_count

@property
def icon(self):
Expand All @@ -140,14 +178,13 @@ class TautulliData:
def __init__(self, api):
"""Initialize the data object."""
self.api = api
self.activity = None
self.home_stats = None
self.users = None

@Throttle(TIME_BETWEEN_UPDATES)
async def async_update(self):
"""Get the latest data from Tautulli."""
await self.api.get_data()

async def test_connection(self):
"""Test connection to Tautulli."""
await self.api.test_connection()
connection_status = self.api.connection
return connection_status
self.activity = await self.api.async_get_activity()
self.home_stats = await self.api.async_get_home_stats()
self.users = await self.api.async_get_users()
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,7 @@ pysyncthru==0.7.3
pytankerkoenig==0.0.6

# homeassistant.components.tautulli
pytautulli==0.5.0
pytautulli==21.8.1

# homeassistant.components.tfiac
pytfiac==0.4
Expand Down

0 comments on commit a892605

Please sign in to comment.