Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
warrior25 committed Nov 14, 2024
1 parent ceaa1c6 commit 56a9d69
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 1 addition & 3 deletions custom_components/nysse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ async def async_setup_entry(
hass.data[DOMAIN][entry.entry_id] = entry.data

# Forward the setup to the sensor platform.
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
await hass.config_entries.async_forward_entry_setup(entry, "sensor")
entry.async_on_unload(entry.add_update_listener(options_update_listener))
return True

Expand Down
11 changes: 6 additions & 5 deletions custom_components/nysse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _remove_unwanted_departures(self, departures):
)

return departures[: self._max_items]
except (KeyError, OSError) as err:
except (KeyError, TypeError, OSError) as err:
_LOGGER.info(
"%s: Failed to process realtime departures: %s",
self._stop_code,
Expand Down Expand Up @@ -235,7 +235,7 @@ def _data_to_display_format(self, data):
}
formatted_data.append(departure)
return sorted(formatted_data, key=lambda x: x["time_to_station"])
except OSError as err:
except (OSError, ValueError) as err:
_LOGGER.debug("%s: Failed to format data: %s", self._stop_code, err)
return []

Expand Down Expand Up @@ -298,7 +298,7 @@ def state(self) -> str:
"""Return the state of the sensor."""
if len(self._all_data) > 0:
return self._all_data[0]["departure"]
return
return "unknown"

@property
def extra_state_attributes(self):
Expand Down Expand Up @@ -400,9 +400,10 @@ def icon(self) -> str:
@property
def state(self) -> str:
"""Return the state of the sensor."""
if len(self._alerts) > 0:
try:
return len(self._alerts)
return 0
except TypeError:
return 0

@property
def extra_state_attributes(self):
Expand Down

0 comments on commit 56a9d69

Please sign in to comment.