-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Refactor nzbget to support future platform changes #26462
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6368afa
Re-factor nzbget platform to enable future features.
chriscla c227528
Re-factor nzbget platform to enable future features.
chriscla eca5bef
Use pynzbgetapi instead of raw HTTP requests
chriscla 9338b54
Re-factor nzbget platform to enable future features.
chriscla d14d32e
Re-factor nzbget platform to enable future features.
chriscla deb771b
Using pynzbgetapi
chriscla 4af1991
Pinning pynzbgetapi version.
chriscla 7a57c77
Requiring pynzbgetapi 0.2.0
chriscla 1c720d4
Addressing review comments
chriscla a96068a
Refreshing requirements (adding pynzbgetapi)
chriscla c847d3a
Remove period from logging message
chriscla 520107d
Updating requirements file
chriscla a689eb8
Add nzbget init to .coveragerc
chriscla 153e7b1
Adding nzbget codeowner
chriscla 6486708
Updating codeowners file
chriscla 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
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 |
|---|---|---|
| @@ -1 +1,106 @@ | ||
| """The nzbget component.""" | ||
| from datetime import timedelta | ||
| import logging | ||
|
|
||
| import pynzbgetapi | ||
| import requests | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.const import ( | ||
| CONF_HOST, | ||
| CONF_NAME, | ||
| CONF_PASSWORD, | ||
| CONF_PORT, | ||
| CONF_SCAN_INTERVAL, | ||
| CONF_SSL, | ||
| CONF_USERNAME, | ||
| ) | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.dispatcher import dispatcher_send | ||
| from homeassistant.helpers.event import track_time_interval | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DOMAIN = "nzbget" | ||
| DATA_NZBGET = "data_nzbget" | ||
| DATA_UPDATED = "nzbget_data_updated" | ||
|
|
||
| DEFAULT_NAME = "NZBGet" | ||
| DEFAULT_PORT = 6789 | ||
|
|
||
| DEFAULT_SCAN_INTERVAL = timedelta(seconds=5) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema( | ||
| { | ||
| DOMAIN: vol.Schema( | ||
| { | ||
| vol.Required(CONF_HOST): cv.string, | ||
| vol.Optional(CONF_PASSWORD): cv.string, | ||
| vol.Optional(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_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL | ||
| ): cv.time_period, | ||
| vol.Optional(CONF_SSL, default=False): cv.boolean, | ||
| } | ||
| ) | ||
| }, | ||
| extra=vol.ALLOW_EXTRA, | ||
| ) | ||
|
|
||
|
|
||
| def setup(hass, config): | ||
| """Set up the NZBGet sensors.""" | ||
| host = config[DOMAIN][CONF_HOST] | ||
| port = config[DOMAIN][CONF_PORT] | ||
| ssl = "s" if config[DOMAIN][CONF_SSL] else "" | ||
| name = config[DOMAIN][CONF_NAME] | ||
| username = config[DOMAIN].get(CONF_USERNAME) | ||
| password = config[DOMAIN].get(CONF_PASSWORD) | ||
| scan_interval = config[DOMAIN][CONF_SCAN_INTERVAL] | ||
|
|
||
| try: | ||
| nzbget_api = pynzbgetapi.NZBGetAPI(host, username, password, ssl, ssl, port) | ||
| nzbget_api.version() | ||
| except pynzbgetapi.NZBGetAPIException as conn_err: | ||
| _LOGGER.error("Error setting up NZBGet API: %s", conn_err) | ||
| return False | ||
|
|
||
| _LOGGER.debug("Successfully validated NZBGet API connection") | ||
|
|
||
| nzbget_data = hass.data[DATA_NZBGET] = NZBGetData(hass, nzbget_api) | ||
| nzbget_data.update() | ||
|
|
||
| def refresh(event_time): | ||
| """Get the latest data from NZBGet.""" | ||
| nzbget_data.update() | ||
|
|
||
| track_time_interval(hass, refresh, scan_interval) | ||
|
|
||
| sensorconfig = {"client_name": name} | ||
|
|
||
| hass.helpers.discovery.load_platform("sensor", DOMAIN, sensorconfig, config) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class NZBGetData: | ||
| """Get the latest data and update the states.""" | ||
|
|
||
| def __init__(self, hass, api): | ||
| """Initialize the NZBGet RPC API.""" | ||
| self.hass = hass | ||
| self.status = None | ||
| self.available = True | ||
| self._api = api | ||
|
|
||
| def update(self): | ||
| """Get the latest data from NZBGet instance.""" | ||
| try: | ||
| self.status = self._api.status() | ||
| self.available = True | ||
| dispatcher_send(self.hass, DATA_UPDATED) | ||
| except requests.exceptions.ConnectionError: | ||
| self.available = False | ||
| _LOGGER.error("Unable to refresh NZBGet data") | ||
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
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
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.