-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
87 lines (72 loc) · 2.76 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""An ICE Portal API consumer."""
import asyncio
import logging
import socket
from datetime import datetime
import httpx
from . import exceptions
_LOGGER = logging.getLogger(__name__)
_PORTAL = "https://iceportal.de/api1/rs/tripInfo/trip"
_STATUS = "https://iceportal.de/api1/rs/status"
class IcePortal:
"""A class for handling connections with the ICE Portal."""
def __init__(self):
"""Initialize the connection the ICE Portal."""
self.data = {}
self._track = self._arrival_time = None
self.status = {}
async def get_data(self):
"""Get details of the current trip."""
try:
async with httpx.AsyncClient() as client:
response = await client.get(str(_PORTAL))
except httpx.ConnectError:
raise exceptions.IcePortalConnectionError(f"Connection to {_PORTAL} failed")
if response.status_code == httpx.codes.OK:
try:
_LOGGER.debug(response.json())
self.data = response.json()
except TypeError:
_LOGGER.error("Can not load data from the ICE portal")
raise exceptions.IcePortalError(
"Unable to get the data from the ICE portal"
)
try:
async with httpx.AsyncClient() as client:
response = await client.get(str(_STATUS))
except httpx.ConnectError:
raise exceptions.IcePortalConnectionError(f"Connection to {_STATUS} failed")
if response.status_code == httpx.codes.OK:
_LOGGER.debug(response.json())
try:
self.status = response.json()
except TypeError:
_LOGGER.error("Can not load data from the ICE portal")
raise exceptions.IcePortalError(
"Unable to get the data from the ICE portal"
)
@property
def train(self):
"""The ID of the train."""
return f"{self.data['trip']['trainType']}{self.data['trip']['vzn']}"
@property
def next_stop(self):
"""The next stop."""
stops = self.data["trip"]["stops"]
for stop in stops:
if stop["info"]["passed"] is False:
self._track = stop["track"]["actual"]
self._arrival_time = str(stop["timetable"]["actualArrivalTime"])[:-3]
return stop["station"]["name"]
@property
def track(self):
"""The track of the next station."""
return self._track
@property
def arrival_time(self):
"""The arrival time at the next station."""
return datetime.utcfromtimestamp(int(self._arrival_time))
@property
def train_speed(self):
"""The current speed of the trains."""
return self.status["speed"]