2626import voluptuous as vol
2727
2828from homeassistant .config_entries import ConfigEntry
29- from homeassistant .const import UnitOfLength
29+ from homeassistant .const import CONF_MODE , UnitOfLength
3030from homeassistant .core import HomeAssistant
3131from homeassistant .helpers import config_validation as cv
3232from homeassistant .helpers .location import find_coordinates
3333from homeassistant .helpers .update_coordinator import DataUpdateCoordinator , UpdateFailed
3434from homeassistant .util import dt as dt_util
3535from homeassistant .util .unit_conversion import DistanceConverter
3636
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
3952
4053BACKOFF_MULTIPLIER = 1.1
4154
4760
4861
4962class HERERoutingDataUpdateCoordinator (DataUpdateCoordinator [HERETravelTimeData ]):
50- """here_routing DataUpdateCoordinator."""
63+ """HERETravelTime DataUpdateCoordinator for the routing API ."""
5164
5265 config_entry : HereConfigEntry
5366
@@ -56,7 +69,6 @@ def __init__(
5669 hass : HomeAssistant ,
5770 config_entry : HereConfigEntry ,
5871 api_key : str ,
59- config : HERETravelTimeConfig ,
6072 ) -> None :
6173 """Initialize."""
6274 super ().__init__ (
@@ -67,41 +79,34 @@ def __init__(
6779 update_interval = timedelta (seconds = DEFAULT_SCAN_INTERVAL ),
6880 )
6981 self ._api = HERERoutingApi (api_key )
70- self .config = config
7182
7283 async def _async_update_data (self ) -> HERETravelTimeData :
7384 """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 )
8386
8487 _LOGGER .debug (
8588 (
8689 "Requesting route for origin: %s, destination: %s, route_mode: %s,"
8790 " mode: %s, arrival: %s, departure: %s"
8891 ),
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 ,
9598 )
9699
97100 try :
98101 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 ,
105110 return_values = [Return .POLYINE , Return .SUMMARY ],
106111 spans = [Spans .NAMES ],
107112 )
@@ -175,7 +180,7 @@ def _parse_routing_response(self, response: dict[str, Any]) -> HERETravelTimeDat
175180class HERETransitDataUpdateCoordinator (
176181 DataUpdateCoordinator [HERETravelTimeData | None ]
177182):
178- """HERETravelTime DataUpdateCoordinator."""
183+ """HERETravelTime DataUpdateCoordinator for the transit API ."""
179184
180185 config_entry : HereConfigEntry
181186
@@ -184,7 +189,6 @@ def __init__(
184189 hass : HomeAssistant ,
185190 config_entry : HereConfigEntry ,
186191 api_key : str ,
187- config : HERETravelTimeConfig ,
188192 ) -> None :
189193 """Initialize."""
190194 super ().__init__ (
@@ -195,32 +199,31 @@ def __init__(
195199 update_interval = timedelta (seconds = DEFAULT_SCAN_INTERVAL ),
196200 )
197201 self ._api = HERETransitApi (api_key )
198- self .config = config
199202
200203 async def _async_update_data (self ) -> HERETravelTimeData | None :
201204 """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 )
205206
206207 _LOGGER .debug (
207208 (
208209 "Requesting transit route for origin: %s, destination: %s, arrival: %s,"
209210 " departure: %s"
210211 ),
211- origin ,
212- destination ,
213- arrival ,
214- departure ,
212+ params . origin ,
213+ params . destination ,
214+ params . arrival ,
215+ params . departure ,
215216 )
216217 try :
217218 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+ ),
219222 destination = here_transit .Place (
220- latitude = destination [0 ], longitude = destination [1 ]
223+ latitude = params . destination [0 ], longitude = params . destination [1 ]
221224 ),
222- arrival_time = arrival ,
223- departure_time = departure ,
225+ arrival_time = params . arrival ,
226+ departure_time = params . departure ,
224227 return_values = [
225228 here_transit .Return .POLYLINE ,
226229 here_transit .Return .TRAVEL_SUMMARY ,
@@ -285,8 +288,8 @@ def _parse_transit_response(self, response: dict[str, Any]) -> HERETravelTimeDat
285288
286289def prepare_parameters (
287290 hass : HomeAssistant ,
288- config : HERETravelTimeConfig ,
289- ) -> tuple [ list [ str ], list [ str ], str | None , str | None ] :
291+ config_entry : HereConfigEntry ,
292+ ) -> HERETravelTimeAPIParams :
290293 """Prepare parameters for the HERE api."""
291294
292295 def _from_entity_id (entity_id : str ) -> list [str ]:
@@ -305,32 +308,55 @@ def _from_entity_id(entity_id: str) -> list[str]:
305308 return formatted_coordinates
306309
307310 # 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 ))
310315 else :
311316 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 ] ),
314319 ]
315320
316321 # 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 ) )
319324 else :
320325 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 ] ),
323328 ]
324329
325330 # Arrival/Departure
326- arrival : str | None = None
327- 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 )
331+ arrival : datetime | 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 )
338+ departure : datetime | None = None
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 )
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+ )
334360
335361
336362def build_hass_attribution (sections : list [dict [str , Any ]]) -> str | None :
0 commit comments