-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Move config and connections to Plex component #26488
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 19 commits into
home-assistant:dev
from
jjlawren:plex_common_config
Sep 9, 2019
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a89a8cb
Move config and connections to component
jjlawren 9033c0a
Separate imports
jjlawren d73264d
Set a unique_id on sensor
jjlawren c50fa62
Set a platforms const
jjlawren 0136429
Add SERVERS dict, hardcode to single server
jjlawren cae33d7
Move to debug
jjlawren 79e7492
Return false
jjlawren f56beec
More debug
jjlawren f4e4cf5
Import at top to fix lint
jjlawren 630a67e
Guard against legacy setup attempts
jjlawren 288940b
Refactor to add setup callback
jjlawren 7ca9592
Review comments
jjlawren c26fc57
Log levels
jjlawren 9d5dc91
Return result of callback
jjlawren 08fd236
Store CONFIGURING in hass.data
jjlawren 37b5be3
Set up discovery if no config data
jjlawren bab5f66
Use schema to set defaults
jjlawren 8935088
Remove media_player options to remove entities
jjlawren f13adf3
Improve error handling
jjlawren 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 |
|---|---|---|
| @@ -1 +1,201 @@ | ||
| """The plex component.""" | ||
| """Support to embed Plex.""" | ||
| import logging | ||
|
|
||
| import plexapi.exceptions | ||
| import requests.exceptions | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.discovery import SERVICE_PLEX | ||
| from homeassistant.components.media_player import DOMAIN as MP_DOMAIN | ||
| from homeassistant.const import ( | ||
| CONF_HOST, | ||
| CONF_PORT, | ||
| CONF_SSL, | ||
| CONF_TOKEN, | ||
| CONF_URL, | ||
| CONF_VERIFY_SSL, | ||
| ) | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers import discovery | ||
| from homeassistant.util.json import load_json, save_json | ||
|
|
||
| from .const import ( | ||
| CONF_USE_EPISODE_ART, | ||
| CONF_SHOW_ALL_CONTROLS, | ||
| DEFAULT_HOST, | ||
| DEFAULT_PORT, | ||
| DEFAULT_SSL, | ||
| DEFAULT_VERIFY_SSL, | ||
| DOMAIN as PLEX_DOMAIN, | ||
| PLATFORMS, | ||
| PLEX_CONFIG_FILE, | ||
| PLEX_MEDIA_PLAYER_OPTIONS, | ||
| SERVERS, | ||
| ) | ||
| from .server import PlexServer | ||
|
|
||
| MEDIA_PLAYER_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Optional(CONF_USE_EPISODE_ART, default=False): cv.boolean, | ||
| vol.Optional(CONF_SHOW_ALL_CONTROLS, default=False): cv.boolean, | ||
| } | ||
| ) | ||
|
|
||
| SERVER_CONFIG_SCHEMA = vol.Schema( | ||
| { | ||
| vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, | ||
| vol.Optional(CONF_TOKEN): cv.string, | ||
| vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, | ||
| vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean, | ||
| vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, | ||
| vol.Optional(MP_DOMAIN, default={}): MEDIA_PLAYER_SCHEMA, | ||
|
MartinHjelmare marked this conversation as resolved.
|
||
| } | ||
| ) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({PLEX_DOMAIN: SERVER_CONFIG_SCHEMA}, extra=vol.ALLOW_EXTRA) | ||
|
|
||
| CONFIGURING = "configuring" | ||
| _LOGGER = logging.getLogger(__package__) | ||
|
|
||
|
|
||
| def setup(hass, config): | ||
| """Set up the Plex component.""" | ||
|
|
||
| def server_discovered(service, info): | ||
| """Pass back discovered Plex server details.""" | ||
| if hass.data[PLEX_DOMAIN][SERVERS]: | ||
| _LOGGER.debug("Plex server already configured, ignoring discovery.") | ||
| return | ||
| _LOGGER.debug("Discovered Plex server: %s:%s", info["host"], info["port"]) | ||
| setup_plex(discovery_info=info) | ||
|
|
||
| def setup_plex(config=None, discovery_info=None, configurator_info=None): | ||
|
MartinHjelmare marked this conversation as resolved.
|
||
| """Return assembled server_config dict.""" | ||
| json_file = hass.config.path(PLEX_CONFIG_FILE) | ||
| file_config = load_json(json_file) | ||
|
|
||
| if config: | ||
| server_config = config | ||
| host_and_port = ( | ||
| f"{server_config.pop(CONF_HOST)}:{server_config.pop(CONF_PORT)}" | ||
| ) | ||
| if MP_DOMAIN in server_config: | ||
| hass.data[PLEX_MEDIA_PLAYER_OPTIONS] = server_config.pop(MP_DOMAIN) | ||
| elif file_config: | ||
| _LOGGER.debug("Loading config from %s", json_file) | ||
| host_and_port, server_config = file_config.popitem() | ||
| server_config[CONF_VERIFY_SSL] = server_config.pop("verify") | ||
| elif discovery_info: | ||
| server_config = {} | ||
| host_and_port = f"{discovery_info[CONF_HOST]}:{discovery_info[CONF_PORT]}" | ||
| elif configurator_info: | ||
| server_config = configurator_info | ||
| host_and_port = server_config["host_and_port"] | ||
| else: | ||
| discovery.listen(hass, SERVICE_PLEX, server_discovered) | ||
|
MartinHjelmare marked this conversation as resolved.
|
||
| return True | ||
|
|
||
| use_ssl = server_config.get(CONF_SSL, DEFAULT_SSL) | ||
| http_prefix = "https" if use_ssl else "http" | ||
| server_config[CONF_URL] = f"{http_prefix}://{host_and_port}" | ||
|
|
||
| plex_server = PlexServer(server_config) | ||
| try: | ||
| plex_server.connect() | ||
| except requests.exceptions.ConnectionError as error: | ||
| _LOGGER.error( | ||
| "Plex server could not be reached, please verify host and port: [%s]", | ||
| error, | ||
| ) | ||
| return False | ||
| except ( | ||
| plexapi.exceptions.BadRequest, | ||
| plexapi.exceptions.Unauthorized, | ||
| plexapi.exceptions.NotFound, | ||
| ) as error: | ||
| _LOGGER.error( | ||
| "Connection to Plex server failed, please verify token and SSL settings: [%s]", | ||
| error, | ||
| ) | ||
| request_configuration(host_and_port) | ||
| return False | ||
| else: | ||
| hass.data[PLEX_DOMAIN][SERVERS][ | ||
| plex_server.machine_identifier | ||
| ] = plex_server | ||
|
|
||
| if host_and_port in hass.data[PLEX_DOMAIN][CONFIGURING]: | ||
| request_id = hass.data[PLEX_DOMAIN][CONFIGURING].pop(host_and_port) | ||
| configurator = hass.components.configurator | ||
| configurator.request_done(request_id) | ||
| _LOGGER.debug("Discovery configuration done") | ||
| if configurator_info: | ||
| # Write plex.conf if created via discovery/configurator | ||
| save_json( | ||
| hass.config.path(PLEX_CONFIG_FILE), | ||
| { | ||
| host_and_port: { | ||
| CONF_TOKEN: server_config[CONF_TOKEN], | ||
| CONF_SSL: use_ssl, | ||
| "verify": server_config[CONF_VERIFY_SSL], | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| if not hass.data.get(PLEX_MEDIA_PLAYER_OPTIONS): | ||
| hass.data[PLEX_MEDIA_PLAYER_OPTIONS] = MEDIA_PLAYER_SCHEMA({}) | ||
|
|
||
| for platform in PLATFORMS: | ||
| hass.helpers.discovery.load_platform( | ||
| platform, PLEX_DOMAIN, {}, original_config | ||
| ) | ||
|
|
||
| return True | ||
|
|
||
| def request_configuration(host_and_port): | ||
| """Request configuration steps from the user.""" | ||
| configurator = hass.components.configurator | ||
| if host_and_port in hass.data[PLEX_DOMAIN][CONFIGURING]: | ||
| configurator.notify_errors( | ||
| hass.data[PLEX_DOMAIN][CONFIGURING][host_and_port], | ||
| "Failed to register, please try again.", | ||
| ) | ||
| return | ||
|
|
||
| def plex_configuration_callback(data): | ||
| """Handle configuration changes.""" | ||
| config = { | ||
| "host_and_port": host_and_port, | ||
| CONF_TOKEN: data.get("token"), | ||
| CONF_SSL: cv.boolean(data.get("ssl")), | ||
| CONF_VERIFY_SSL: cv.boolean(data.get("verify_ssl")), | ||
| } | ||
| setup_plex(configurator_info=config) | ||
|
|
||
| hass.data[PLEX_DOMAIN][CONFIGURING][ | ||
| host_and_port | ||
| ] = configurator.request_config( | ||
| "Plex Media Server", | ||
| plex_configuration_callback, | ||
| description="Enter the X-Plex-Token", | ||
| entity_picture="/static/images/logo_plex_mediaserver.png", | ||
| submit_caption="Confirm", | ||
| fields=[ | ||
| {"id": "token", "name": "X-Plex-Token", "type": ""}, | ||
| {"id": "ssl", "name": "Use SSL", "type": ""}, | ||
| {"id": "verify_ssl", "name": "Verify SSL", "type": ""}, | ||
| ], | ||
| ) | ||
|
|
||
| # End of inner functions. | ||
|
|
||
| original_config = config | ||
|
|
||
| hass.data.setdefault(PLEX_DOMAIN, {SERVERS: {}, CONFIGURING: {}}) | ||
|
|
||
| if hass.data[PLEX_DOMAIN][SERVERS]: | ||
| _LOGGER.debug("Plex server already configured") | ||
| return False | ||
|
|
||
| plex_config = config.get(PLEX_DOMAIN, {}) | ||
| return setup_plex(config=plex_config) | ||
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
Oops, something went wrong.
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.