-
-
Notifications
You must be signed in to change notification settings - Fork 37.9k
New Transmission component #19230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinHjelmare
merged 21 commits into
home-assistant:dev
from
MatteGary:transmission-dev
Jan 29, 2019
Merged
New Transmission component #19230
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0254cf7
New Transmission component and interaction
MatteGary 2d5ce74
Fix commit
MatteGary 6142058
Fix commit
MatteGary 8de3a2e
Fix + Switch checkin
MatteGary 86d3bea
Bugfixing
MatteGary ce529ff
Fix commit
MatteGary 0e8578d
Fix
MatteGary c7e444f
fix for missing config
MatteGary e3a645b
Update on requirements_all.txt
MatteGary eafafd8
Fix in requirements_all.txt
MatteGary c2cf118
Fix
MatteGary 80278e7
Fix for build
MatteGary 55a4e1d
fix
MatteGary e902ca0
Fix
MatteGary 8bc465d
Fix (again)
MatteGary a66676d
Fix
MatteGary 953a614
Fix indentation
MatteGary 40dd84a
Fix indentation
MatteGary 5dd815f
Fix Throttle
MatteGary 6c6fe87
Update .coveragerc
MatteGary 059f43c
Fix import and coveragerc
MatteGary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| """ | ||
| Component for monitoring the Transmission BitTorrent client API. | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/transmission/ | ||
| """ | ||
| from datetime import timedelta | ||
| import logging | ||
| import voluptuous as vol | ||
|
MatteGary marked this conversation as resolved.
|
||
| import homeassistant.helpers.config_validation as cv | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| from homeassistant.util import Throttle | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| from homeassistant.exceptions import PlatformNotReady | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| from homeassistant.helpers.event import track_time_interval | ||
| from homeassistant.helpers.discovery import load_platform | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| from homeassistant.const import (CONF_HOST, CONF_PASSWORD, CONF_NAME, | ||
| CONF_PORT, CONF_USERNAME, | ||
| CONF_MONITORED_VARIABLES) | ||
|
|
||
| REQUIREMENTS = ['transmissionrpc==0.11'] | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DOMAIN = 'transmission' | ||
| DATA_TRANSMISSION = 'TRANSMISSION' | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
|
|
||
| DEFAULT_NAME = 'Transmission' | ||
| DEFAULT_PORT = 9091 | ||
|
|
||
| SENSOR_TYPES = { | ||
| 'active_torrents': ['Active Torrents', None], | ||
| 'current_status': ['Status', None], | ||
| 'download_speed': ['Down Speed', 'MB/s'], | ||
| 'paused_torrents': ['Paused Torrents', None], | ||
| 'total_torrents': ['Total Torrents', None], | ||
| 'upload_speed': ['Up Speed', 'MB/s'], | ||
| 'completed_torrents': ['Completed Torrents', None], | ||
| 'started_torrents': ['Started Torrents', None], | ||
| } | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Required(CONF_HOST): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Optional(CONF_MONITORED_VARIABLES, default=['current_status']): | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]), | ||
| }) | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
| SCAN_INTERVAL = timedelta(minutes=2) | ||
|
MatteGary marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def setup(hass, config): | ||
|
MatteGary marked this conversation as resolved.
|
||
| hass.data[DATA_TRANSMISSION] = TransmissionData(hass, config) | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
|
|
||
| def refresh(event_time): | ||
|
MatteGary marked this conversation as resolved.
|
||
| hass.data[DATA_TRANSMISSION].update() | ||
|
|
||
| track_time_interval(hass, refresh, SCAN_INTERVAL) | ||
|
|
||
| sensorconfig = { | ||
| 'sensors': config[DOMAIN].get(CONF_MONITORED_VARIABLES), | ||
| 'client_name': config[DOMAIN].get(CONF_NAME)} | ||
| load_platform(hass, 'sensor', DOMAIN, sensorconfig) | ||
|
|
||
| _LOGGER.info("Transmission component setup completed.") | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| return True | ||
|
|
||
|
|
||
| class TransmissionData: | ||
|
MatteGary marked this conversation as resolved.
|
||
| """Get the latest data and update the states.""" | ||
|
|
||
| def __init__(self, hass, config): | ||
| import transmissionrpc | ||
| from transmissionrpc.error import TransmissionError | ||
| try: | ||
| """Initialize the Transmission RPC API""" | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| host = config[DOMAIN].get(CONF_HOST) | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| username = config[DOMAIN].get(CONF_USERNAME) | ||
| password = config[DOMAIN].get(CONF_PASSWORD) | ||
| port = config[DOMAIN].get(CONF_PORT) | ||
|
|
||
| api = transmissionrpc.Client( | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| host, port=port, user=username, password=password) | ||
|
|
||
| """Initialize the Transmission data object.""" | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| self.data = None | ||
| self.torrents = None | ||
| self.available = True | ||
| self._api = api | ||
| self.completed_torrents = [] | ||
| self.started_torrents = [] | ||
| self.initTorrentList() | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| self.hass = hass | ||
|
|
||
| except TransmissionError as error: | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| if str(error).find("401: Unauthorized"): | ||
| _LOGGER.error("Credentials for" | ||
| " Transmission client are not valid") | ||
| return | ||
|
|
||
| _LOGGER.warning( | ||
| "Unable to connect to Transmission client: %s:%s", host, port) | ||
| raise PlatformNotReady | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Throttle(SCAN_INTERVAL) | ||
| def update(self): | ||
| """Get the latest data from Transmission instance.""" | ||
| from transmissionrpc.error import TransmissionError | ||
|
|
||
| try: | ||
| self.data = self._api.session_stats() | ||
| self.torrents = self._api.get_torrents() | ||
|
|
||
| self.checkCompletedTorrent() | ||
| self.checkStartedTorrent() | ||
|
|
||
| _LOGGER.info("Torrent Data updated") | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| self.available = True | ||
| except TransmissionError: | ||
| self.available = False | ||
| _LOGGER.error("Unable to connect to Transmission client") | ||
|
|
||
| def initTorrentList(self): | ||
| self.torrents = self._api.get_torrents() | ||
| self.completed_torrents = [ | ||
| x.name for x in self.torrents if x.status == "seeding"] | ||
| self.started_torrents = [ | ||
| x.name for x in self.torrents if x.status == "downloading"] | ||
|
|
||
| def checkCompletedTorrent(self): | ||
| """Get completed torrent functionality""" | ||
| actual_torrents = self.torrents | ||
| actual_completed_torrents = [ | ||
| var.name for var in actual_torrents if var.status == "seeding"] | ||
|
|
||
| tmp_completed_torrents = list( | ||
| set(actual_completed_torrents).difference( | ||
| self.completed_torrents)) | ||
| if tmp_completed_torrents: | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| for var in tmp_completed_torrents: | ||
| _LOGGER.info("Completed Torrent found") | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| self.hass.bus.fire( | ||
| 'transmission_downloaded_torrent', { | ||
| 'name': var}) | ||
|
|
||
| self.completed_torrents = actual_completed_torrents | ||
|
|
||
| def checkStartedTorrent(self): | ||
| """Get started torrent functionality""" | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| actual_torrents = self.torrents | ||
| actual_started_torrents = [ | ||
| var.name for var | ||
| in actual_torrents if var.status == "downloading"] | ||
|
|
||
| tmp_started_torrents = list( | ||
| set(actual_started_torrents).difference( | ||
| self.started_torrents)) | ||
| if tmp_started_torrents: | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| for var in tmp_started_torrents: | ||
| self.hass.bus.fire( | ||
| 'transmission_started_torrent', { | ||
| 'name': var}) | ||
| self.started_torrents = actual_started_torrents | ||
|
|
||
| def getStartedTorrentCount(self): | ||
|
MatteGary marked this conversation as resolved.
Outdated
|
||
| return len(self.started_torrents) | ||
|
|
||
| def getCompletedTorrentCount(self): | ||
| return len(self.completed_torrents) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.