Skip to content

Commit e4b5d34

Browse files
committed
Gracefully set not connected status if unconnected
1 parent fc0ef03 commit e4b5d34

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

custom_components/ultimaker/sensor.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,23 @@ def __init__(self, session, host):
101101
async def async_update(self):
102102
"""Download and update data from the Ultimaker Printer"""
103103
if self._host:
104-
self._data = await self.fetch_data(self._url_printer)
105-
self._data |= await self.fetch_data(self._url_print_job)
106-
self._data |= await self.fetch_data(self._url_system)
104+
try:
105+
self._data = await self.fetch_data(self._url_printer)
106+
self._data |= await self.fetch_data(self._url_print_job)
107+
self._data |= await self.fetch_data(self._url_system)
108+
except aiohttp.ClientConnectorError:
109+
self._data = {'status': 'not connected'}
107110
self._data['sampleTime'] = datetime.now()
108111

109112
async def fetch_data(self, url):
110113
try:
111114
with async_timeout.timeout(5):
112115
response = await self._session.get(url)
113-
except aiohttp.ClientError:
114-
_LOGGER.error(f"Cannot poll Ultimaker printer using url: {url}")
116+
except aiohttp.ClientConnectorError as err:
117+
_LOGGER.warning(f"Printer {self._host} is offline")
118+
raise err
119+
except aiohttp.ClientError as err:
120+
_LOGGER.error(f"Cannot poll Ultimaker printer using url: {url} error {err}")
115121
except asyncio.TimeoutError:
116122
_LOGGER.error(f" Timeout error occurred while polling ultimaker printer using url {url}")
117123
except Exception as err:

0 commit comments

Comments
 (0)