77https://github.com/custom-cards/upcoming-media-card
88
99"""
10- import json
11- import logging
12- import math
13- import os
1410import 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-
11+ import logging
12+ import json
2213import requests
23-
24- import homeassistant .helpers .config_validation as cv
2514import voluptuous as vol
15+ import homeassistant .helpers .config_validation as cv
16+ from datetime import datetime
2617from homeassistant .components .sensor import PLATFORM_SCHEMA
2718from homeassistant .const import CONF_HOST , CONF_PORT , CONF_SSL
2819from homeassistant .helpers .entity import Entity
29- from pytz import timezone , utc
3020
3121__version__ = '0.1.5'
3222
@@ -60,6 +50,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
6050class PlexRecentlyAddedSensor (Entity ):
6151
6252 def __init__ (self , hass , conf ):
53+ from pytz import timezone
6354 self .conf_dir = str (hass .config .path ()) + '/'
6455 self ._dir = conf .get (CONF_IMG_CACHE )
6556 self .img = '{0}{1}{2}{3}{4}.jpg' .format (
@@ -97,6 +88,7 @@ def state(self):
9788
9889 @property
9990 def device_state_attributes (self ):
91+ import math
10092 attributes = {}
10193 if self .change_detected :
10294 self .card_json = []
@@ -186,6 +178,8 @@ def device_state_attributes(self):
186178 return attributes
187179
188180 def update (self ):
181+ import re
182+ import os
189183 plex = requests .Session ()
190184 if not self .cert :
191185 """Default SSL certificate is for plex.tv not our api server"""
@@ -283,6 +277,7 @@ def update(self):
283277
284278def image_url (url_elements , cert_check , img ):
285279 """Plex can resize images with a long & partially % encoded url."""
280+ from urllib .parse import quote
286281 ssl , host , local , port , token , self_cert , dl_images = url_elements
287282 if not cert_check and not self_cert :
288283 ssl = ''
@@ -306,6 +301,8 @@ def image_url(url_elements, cert_check, img):
306301
307302def get_server_ip (name , token ):
308303 """With a token and server name we get server's ip, local ip, and port"""
304+ import xml .etree .ElementTree as ET
305+ from unicodedata import normalize
309306 plex_tv = requests .get (
310307 'https://plex.tv/api/servers.xml?X-Plex-Token=' + token , timeout = 10 )
311308 plex_xml = ET .fromstring (plex_tv .content )
@@ -318,6 +315,8 @@ def get_server_ip(name, token):
318315
319316
320317def days_since (date , tz ):
318+ import time
319+ from pytz import utc
321320 date = datetime .utcfromtimestamp (date ).isoformat () + 'Z'
322321 date = datetime .strptime (date , '%Y-%m-%dT%H:%M:%SZ' )
323322 date = str (date .replace (tzinfo = utc ).astimezone (tz ))[:10 ]
0 commit comments