|
15 | 15 | import async_timeout |
16 | 16 | import voluptuous as vol |
17 | 17 | import homeassistant.helpers.config_validation as cv |
18 | | -from datetime import datetime |
| 18 | +from datetime import datetime, timedelta |
19 | 19 | from homeassistant.components.sensor import PLATFORM_SCHEMA |
20 | 20 | from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SSL |
21 | 21 | from homeassistant.helpers.entity import Entity |
22 | 22 |
|
| 23 | +SCAN_INTERVAL = timedelta(minutes=3) |
23 | 24 | _LOGGER = logging.getLogger(__name__) |
24 | 25 |
|
25 | 26 |
|
26 | 27 | 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 |
36 | 40 |
|
37 | 41 |
|
38 | 42 | async def request(url, self, content=False, ssl=False): |
@@ -234,6 +238,9 @@ async def async_update(self): |
234 | 238 | sections = [] |
235 | 239 | try: |
236 | 240 | libraries = await request(all_libraries, self) |
| 241 | + if not libraries: |
| 242 | + self._state = '%s cannot be reached' % self.server_ip |
| 243 | + return |
237 | 244 | libraries = json.loads(libraries) |
238 | 245 | for lib_section in libraries['MediaContainer']['Directory']: |
239 | 246 | if lib_section['type'] in self.sections: |
|
0 commit comments