From a892605a90395c1789ea2da361b20b3fcef5365a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 16 Aug 2021 16:15:42 +0200 Subject: [PATCH] Bump pytautulli (#54594) --- .../components/tautulli/manifest.json | 2 +- homeassistant/components/tautulli/sensor.py | 97 +++++++++++++------ requirements_all.txt | 2 +- 3 files changed, 69 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/tautulli/manifest.json b/homeassistant/components/tautulli/manifest.json index cb2e38ebd6d79f..d413e47739713d 100644 --- a/homeassistant/components/tautulli/manifest.json +++ b/homeassistant/components/tautulli/manifest.json @@ -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" } diff --git a/homeassistant/components/tautulli/sensor.py b/homeassistant/components/tautulli/sensor.py index 67df02cb15d2be..16b58b206aafbd 100644 --- a/homeassistant/components/tautulli/sensor.py +++ b/homeassistant/components/tautulli/sensor.py @@ -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 @@ -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)] @@ -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): @@ -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): @@ -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() diff --git a/requirements_all.txt b/requirements_all.txt index db0885255bc656..9203b216eb7b10 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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