Skip to content

Commit

Permalink
keep attributes on disconnect, increase scan interval
Browse files Browse the repository at this point in the history
  • Loading branch information
maykar committed Jun 22, 2020
1 parent 2d0b00b commit 67def37
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions custom_components/plex_recently_added/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@
import async_timeout
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from datetime import datetime
from datetime import datetime, timedelta
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SSL
from homeassistant.helpers.entity import Entity

SCAN_INTERVAL = timedelta(minutes=3)
_LOGGER = logging.getLogger(__name__)


async def fetch(session, url, self, ssl, content):
with async_timeout.timeout(10):
async with session.get(
url, ssl=ssl, headers={
"Accept": "application/json", "X-Plex-Token": self.token}
) as response:
if content:
return await response.content.read()
else:
return await response.text()
try:
with async_timeout.timeout(8):
async with session.get(
url, ssl=ssl, headers={
"Accept": "application/json", "X-Plex-Token": self.token}
) as response:
if content:
return await response.content.read()
else:
return await response.text()
except:
pass


async def request(url, self, content=False, ssl=False):
Expand Down Expand Up @@ -234,6 +238,9 @@ async def async_update(self):
sections = []
try:
libraries = await request(all_libraries, self)
if not libraries:
self._state = '%s cannot be reached' % self.server_ip
return
libraries = json.loads(libraries)
for lib_section in libraries['MediaContainer']['Directory']:
if lib_section['type'] in self.sections:
Expand Down

0 comments on commit 67def37

Please sign in to comment.