London Underground sensor

@robmarkcole (because I think you wrote the component?)

Is anyone else having problems with this?
I just want to check whether it is just me. My log was flooded with errors from this component last night so I restarted and now it won’t even set up the platform (I particularly like the KeyError at the bottom!)

2018-12-29 09:41:00 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform london_underground
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/local/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/london_underground.py", line 49, in setup_platform
    data.update()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/util/__init__.py", line 324, in wrapper
    result = method(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/london_underground.py", line 113, in update
    self.data = parse_api_response(response.json())
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/london_underground.py", line 130, in parse_api_response
    [status['reason'] for status in line['lineStatuses']])
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/london_underground.py", line 130, in <listcomp>
    [status['reason'] for status in line['lineStatuses']])
KeyError: 'reason'

Hi, which line gave the error? Thanks

All of them through last night.

EDIT: I took your question to mean that maybe it wasn’t a global problem so I did a another restart but got the same, Error while setting up platform london_underground

OK I created an issue

https://github.com/home-assistant/home-assistant/issues/19639

UPDATE: TFL changed their API from what I can tell.
To fix, in london_underground.py replace the current function with:

def parse_api_response(response):
    """Take in the TFL API json response."""
    lines = [line['name'] for line in response]
    data_dict = dict.fromkeys(lines)

    for line in response:

        statuses = [status['statusSeverityDescription']
                    for status in line['lineStatuses']]
        state = ' + '.join(sorted(set(statuses)))

        if state == 'Good Service':   # if good status, this is the only status returned
            reason = 'Nothing to report'
        else:
            reason = ' *** '.join([status['disruption']['additionalInfo']
                                    for status in line['lineStatuses']])
            reason = reason.replace('\r\n', ' ')

        attr = {'State': state, 'Description': reason}
        data_dict[line['name']] = attr

    return data_dict

Thanks.

Can I do this if I am using hassio?
I can’t find

/usr/local/lib/python3.6/site-packages/homeassistant/components/sensor/london_underground.py.

I have a path to deps/lib/python3.6/site-packages/ but no homeassistant folder in there

Ah I’m not sure on hassio. You can just put the edited file in custom_components/sensor/london_underground.py

I created a PR https://github.com/home-assistant/home-assistant/pull/19642

1 Like