"""
homeassistant.components.sensor.swiss_public_transport
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Swiss public transport sensor will give you the next two departure times
from a given location to another one. This sensor is limited to Switzerland.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.swiss_public_transport.html
"""
import logging
from datetime import timedelta
import requests

from homeassistant.util import Throttle
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity

_LOGGER = logging.getLogger(__name__)
_RESOURCE = 'http://transport.opendata.ch/v1/'

# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)


def setup_platform(hass, config, add_devices, discovery_info=None):
    """ Get the Swiss public transport sensor. """

    # journal contains [0] Station ID start, [1] Station ID destination
    # [2] Station name start, and [3] Station name destination
    journey = [config.get('from'), config.get('to')]
    try:
        for location in [config.get('from', None), config.get('to', None)]:
            # transport.opendata.ch doesn't play nice with requests.Session
            result = requests.get(_RESOURCE + 'locations?query=%s' % location,
                                  timeout=10)
            journey.append(result.json()['stations'][0]['name'])
    except KeyError:
        _LOGGER.exception(
            "Unable to determine stations. "
            "Check your settings and/or the availability of opendata.ch")

        return None

    dev = []
    data = PublicTransportData(journey)
    dev.append(SwissPublicTransportSensor(data, journey))
    add_devices(dev)


# pylint: disable=too-few-public-methods
class SwissPublicTransportSensor(Entity):
    """ Implements an Swiss public transport sensor. """

    def __init__(self, data, journey):
        self.data = data
        self._name = '{}-{}'.format(journey[2], journey[3])
        self.update()

    @property
    def name(self):
        """ Returns the name. """
        return self._name

    @property
    def state(self):
        """ Returns the state of the device. """
        return self._state

    # pylint: disable=too-many-branches
    def update(self):
        """ Gets the latest data from opendata.ch and updates the states. """
        times = self.data.update()
        try:
            self._state = ', '.join(times)
        except TypeError:
            pass


# pylint: disable=too-few-public-methods
class PublicTransportData(object):
    """ Class for handling the data retrieval. """

    def __init__(self, journey):
        self.start = journey[0]
        self.destination = journey[1]

    @Throttle(MIN_TIME_BETWEEN_UPDATES)
    def update(self):
        """ Gets the latest data from opendata.ch. """

        response = requests.get(
            _RESOURCE +
            'connections?' +
            'from=' + self.start + '&' +
            'to=' + self.destination + '&' +
            'fields[]=connections/from/departureTimestamp/&' +
            'fields[]=connections/',
            timeout=10)
        connections = response.json()['connections'][:2]

        try:
            return [
                dt_util.datetime_to_time_str(
                    dt_util.as_local(dt_util.utc_from_timestamp(
                        item['from']['departureTimestamp']))
                )
                for item in connections
            ]
        except KeyError:
            return ['n/a']

@@ -1633,17 +1633,16 @@
 ta.ch%22)%0A
-%0A
         
@@ -1648,19 +1648,20 @@
  return 
-Non
+Fals
 e%0A%0A    d

Fix return value