aquatix / ns-api

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

TypeError: the JSON object must be str, bytes or bytearray, not NoneType #27

Open Tuumke opened 4 years ago

Tuumke commented 4 years ago

Was requested to create an issue here about the NoneType error i had in HomeAssistant.

The error is starting to shop up more and more. Will look into if we can turn on extra logging

Yeah, it looks like it does not get any data back, and then wants to convert it to json (Python dict), which of course results in a crash (it talks about NoneType, which means data was None). The library can try to catch that, but will likely then throw a custom error of its own, as it is not an answer we expect.

Wed Feb 12 2020 07:30:05 GMT+0100 (Midden-Europese standaardtijd)
Update for sensor.amersfoort_harderwijk fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 461, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 240, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 208, in update
    2,
  File "/usr/local/lib/python3.7/site-packages/ns_api.py", line 932, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.7/site-packages/ns_api.py", line 834, in parse_trips
    obj = json.loads(data)
  File "/usr/local/lib/python3.7/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
aquatix commented 4 years ago

It might be that the NS API is not functioning well because it is overworked. I'll look into getting better feedback. Could you share your sensor config for this one (of course without the API key and such)?

Tuumke commented 4 years ago
platform: nederlandse_spoorwegen
api_key: !secret ns_api
routes:
  - name: Harderwijk-Amersfoort
    from: Hd
    to: Amf
  - name: Amersfoort-Harderwijk
    from: Amf
    to: Hd

And my automation:

id: 'Check NS in de Middag'
alias: 'Check NS in de Middag'
trigger:
  - platform: template
    value_template: "{% if is_state('sensor.amersfoort_harderwijk', 'departure_delay') %}true{% endif %}"
condition:
  - condition: time
    after: '13:00:00'
    before: '18:00:00'
action:
  - service: notify.telegram
    data_template:
      title: "Trein naar Harderwijk heeft vertraging"
      message: |
        Vertrektijd {{ state_attr('sensor.amersfoort_harderwijk', 'departure_time_actual') }}
        vanaf spoor {{ state_attr('sensor.amersfoort_harderwijk', 'departure_platform_actual') }}
        aankomst om {{ state_attr('sensor.amersfoort_harderwijk', 'arrival_time_actual') }}
        Volgende trein vertrekt om {{ state_attr('sensor.amersfoort_harderwijk', 'next') }}
        Going {{ state_attr('sensor.amersfoort_harderwijk', 'going') }}
        departure_delay {{ state_attr('sensor.amersfoort_harderwijk', 'departure_delay') }}
        departure_time_planned {{ state_attr('sensor.amersfoort_harderwijk', 'departure_time_planned') }}
        departure_time_actual {{ state_attr('sensor.amersfoort_harderwijk', 'departure_time_actual') }}
        status {{ state_attr('sensor.amersfoort_harderwijk', 'status') }}"

Not sure which of the 2 makes the checks to the NS api? I guess the sensor? Is there a reason why we have 2 API keys in the portal? Maybe there is an API limit? -edit-

Het gebruik van de NS API’s is gratis, maar er zit wel een limiet op het aantal calls dat uitgevoerd mag worden. Het limiet verschilt per product. NS bepaald dit limiet en dit kan aangepast worden indien dit door NS gewenst is.

aquatix commented 4 years ago

Oy, the typo in 'bepaald' is a nice cringe. As far as I understand HA, the sensor does the actual calls/checking, as it is responsible for setting its boolean to true or false according to whatever you are measuring. Your automation then uses that state to trigger the Telegram notification (or not). I'm curious to how often HA runs the sensor check; if it is run multiple times every minute, you might run out of your API quotum. Maybe you can see it in your history?

aquatix commented 4 years ago

Oh, the API portal has an overview too: https://apiportal.ns.nl/Developer/Analytics

YarmoM commented 4 years ago

I've been getting the same issue lately and it's not related to quotas (have lived without HA for a few days, so usage is sub-any-quota). Shall we set up a custom exception to handle this one?

aquatix commented 8 months ago

I'm currently taking a new look at the library, also because of #34 so I'll implement this exception handler there too.

Thanks for your patience, ns-api hasn't been a real focus for me lately 😅