diff --git a/homeassistant/components/transmission/__init__.py b/homeassistant/components/transmission/__init__.py index d020bfe9745..76d9aedd8d5 100644 --- a/homeassistant/components/transmission/__init__.py +++ b/homeassistant/components/transmission/__init__.py @@ -397,42 +397,49 @@ class TransmissionData: def check_completed_torrent(self): """Get completed torrent functionality.""" + old_completed_torrent_names = { + torrent.name for torrent in self._completed_torrents + } + current_completed_torrents = [ torrent for torrent in self._torrents if torrent.status == "seeding" ] - freshly_completed_torrents = set(current_completed_torrents).difference( - self._completed_torrents - ) - self._completed_torrents = current_completed_torrents - for torrent in freshly_completed_torrents: - self.hass.bus.fire( - EVENT_DOWNLOADED_TORRENT, {"name": torrent.name, "id": torrent.id} - ) + for torrent in current_completed_torrents: + if torrent.name not in old_completed_torrent_names: + self.hass.bus.fire( + EVENT_DOWNLOADED_TORRENT, {"name": torrent.name, "id": torrent.id} + ) + + self._completed_torrents = current_completed_torrents def check_started_torrent(self): """Get started torrent functionality.""" + old_started_torrent_names = {torrent.name for torrent in self._started_torrents} + current_started_torrents = [ torrent for torrent in self._torrents if torrent.status == "downloading" ] - freshly_started_torrents = set(current_started_torrents).difference( - self._started_torrents - ) - self._started_torrents = current_started_torrents - for torrent in freshly_started_torrents: - self.hass.bus.fire( - EVENT_STARTED_TORRENT, {"name": torrent.name, "id": torrent.id} - ) + for torrent in current_started_torrents: + if torrent.name not in old_started_torrent_names: + self.hass.bus.fire( + EVENT_STARTED_TORRENT, {"name": torrent.name, "id": torrent.id} + ) + + self._started_torrents = current_started_torrents def check_removed_torrent(self): """Get removed torrent functionality.""" - freshly_removed_torrents = set(self._all_torrents).difference(self._torrents) - self._all_torrents = self._torrents - for torrent in freshly_removed_torrents: - self.hass.bus.fire( - EVENT_REMOVED_TORRENT, {"name": torrent.name, "id": torrent.id} - ) + current_torrent_names = {torrent.name for torrent in self._torrents} + + for torrent in self._all_torrents: + if torrent.name not in current_torrent_names: + self.hass.bus.fire( + EVENT_REMOVED_TORRENT, {"name": torrent.name, "id": torrent.id} + ) + + self._all_torrents = self._torrents.copy() def start_torrents(self): """Start all torrents."""