Skip to content

Commit 56a9d69

Browse files
committed
Improve error handling
1 parent ceaa1c6 commit 56a9d69

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

custom_components/nysse/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ async def async_setup_entry(
1515
hass.data[DOMAIN][entry.entry_id] = entry.data
1616

1717
# Forward the setup to the sensor platform.
18-
hass.async_create_task(
19-
hass.config_entries.async_forward_entry_setup(entry, "sensor")
20-
)
18+
await hass.config_entries.async_forward_entry_setup(entry, "sensor")
2119
entry.async_on_unload(entry.add_update_listener(options_update_listener))
2220
return True
2321

custom_components/nysse/sensor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _remove_unwanted_departures(self, departures):
105105
)
106106

107107
return departures[: self._max_items]
108-
except (KeyError, OSError) as err:
108+
except (KeyError, TypeError, OSError) as err:
109109
_LOGGER.info(
110110
"%s: Failed to process realtime departures: %s",
111111
self._stop_code,
@@ -235,7 +235,7 @@ def _data_to_display_format(self, data):
235235
}
236236
formatted_data.append(departure)
237237
return sorted(formatted_data, key=lambda x: x["time_to_station"])
238-
except OSError as err:
238+
except (OSError, ValueError) as err:
239239
_LOGGER.debug("%s: Failed to format data: %s", self._stop_code, err)
240240
return []
241241

@@ -298,7 +298,7 @@ def state(self) -> str:
298298
"""Return the state of the sensor."""
299299
if len(self._all_data) > 0:
300300
return self._all_data[0]["departure"]
301-
return
301+
return "unknown"
302302

303303
@property
304304
def extra_state_attributes(self):
@@ -400,9 +400,10 @@ def icon(self) -> str:
400400
@property
401401
def state(self) -> str:
402402
"""Return the state of the sensor."""
403-
if len(self._alerts) > 0:
403+
try:
404404
return len(self._alerts)
405-
return 0
405+
except TypeError:
406+
return 0
406407

407408
@property
408409
def extra_state_attributes(self):

0 commit comments

Comments
 (0)