Skip to content

Commit 67def37

Browse files
committed
keep attributes on disconnect, increase scan interval
1 parent 2d0b00b commit 67def37

File tree

1 file changed

+17
-10
lines changed
  • custom_components/plex_recently_added

1 file changed

+17
-10
lines changed

custom_components/plex_recently_added/sensor.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@
1515
import async_timeout
1616
import voluptuous as vol
1717
import homeassistant.helpers.config_validation as cv
18-
from datetime import datetime
18+
from datetime import datetime, timedelta
1919
from homeassistant.components.sensor import PLATFORM_SCHEMA
2020
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SSL
2121
from homeassistant.helpers.entity import Entity
2222

23+
SCAN_INTERVAL = timedelta(minutes=3)
2324
_LOGGER = logging.getLogger(__name__)
2425

2526

2627
async def fetch(session, url, self, ssl, content):
27-
with async_timeout.timeout(10):
28-
async with session.get(
29-
url, ssl=ssl, headers={
30-
"Accept": "application/json", "X-Plex-Token": self.token}
31-
) as response:
32-
if content:
33-
return await response.content.read()
34-
else:
35-
return await response.text()
28+
try:
29+
with async_timeout.timeout(8):
30+
async with session.get(
31+
url, ssl=ssl, headers={
32+
"Accept": "application/json", "X-Plex-Token": self.token}
33+
) as response:
34+
if content:
35+
return await response.content.read()
36+
else:
37+
return await response.text()
38+
except:
39+
pass
3640

3741

3842
async def request(url, self, content=False, ssl=False):
@@ -234,6 +238,9 @@ async def async_update(self):
234238
sections = []
235239
try:
236240
libraries = await request(all_libraries, self)
241+
if not libraries:
242+
self._state = '%s cannot be reached' % self.server_ip
243+
return
237244
libraries = json.loads(libraries)
238245
for lib_section in libraries['MediaContainer']['Directory']:
239246
if lib_section['type'] in self.sections:

0 commit comments

Comments
 (0)