Fix NMBS AttributeError (#90525)
* Fix NMBS AttributeError (Issue #90505) * Set and use API_FAILURE * Configure the logger to track API failures * Remove broad exceptions and rewite logging
This commit is contained in:
parent
9705607db4
commit
06e36bcff5
1 changed files with 23 additions and 12 deletions
|
@ -22,6 +22,8 @@ import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
API_FAILURE = -1
|
||||||
|
|
||||||
DEFAULT_NAME = "NMBS"
|
DEFAULT_NAME = "NMBS"
|
||||||
|
|
||||||
DEFAULT_ICON = "mdi:train"
|
DEFAULT_ICON = "mdi:train"
|
||||||
|
@ -162,16 +164,19 @@ class NMBSLiveBoard(SensorEntity):
|
||||||
"""Set the state equal to the next departure."""
|
"""Set the state equal to the next departure."""
|
||||||
liveboard = self._api_client.get_liveboard(self._station)
|
liveboard = self._api_client.get_liveboard(self._station)
|
||||||
|
|
||||||
if (
|
if liveboard == API_FAILURE:
|
||||||
liveboard is None
|
_LOGGER.warning("API failed in NMBSLiveBoard")
|
||||||
or liveboard.get("departures") is None
|
|
||||||
or liveboard.get("departures").get("number") is None
|
|
||||||
or liveboard.get("departures").get("number") == "0"
|
|
||||||
or liveboard.get("departures").get("departure") is None
|
|
||||||
):
|
|
||||||
return
|
return
|
||||||
|
|
||||||
next_departure = liveboard["departures"]["departure"][0]
|
if not (departures := liveboard.get("departures")):
|
||||||
|
_LOGGER.warning("API returned invalid departures: %r", liveboard)
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug("API returned departures: %r", departures)
|
||||||
|
if departures["number"] == "0":
|
||||||
|
# No trains are scheduled
|
||||||
|
return
|
||||||
|
next_departure = departures["departure"][0]
|
||||||
|
|
||||||
self._attrs = next_departure
|
self._attrs = next_departure
|
||||||
self._state = (
|
self._state = (
|
||||||
|
@ -290,13 +295,19 @@ class NMBSSensor(SensorEntity):
|
||||||
self._station_from, self._station_to
|
self._station_from, self._station_to
|
||||||
)
|
)
|
||||||
|
|
||||||
if connections is None or not connections.get("connection"):
|
if connections == API_FAILURE:
|
||||||
|
_LOGGER.warning("API failed in NMBSSensor")
|
||||||
return
|
return
|
||||||
|
|
||||||
if int(connections["connection"][0]["departure"]["left"]) > 0:
|
if not (connection := connections.get("connection")):
|
||||||
next_connection = connections["connection"][1]
|
_LOGGER.warning("API returned invalid connection: %r", connections)
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug("API returned connection: %r", connection)
|
||||||
|
if int(connection[0]["departure"]["left"]) > 0:
|
||||||
|
next_connection = connection[1]
|
||||||
else:
|
else:
|
||||||
next_connection = connections["connection"][0]
|
next_connection = connection[0]
|
||||||
|
|
||||||
self._attrs = next_connection
|
self._attrs = next_connection
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue