Skip to content

Commit 6682e82

Browse files
authored
Merge pull request #4 from tmcarr/master
Make image download dir configurable and clean up imports
2 parents b754178 + 17fce13 commit 6682e82

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This component does not require, nor conflict with, the default Plex components.
2626
| ssl | false | no | Set to true if you use SSL to access Plex.
2727
| max | 5 | no | Max number of items to show in sensor.
2828
| download_images | true | no | Setting this to false will turn off downloading of images, but will require certain Plex settings to work. See below.
29+
| img_dir | '/custom-lovelace/upcoming-media-card/images/plex/' | no | This option allows you to choose a custom directory to store images in if you enable download_images.
2930
| ssl_cert | false | no | If you provide your own SSL certificate in Plex's network settings set this to true.
3031

3132
#### By default this addon automatically downloads images from Plex to your /www/custom-lovelace/upcoming-media-card/ directory. The directory is automatically created & only images reported in the upcoming list are downloaded. Images are small in size and are removed automatically when no longer needed. Currently & unfortunately, this may not work on all systems.

custom_components/sensor/plex_recently_added.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,26 @@
77
https://github.com/custom-cards/upcoming-media-card
88
99
"""
10-
import os.path
11-
import logging
1210
import json
11+
import logging
12+
import math
13+
import os
14+
import os.path
15+
import re
16+
import time
17+
import xml.etree.ElementTree as ET
18+
from datetime import datetime
19+
from unicodedata import normalize
20+
from urllib.parse import quote
21+
1322
import requests
14-
import voluptuous as vol
23+
1524
import homeassistant.helpers.config_validation as cv
16-
from datetime import datetime
25+
import voluptuous as vol
1726
from homeassistant.components.sensor import PLATFORM_SCHEMA
1827
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL
1928
from homeassistant.helpers.entity import Entity
29+
from pytz import timezone, utc
2030

2131
__version__ = '0.1.6'
2232

@@ -27,6 +37,7 @@
2737
CONF_SSL_CERT = 'ssl_cert'
2838
CONF_TOKEN = 'token'
2939
CONF_MAX = 'max'
40+
CONF_IMG_CACHE = 'img_dir'
3041

3142
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
3243
vol.Optional(CONF_SSL, default=False): cv.boolean,
@@ -37,6 +48,8 @@
3748
vol.Optional(CONF_DL_IMAGES, default=True): cv.boolean,
3849
vol.Optional(CONF_HOST, default='localhost'): cv.string,
3950
vol.Optional(CONF_PORT, default=32400): cv.port,
51+
vol.Optional(CONF_IMG_CACHE,
52+
default='/custom-lovelace/upcoming-media-card/images/plex/'): cv.string
4053
})
4154

4255

@@ -47,9 +60,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
4760
class PlexRecentlyAddedSensor(Entity):
4861

4962
def __init__(self, hass, conf):
50-
from pytz import timezone
5163
self.conf_dir = str(hass.config.path()) + '/'
52-
self._dir = '/custom-lovelace/upcoming-media-card/images/plex/'
64+
self._dir = conf.get(CONF_IMG_CACHE)
5365
self.img = '{0}{1}{2}{3}{4}.jpg'.format(
5466
self.conf_dir, {}, self._dir, {}, {})
5567
self._tz = timezone(str(hass.config.time_zone))
@@ -84,7 +96,6 @@ def state(self):
8496

8597
@property
8698
def device_state_attributes(self):
87-
import math
8899
attributes = {}
89100
if self.change_detected:
90101
self.card_json = []
@@ -174,8 +185,6 @@ def device_state_attributes(self):
174185
return attributes
175186

176187
def update(self):
177-
import re
178-
import os
179188
plex = requests.Session()
180189
if not self.cert:
181190
"""Default SSL certificate is for plex.tv not our api server"""
@@ -282,7 +291,6 @@ def update(self):
282291

283292
def image_url(url_elements, cert_check, img):
284293
"""Plex can resize images with a long & partially % encoded url."""
285-
from urllib.parse import quote
286294
ssl, host, local, port, token, self_cert, dl_images = url_elements
287295
if not cert_check and not self_cert:
288296
ssl = ''
@@ -306,8 +314,6 @@ def image_url(url_elements, cert_check, img):
306314

307315
def get_server_ip(name, token):
308316
"""With a token and server name we get server's ip, local ip, and port"""
309-
import xml.etree.ElementTree as ET
310-
from unicodedata import normalize
311317
plex_tv = requests.get(
312318
'https://plex.tv/api/servers.xml?X-Plex-Token=' + token, timeout=10)
313319
plex_xml = ET.fromstring(plex_tv.content)
@@ -320,8 +326,6 @@ def get_server_ip(name, token):
320326

321327

322328
def days_since(date, tz):
323-
import time
324-
from pytz import utc
325329
date = datetime.utcfromtimestamp(date).isoformat() + 'Z'
326330
date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
327331
date = str(date.replace(tzinfo=utc).astimezone(tz))[:10]

0 commit comments

Comments
 (0)