26
26
import voluptuous as vol
27
27
28
28
from homeassistant .config_entries import ConfigEntry
29
- from homeassistant .const import UnitOfLength
29
+ from homeassistant .const import CONF_MODE , UnitOfLength
30
30
from homeassistant .core import HomeAssistant
31
31
from homeassistant .helpers import config_validation as cv
32
32
from homeassistant .helpers .location import find_coordinates
33
33
from homeassistant .helpers .update_coordinator import DataUpdateCoordinator , UpdateFailed
34
34
from homeassistant .util import dt as dt_util
35
35
from homeassistant .util .unit_conversion import DistanceConverter
36
36
37
- from .const import DEFAULT_SCAN_INTERVAL , DOMAIN , ROUTE_MODE_FASTEST
38
- from .model import HERETravelTimeConfig , HERETravelTimeData
37
+ from .const import (
38
+ CONF_ARRIVAL_TIME ,
39
+ CONF_DEPARTURE_TIME ,
40
+ CONF_DESTINATION_ENTITY_ID ,
41
+ CONF_DESTINATION_LATITUDE ,
42
+ CONF_DESTINATION_LONGITUDE ,
43
+ CONF_ORIGIN_ENTITY_ID ,
44
+ CONF_ORIGIN_LATITUDE ,
45
+ CONF_ORIGIN_LONGITUDE ,
46
+ CONF_ROUTE_MODE ,
47
+ DEFAULT_SCAN_INTERVAL ,
48
+ DOMAIN ,
49
+ ROUTE_MODE_FASTEST ,
50
+ )
51
+ from .model import HERETravelTimeAPIParams , HERETravelTimeData
39
52
40
53
BACKOFF_MULTIPLIER = 1.1
41
54
47
60
48
61
49
62
class HERERoutingDataUpdateCoordinator (DataUpdateCoordinator [HERETravelTimeData ]):
50
- """here_routing DataUpdateCoordinator."""
63
+ """HERETravelTime DataUpdateCoordinator for the routing API ."""
51
64
52
65
config_entry : HereConfigEntry
53
66
@@ -56,7 +69,6 @@ def __init__(
56
69
hass : HomeAssistant ,
57
70
config_entry : HereConfigEntry ,
58
71
api_key : str ,
59
- config : HERETravelTimeConfig ,
60
72
) -> None :
61
73
"""Initialize."""
62
74
super ().__init__ (
@@ -67,41 +79,34 @@ def __init__(
67
79
update_interval = timedelta (seconds = DEFAULT_SCAN_INTERVAL ),
68
80
)
69
81
self ._api = HERERoutingApi (api_key )
70
- self .config = config
71
82
72
83
async def _async_update_data (self ) -> HERETravelTimeData :
73
84
"""Get the latest data from the HERE Routing API."""
74
- origin , destination , arrival , departure = prepare_parameters (
75
- self .hass , self .config
76
- )
77
-
78
- route_mode = (
79
- RoutingMode .FAST
80
- if self .config .route_mode == ROUTE_MODE_FASTEST
81
- else RoutingMode .SHORT
82
- )
85
+ params = prepare_parameters (self .hass , self .config_entry )
83
86
84
87
_LOGGER .debug (
85
88
(
86
89
"Requesting route for origin: %s, destination: %s, route_mode: %s,"
87
90
" mode: %s, arrival: %s, departure: %s"
88
91
),
89
- origin ,
90
- destination ,
91
- route_mode ,
92
- TransportMode (self . config .travel_mode ),
93
- arrival ,
94
- departure ,
92
+ params . origin ,
93
+ params . destination ,
94
+ params . route_mode ,
95
+ TransportMode (params .travel_mode ),
96
+ params . arrival ,
97
+ params . departure ,
95
98
)
96
99
97
100
try :
98
101
response = await self ._api .route (
99
- transport_mode = TransportMode (self .config .travel_mode ),
100
- origin = here_routing .Place (origin [0 ], origin [1 ]),
101
- destination = here_routing .Place (destination [0 ], destination [1 ]),
102
- routing_mode = route_mode ,
103
- arrival_time = arrival ,
104
- departure_time = departure ,
102
+ transport_mode = TransportMode (params .travel_mode ),
103
+ origin = here_routing .Place (params .origin [0 ], params .origin [1 ]),
104
+ destination = here_routing .Place (
105
+ params .destination [0 ], params .destination [1 ]
106
+ ),
107
+ routing_mode = params .route_mode ,
108
+ arrival_time = params .arrival ,
109
+ departure_time = params .departure ,
105
110
return_values = [Return .POLYINE , Return .SUMMARY ],
106
111
spans = [Spans .NAMES ],
107
112
)
@@ -175,7 +180,7 @@ def _parse_routing_response(self, response: dict[str, Any]) -> HERETravelTimeDat
175
180
class HERETransitDataUpdateCoordinator (
176
181
DataUpdateCoordinator [HERETravelTimeData | None ]
177
182
):
178
- """HERETravelTime DataUpdateCoordinator."""
183
+ """HERETravelTime DataUpdateCoordinator for the transit API ."""
179
184
180
185
config_entry : HereConfigEntry
181
186
@@ -184,7 +189,6 @@ def __init__(
184
189
hass : HomeAssistant ,
185
190
config_entry : HereConfigEntry ,
186
191
api_key : str ,
187
- config : HERETravelTimeConfig ,
188
192
) -> None :
189
193
"""Initialize."""
190
194
super ().__init__ (
@@ -195,32 +199,31 @@ def __init__(
195
199
update_interval = timedelta (seconds = DEFAULT_SCAN_INTERVAL ),
196
200
)
197
201
self ._api = HERETransitApi (api_key )
198
- self .config = config
199
202
200
203
async def _async_update_data (self ) -> HERETravelTimeData | None :
201
204
"""Get the latest data from the HERE Routing API."""
202
- origin , destination , arrival , departure = prepare_parameters (
203
- self .hass , self .config
204
- )
205
+ params = prepare_parameters (self .hass , self .config_entry )
205
206
206
207
_LOGGER .debug (
207
208
(
208
209
"Requesting transit route for origin: %s, destination: %s, arrival: %s,"
209
210
" departure: %s"
210
211
),
211
- origin ,
212
- destination ,
213
- arrival ,
214
- departure ,
212
+ params . origin ,
213
+ params . destination ,
214
+ params . arrival ,
215
+ params . departure ,
215
216
)
216
217
try :
57AE
217
218
response = await self ._api .route (
218
- origin = here_transit .Place (latitude = origin [0 ], longitude = origin [1 ]),
219
+ origin = here_transit .Place (
220
+ latitude = params .origin [0 ], longitude = params .origin [1 ]
221
+ ),
219
222
destination = here_transit .Place (
220
- latitude = destination [0 ], longitude = destination [1 ]
223
+ latitude = params . destination [0 ], longitude = params . destination [1 ]
221
224
),
222
- arrival_time = arrival ,
223
- departure_time = departure ,
225
+ arrival_time = params . arrival ,
226
+ departure_time = params . departure ,
224
227
return_values = [
225
228
here_transit .Return .POLYLINE ,
226
229
here_transit .Return .TRAVEL_SUMMARY ,
@@ -285,8 +288,8 @@ def _parse_transit_response(self, response: dict[str, Any]) -> HERETravelTimeDat
285
288
286
289
def prepare_parameters (
287
290
hass : HomeAssistant ,
288
- config : HERETravelTimeConfig ,
289
- ) -> tuple [ list [ str ], list [ str ], str | None , str | None ] :
291
+ config_entry : HereConfigEntry ,
292
+ ) -> HERETravelTimeAPIParams :
290
293
"""Prepare parameters for the HERE api."""
291
294
292
295
def _from_entity_id (entity_id : str ) -> list [str ]:
@@ -305,32 +308,55 @@ def _from_entity_id(entity_id: str) -> list[str]:
305
308
return formatted_coordinates
306
309
307
310
# Destination
308
- if config .destination_entity_id is not None :
309
- destination = _from_entity_id (config .destination_entity_id )
311
+ if (
312
+ destination_entity_id := config_entry .data .get (CONF_DESTINATION_ENTITY_ID )
313
+ ) is not None :
314
+ destination = _from_entity_id (str (destination_entity_id ))
310
315
else :
311
316
destination = [
312
- str (config . destination_latitude ),
313
- str (config . destination_longitude ),
317
+ str (config_entry . data [ CONF_DESTINATION_LATITUDE ] ),
318
+ str (config_entry . data [ CONF_DESTINATION_LONGITUDE ] ),
314
319
]
315
320
316
321
# Origin
317
- if config . origin_entity_id is not None :
318
- origin = _from_entity_id (config . origin_entity_id )
322
+ if ( origin_entity_id := config_entry . data . get ( CONF_ORIGIN_ENTITY_ID )) is not None :
323
+ origin = _from_entity_id (str ( origin_entity_id ) )
319
324
else :
320
325
origin = [
321
- str (config . origin_latitude ),
322
- str (config . origin_longitude ),
326
+ str (config_entry . data [ CONF_ORIGIN_LATITUDE ] ),
327
+ str (config_entry . data [ CONF_ORIGIN_LONGITUDE ] ),
323
328
]
324
329
325
330
# Arrival/Departure
326
331
arrival : str | None = None
332
+ if (
333
+ conf_arrival := dt_util .parse_time (
334
+ config_entry .options .get (CONF_ARRIVAL_TIME , "" )
335
+ )
336
+ ) is not None :
337
+ arrival = next_datetime (conf_arrival ).isoformat ()
327
338
departure : str | None = None
328
- if config .arrival is not None :
329
- arrival = next_datetime (config .arrival ).isoformat ()
330
- if config .departure is not None :
331
- departure = next_datetime (config .departure ).isoformat ()
332
-
333
- return (origin , destination , arrival , departure )
339
+ if (
340
+ conf_departure := dt_util .parse_time (
341
+ config_entry .options .get (CONF_DEPARTURE_TIME , "" )
342
+ )
343
+ ) is not None :
344
+ departure = next_datetime (conf_departure ).isoformat ()
345
+
346
+ route_mode = (
347
+ RoutingMode .FAST
348
+ if config_entry .options [CONF_ROUTE_MODE ] == ROUTE_MODE_FASTEST
349
+ else RoutingMode .SHORT
350
+ )
351
+
352
+ return HERETravelTimeAPIParams (
353
+ destination = destination ,
354
+ origin = origin ,
355
+ travel_mode = config_entry .data [CONF_MODE ],
356
+ route_mode = route_mode ,
357
+ arrival = arrival ,
358
+ departure = departure ,
359
+ )
334
360
335
361
336
362
def build_hass_attribution (sections : list [dict [str , Any ]]) -> str | None :
0 commit comments