async def async_get_next_train_stop(self, from_station: StationInfo,
                                        to_station: StationInfo,
                                        after_time: datetime) -> TrainStop:
        """Enable retreival of next departure."""
        date_as_text = after_time.strftime(Trafikverket.date_time_format)

        filters = [
            FieldFilter(FilterOperation.equal, "ActivityType", "Avgang"),
            FieldFilter(FilterOperation.equal, "LocationSignature",
                        from_station.signature),
            FieldFilter(FilterOperation.greater_than_equal,
                        "AdvertisedTimeAtLocation", date_as_text),
            OrFilter([
                FieldFilter(FilterOperation.equal,
                            "ViaToLocation.LocationName",
                            to_station.signature),
                FieldFilter(FilterOperation.equal, "ToLocation.LocationName",
                            to_station.signature)
            ])
        ]

        sorting = [FieldSort("AdvertisedTimeAtLocation", SortOrder.ascending)]
        train_announcements = await self._api.async_make_request(
            "TrainAnnouncement", TrainStop._required_fields, filters, 1,
            sorting)

        if len(train_announcements) == 0:
            raise ValueError("No TrainAnnouncement found")

        if len(train_announcements) > 1:
            raise ValueError("Multiple TrainAnnouncements found")

        train_announcement = train_announcements[0]

        return TrainStop.from_xml_node(train_announcement)
    async def async_get_train_stop(self, from_station: StationInfo,
                                   to_station: StationInfo,
                                   time_at_location: datetime) -> TrainStop:
        """Retrieve the train stop."""
        date_as_text = time_at_location.strftime(Trafikverket.date_time_format)

        filters = [
            FieldFilter(FilterOperation.equal, "ActivityType", "Avgang"),
            FieldFilter(FilterOperation.equal, "LocationSignature",
                        from_station.signature),
            FieldFilter(FilterOperation.equal, "AdvertisedTimeAtLocation",
                        date_as_text),
            OrFilter([
                FieldFilter(FilterOperation.equal,
                            "ViaToLocation.LocationName",
                            to_station.signature),
                FieldFilter(FilterOperation.equal, "ToLocation.LocationName",
                            to_station.signature)
            ])
        ]

        train_announcements = await self._api.async_make_request(
            "TrainAnnouncement", TrainStop._required_fields, filters)

        if len(train_announcements) == 0:
            raise ValueError("No TrainAnnouncement found")

        if len(train_announcements) > 1:
            raise ValueError("Multiple TrainAnnouncements found")

        train_announcement = train_announcements[0]
        return TrainStop.from_xml_node(train_announcement)
    async def async_get_train_station(self, location_name: str) -> StationInfo:
        """Retreive train station id based on name."""
        train_stations = await self._api.async_make_request(
            "TrainStation", StationInfo._required_fields, [
                FieldFilter(FilterOperation.equal, "AdvertisedLocationName",
                            location_name),
                FieldFilter(FilterOperation.equal, "Advertised", "true")
            ])
        if len(train_stations) == 0:
            raise ValueError(
                "Could not find a station with the specified name")
        if len(train_stations) > 1:
            raise ValueError("Found multiple stations with the specified name")

        return StationInfo.from_xml_node(train_stations[0])
Beispiel #4
0
    async def async_get_weather(self,
                                location_name: str) -> WeatherStationInfo:
        weather_stations = await self._api.async_make_request(
            "WeatherStation", WeatherStationInfo.required_fields,
            [FieldFilter(FilterOperation.equal, "Name", location_name)])
        if len(weather_stations) == 0:
            raise ValueError(
                "Could not find a weather station with the specified name")
        if len(weather_stations) > 1:
            raise ValueError(
                "Found multiple weather stations with the specified name")

        return WeatherStationInfo.from_xml_node(weather_stations[0])
    async def async_search_train_stations(
            self, location_name: str) -> typing.List[StationInfo]:
        """Search for train stations."""
        train_stations = await self._api.async_make_request(
            "TrainStation", [
                "AdvertisedLocationName", "LocationSignature", "Advertised",
                "Deleted"
            ], [
                FieldFilter(FilterOperation.like, "AdvertisedLocationName",
                            location_name),
                FieldFilter(FilterOperation.equal, "Advertised", "true")
            ])
        if len(train_stations) == 0:
            raise ValueError(
                "Could not find a station with the specified name")

        result = [StationInfo] * 0

        for train_station in train_stations:
            result.append(StationInfo.from_xml_node(train_station))

        return result