From 6a3c11195c47a423106e2ecd5a5dcfff4b18d105 Mon Sep 17 00:00:00 2001 From: smega Date: Sat, 29 Feb 2020 16:34:24 +0000 Subject: [PATCH 1/5] Extend rtorrent sensor functionality. --- homeassistant/components/rtorrent/sensor.py | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index c6833fcfda038b..3887521de890c9 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -21,12 +21,24 @@ SENSOR_TYPE_CURRENT_STATUS = "current_status" SENSOR_TYPE_DOWNLOAD_SPEED = "download_speed" SENSOR_TYPE_UPLOAD_SPEED = "upload_speed" +SENSOR_TYPE_ALL_TORRENTS = "all_torrents" +SENSOR_TYPE_STOPPED_TORRENTS = "stopped_torrents" +SENSOR_TYPE_COMPLETE_TORRENTS = "complete_torrents" +SENSOR_TYPE_UPLOADING_TORRENTS = "uploading_torrents" +SENSOR_TYPE_DOWNLOADING_TORRENTS = "downloading_torrents" +SENSOR_TYPE_ACTIVE_TORRENTS = "active_torrents" DEFAULT_NAME = "rtorrent" SENSOR_TYPES = { SENSOR_TYPE_CURRENT_STATUS: ["Status", None], SENSOR_TYPE_DOWNLOAD_SPEED: ["Down Speed", DATA_RATE_KILOBYTES_PER_SECOND], SENSOR_TYPE_UPLOAD_SPEED: ["Up Speed", DATA_RATE_KILOBYTES_PER_SECOND], + SENSOR_TYPE_ALL_TORRENTS : ["All Torrents", None], + SENSOR_TYPE_STOPPED_TORRENTS : ["Stopped Torrents", None], + SENSOR_TYPE_COMPLETE_TORRENTS : ["Complete Torrents", None], + SENSOR_TYPE_UPLOADING_TORRENTS : ["Uploading Torrents", None], + SENSOR_TYPE_DOWNLOADING_TORRENTS : ["Downloading Torrents", None], + SENSOR_TYPE_ACTIVE_TORRENTS : ["Active Torrents", None], } PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( @@ -102,6 +114,11 @@ def update(self): multicall = xmlrpc.client.MultiCall(self.client) multicall.throttle.global_up.rate() multicall.throttle.global_down.rate() + multicall.d.multicall2("", "main") + multicall.d.multicall2("", "stopped") + multicall.d.multicall2("", "complete") + multicall.d.multicall2("", "seeding", "d.up.rate=") + multicall.d.multicall2("", "leeching", "d.down.rate=") try: self.data = multicall() @@ -113,6 +130,21 @@ def update(self): upload = self.data[0] download = self.data[1] + alltorrents = self.data[2] + stoppedtorrents = self.data[3] + completetorrents = self.data[4] + + uploading_torrents = 0 + for uptorrent in self.data[5]: + if uptorrent[0] > 0: + uploading_torrents += 1 + + downloading_torrents = 0 + for downtorrent in self.data[6]: + if downtorrent[0] > 0: + downloading_torrents += 1 + + active_torrents = uploading_torrents + downloading_torrents if self.type == SENSOR_TYPE_CURRENT_STATUS: if self.data: @@ -132,3 +164,16 @@ def update(self): self._state = format_speed(download) elif self.type == SENSOR_TYPE_UPLOAD_SPEED: self._state = format_speed(upload) + elif self.type == SENSOR_TYPE_ALL_TORRENTS: + self._state = len(alltorrents) + elif self.type == SENSOR_TYPE_STOPPED_TORRENTS: + self._state = len(stoppedtorrents) + elif self.type == SENSOR_TYPE_COMPLETE_TORRENTS: + self._state = len(completetorrents) + elif self.type == SENSOR_TYPE_UPLOADING_TORRENTS: + self._state = uploading_torrents + elif self.type == SENSOR_TYPE_DOWNLOADING_TORRENTS: + self._state = downloading_torrents + elif self.type == SENSOR_TYPE_ACTIVE_TORRENTS: + self._state = active_torrents + From 5f38e93abe878a3b3b38227a211c65010732afb8 Mon Sep 17 00:00:00 2001 From: smega Date: Sat, 29 Feb 2020 16:58:56 +0000 Subject: [PATCH 2/5] Remove blank line from end of file. --- homeassistant/components/rtorrent/sensor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index 3887521de890c9..ce0717adb3b1d1 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -176,4 +176,3 @@ def update(self): self._state = downloading_torrents elif self.type == SENSOR_TYPE_ACTIVE_TORRENTS: self._state = active_torrents - From 293119650e9d586fe8e43503a895fbcb9e3e30ba Mon Sep 17 00:00:00 2001 From: smega Date: Sat, 29 Feb 2020 17:22:40 +0000 Subject: [PATCH 3/5] After using black formatter. --- homeassistant/components/rtorrent/sensor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index ce0717adb3b1d1..a0e0e7bb2a9a8e 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -33,12 +33,12 @@ SENSOR_TYPE_CURRENT_STATUS: ["Status", None], SENSOR_TYPE_DOWNLOAD_SPEED: ["Down Speed", DATA_RATE_KILOBYTES_PER_SECOND], SENSOR_TYPE_UPLOAD_SPEED: ["Up Speed", DATA_RATE_KILOBYTES_PER_SECOND], - SENSOR_TYPE_ALL_TORRENTS : ["All Torrents", None], - SENSOR_TYPE_STOPPED_TORRENTS : ["Stopped Torrents", None], - SENSOR_TYPE_COMPLETE_TORRENTS : ["Complete Torrents", None], - SENSOR_TYPE_UPLOADING_TORRENTS : ["Uploading Torrents", None], - SENSOR_TYPE_DOWNLOADING_TORRENTS : ["Downloading Torrents", None], - SENSOR_TYPE_ACTIVE_TORRENTS : ["Active Torrents", None], + SENSOR_TYPE_ALL_TORRENTS: ["All Torrents", None], + SENSOR_TYPE_STOPPED_TORRENTS: ["Stopped Torrents", None], + SENSOR_TYPE_COMPLETE_TORRENTS: ["Complete Torrents", None], + SENSOR_TYPE_UPLOADING_TORRENTS: ["Uploading Torrents", None], + SENSOR_TYPE_DOWNLOADING_TORRENTS: ["Downloading Torrents", None], + SENSOR_TYPE_ACTIVE_TORRENTS: ["Active Torrents", None], } PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( From 1d5a61476c4ee6a35ca0473ce0ca222864ce3836 Mon Sep 17 00:00:00 2001 From: smega Date: Fri, 6 Mar 2020 17:43:32 +0000 Subject: [PATCH 4/5] Update sensor.py using snake_case for variable names. --- homeassistant/components/rtorrent/sensor.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index a0e0e7bb2a9a8e..769dcae70ecca8 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -130,18 +130,18 @@ def update(self): upload = self.data[0] download = self.data[1] - alltorrents = self.data[2] - stoppedtorrents = self.data[3] - completetorrents = self.data[4] + all_torrents = self.data[2] + stopped_torrents = self.data[3] + complete_torrents = self.data[4] uploading_torrents = 0 - for uptorrent in self.data[5]: - if uptorrent[0] > 0: + for up_torrent in self.data[5]: + if up_torrent[0] > 0: uploading_torrents += 1 downloading_torrents = 0 - for downtorrent in self.data[6]: - if downtorrent[0] > 0: + for down_torrent in self.data[6]: + if down_torrent[0] > 0: downloading_torrents += 1 active_torrents = uploading_torrents + downloading_torrents @@ -165,11 +165,11 @@ def update(self): elif self.type == SENSOR_TYPE_UPLOAD_SPEED: self._state = format_speed(upload) elif self.type == SENSOR_TYPE_ALL_TORRENTS: - self._state = len(alltorrents) + self._state = len(all_torrents) elif self.type == SENSOR_TYPE_STOPPED_TORRENTS: - self._state = len(stoppedtorrents) + self._state = len(stopped_torrents) elif self.type == SENSOR_TYPE_COMPLETE_TORRENTS: - self._state = len(completetorrents) + self._state = len(complete_torrents) elif self.type == SENSOR_TYPE_UPLOADING_TORRENTS: self._state = uploading_torrents elif self.type == SENSOR_TYPE_DOWNLOADING_TORRENTS: From 9eebbc02ffc0493e77a90d4df7c94dfde193c8ff Mon Sep 17 00:00:00 2001 From: smega Date: Tue, 10 Mar 2020 18:58:42 +0000 Subject: [PATCH 5/5] Update PR by using true value in condition. --- homeassistant/components/rtorrent/sensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index 769dcae70ecca8..cd27b33271f4ed 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -136,12 +136,12 @@ def update(self): uploading_torrents = 0 for up_torrent in self.data[5]: - if up_torrent[0] > 0: + if up_torrent[0]: uploading_torrents += 1 downloading_torrents = 0 for down_torrent in self.data[6]: - if down_torrent[0] > 0: + if down_torrent[0]: downloading_torrents += 1 active_torrents = uploading_torrents + downloading_torrents