aquatix / ns-api

Query the Dutch railways about your routes
MIT License
11 stars 9 forks source link

AttributeError: 'TripStop' object has no attribute 'planned_platform' in Home Assistant #31

Closed Mariusthvdb closed 3 years ago

Mariusthvdb commented 3 years ago

using Nederlandse Spoorwegen in Home Assistant errors on

2021-04-14 10:04:33 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.tilburg_roosendaal fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 292, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 490, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 278, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'

Discord chat with HA dev's lead to open an issue here in the Library more info please see: https://github.com/home-assistant/core/issues/50057 thanks for having a look

aquatix commented 3 years ago

Hm, will take a look indeed. Might be after the weekend, as I'm out of town. Thanks for notifying me!

aquatix commented 3 years ago

Hm, the library seems fine, but the Home Assistant integration seems to rely on the planned_platform property to be there. @YarmoM maybe you can write a fix for that, as you did a lot of the integration work? Please let me know, and thanks in advance!

aquatix commented 3 years ago

Never mind, I need coffee. Currently fixing in the library, bear with me throught the noise ;)

aquatix commented 3 years ago

Uploaded https://pypi.org/project/nsapi/3.0.5/ with the fix. @YarmoM maybe you can test and submit a PR with the Home Assistant project? Much obliged :)

Mariusthvdb commented 3 years ago

thanks for working on this so quickly! much appreciated. did you also see the linked issue with error:

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

?

aquatix commented 3 years ago

Not sure if we can do a lot about that, apparently the NS API didn't return a json response, so the library couldn't even start to parse the answer. This could be caught of course, but it really is an exception, as it should provide an answer normally, so we want to know about it.

Is this a new issue on that route? Are you still encountering it?

Mariusthvdb commented 3 years ago

no this is not new, and has been in HA for a very long time now, and yes, always happening... btw, it isn't on only that route, suspect it to be on any route, see below.

I do feel a guard in the component is best. We can now filter errors in the logger, but rather than cloak them, which is what that does of course, it would be better to update the code to handle these exceptions properly?

021-05-19 08:47:14 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.roosendaal_rotterdam fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 316, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 524, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 280, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'
2021-05-19 08:47:15 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.roosendaal_tilburg fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 316, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 524, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 280, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'
2021-05-19 08:47:44 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.roosendaal_rotterdam fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 316, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 524, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 280, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'
2021-05-19 08:47:44 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.roosendaal_tilburg fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 316, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 524, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 280, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'
2021-05-19 08:48:14 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.roosendaal_rotterdam fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 316, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 524, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 280, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'
2021-05-19 08:48:15 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.roosendaal_tilburg fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 316, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 524, in async_device_update
    raise exc
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 280, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 235, in update
    self._trips = self._nsapi.get_trips(
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 927, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 838, in parse_trips
    newtrip = Trip(trip, requested_time)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 607, in __init__
    trip_part = TripSubpart(part)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 455, in __init__
    stop = TripStop(raw_stop)
  File "/usr/local/lib/python3.8/site-packages/ns_api.py", line 412, in __init__
    self.platform_changed = bool(self.actual_platform != self.planned_platform)
AttributeError: 'TripStop' object has no attribute 'planned_platform'
Mariusthvdb commented 3 years ago

Hm, the library seems fine, but the Home Assistant integration seems to rely on the planned_platform property to be there. @YarmoM maybe you can write a fix for that, as you did a lot of the integration work? Please let me know, and thanks in advance!

please let me get back on this, and thanks for your time in advance ;-) did you fix the library, so it doesn't need YarmoM to fix the 'planned_platform' property any longer, or should that still be a separate PR? Asking because Yarmo hasn't responded, and I am trying to get the bump done in Ha, but need someone to test that first.... preferably the code owner as you can understand.

aquatix commented 3 years ago

Even if I fix the library, that would need a version bump of the requirement in the integration, so someone has to change it in HA.

I do not mind filling the value in the library, but guarding the property in the HA integration is a good practice too. Might take a look at the HA integration myself, as @YarmoM indeed seems to enjoy life offline :) Or are you @Mariusthvdb doing work on the integration already?

Currently, I do not use the library and the HA integration myself because of me working from home. I do use HA though, and would like my library to work correctly in any case. What are you proposing, @Mariusthvdb ?

Mariusthvdb commented 3 years ago

we did that yesterday, and the bump is merged at the moment, see https://github.com/home-assistant/core/pull/53620

havent checked guarding the property in HA yet,, maybe we could have a look together, now this bump is merged?

aquatix commented 3 years ago

Heh, I forgot I already fixed my library :)

I am checking https://github.com/home-assistant/core/blob/dev/homeassistant/components/nederlandse_spoorwegen/sensor.py but I do not see references to this property anyway, so it was purely an internal library crash. Your already merged PR bumping ns-api to the latest version will fix that.

To make the sensor more robust, in the except part starting on line 245 in the above sensor.py, a line catching the AttributeError can be added:

        except (
            requests.exceptions.ConnectionError,
            requests.exceptions.HTTPError,
            AttributeError,
        ) as error:
            _LOGGER.error("Couldn't fetch trip info: %s", error)
Mariusthvdb commented 3 years ago

thanks, and ok, let me do that, Ill link to this discussion.

Mariusthvdb commented 3 years ago

so you've seen the cancellation of this PR in HA. guess I can close this then, as you've fixed the ns-api and we'll wait and see what happens in the HA logs after that ok? thanks for your support and action!

aquatix commented 3 years ago

Sure thing; I'm unsure about how crash-bugs get notified in HA btw, maybe there's a better mechanism in place than having those exceptions caught and logged (probably you just get a nice notification 'hey, the nederlandse spoorwegen integration crashed'). Anyway, looking forward to the next HA version with the fixed ns-api version :)

Thanks for your time, thoughts and work.