def __init__(self, hass, start, destination, name):
        """Initialize the sensor."""
        from opendata_transport import OpendataTransport

        self.hass = hass
        self._name = name
        self._from = start
        self._to = destination
        self._websession = async_get_clientsession(self.hass)
        self._opendata = OpendataTransport(
            self._from, self._to, self.hass.loop, self._websession)
Esempio n. 2
0
async def async_setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    async_add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the Swiss public transport sensor."""

    name = config.get(CONF_NAME)
    start = config.get(CONF_START)
    destination = config.get(CONF_DESTINATION)

    session = async_get_clientsession(hass)
    opendata = OpendataTransport(start, destination, session)

    try:
        await opendata.async_get_data()
    except OpendataTransportError:
        _LOGGER.error(
            "Check at http://transport.opendata.ch/examples/stationboard.html "
            "if your station names are valid"
        )
        return

    async_add_entities([SwissPublicTransportSensor(opendata, start, destination, name)])
Esempio n. 3
0
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    """Set up the Swiss public transport sensor."""
    from opendata_transport import OpendataTransport, exceptions

    name = config.get(CONF_NAME)
    start = config.get(CONF_START)
    destination = config.get(CONF_DESTINATION)

    session = async_get_clientsession(hass)
    opendata = OpendataTransport(start, destination, hass.loop, session)

    try:
        yield from opendata.async_get_data()
    except exceptions.OpendataTransportError:
        _LOGGER.error(
            "Check at http://transport.opendata.ch/examples/stationboard.html "
            "if your station names are valid")
        return

    async_add_devices(
        [SwissPublicTransportSensor(opendata, start, destination, name)])
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
    """Set up the Swiss public transport sensor."""
    from opendata_transport import OpendataTransport, exceptions

    name = config.get(CONF_NAME)
    start = config.get(CONF_START)
    destination = config.get(CONF_DESTINATION)

    session = async_get_clientsession(hass)
    opendata = OpendataTransport(start, destination, hass.loop, session)

    try:
        yield from opendata.async_get_data()
    except exceptions.OpendataTransportError:
        _LOGGER.error(
            "Check at http://transport.opendata.ch/examples/stationboard.html "
            "if your station names are valid")
        return

    async_add_devices(
        [SwissPublicTransportSensor(opendata, start, destination, name)])
Esempio n. 5
0
async def main():
    async with aiohttp.ClientSession() as session:
        data = OpendataTransport('Bex', 'Vevey', loop, session)
        await data.async_get_data()

        # Print the start and the destination name
        print("Train connections:", data.from_name, "->", data.to_name)

        # Print the next three connections
        print(data.connections)

        # Print the details of the next connection
        print(data.connections[0])
async def main():
    async with aiohttp.ClientSession() as session:
        data = OpendataTransport("Zürich, Blumenfeldstrasse",
                                 "Zürich Oerlikon, Bahnhof", loop, session)
        await data.async_get_data()

        # Print the start and the destination name
        print("Train connections:", data.from_name, "->", data.to_name)

        # Print the next three connections
        print(data.connections)

        # Print the details of the next connection
        print(data.connections[0])
class SwissPublicTransportSensor(Entity):
    """Implementation of an Swiss public transport sensor."""

    def __init__(self, hass, start, destination, name):
        """Initialize the sensor."""
        from opendata_transport import OpendataTransport

        self.hass = hass
        self._name = name
        self._from = start
        self._to = destination
        self._websession = async_get_clientsession(self.hass)
        self._opendata = OpendataTransport(
            self._from, self._to, self.hass.loop, self._websession)

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._opendata.connections[0]['departure'] \
            if self._opendata is not None else None

    @property
    def device_state_attributes(self):
        """Return the state attributes."""
        if self._opendata is None:
            return

        remaining_time = dt_util.parse_datetime(
            self._opendata.connections[0]['departure']) -\
            dt_util.as_local(dt_util.utcnow())

        attr = {
            ATTR_TRAIN_NUMBER: self._opendata.connections[0]['number'],
            ATTR_PLATFORM: self._opendata.connections[0]['platform'],
            ATTR_TRANSFERS: self._opendata.connections[0]['transfers'],
            ATTR_DURATION: self._opendata.connections[0]['duration'],
            ATTR_DEPARTURE_TIME1: self._opendata.connections[1]['departure'],
            ATTR_DEPARTURE_TIME2: self._opendata.connections[2]['departure'],
            ATTR_START: self._opendata.from_name,
            ATTR_TARGET: self._opendata.to_name,
            ATTR_REMAINING_TIME: '{}'.format(remaining_time),
            ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
        }
        return attr

    @property
    def icon(self):
        """Icon to use in the frontend, if any."""
        return ICON

    @asyncio.coroutine
    def async_update(self):
        """Get the latest data from opendata.ch and update the states."""
        from opendata_transport.exceptions import OpendataTransportError

        try:
            yield from self._opendata.async_get_data()
        except OpendataTransportError:
            _LOGGER.error("Unable to retrieve data from transport.opendata.ch")
            self._opendata = None