1 """DataUpdateCoordinator for the Trafikverket Ferry integration."""
3 from __future__
import annotations
5 from datetime
import date, datetime, time, timedelta
7 from typing
import TYPE_CHECKING, Any
9 from pytrafikverket
import TrafikverketFerry
10 from pytrafikverket.exceptions
import InvalidAuthentication, NoFerryFound
11 from pytrafikverket.models
import FerryStopModel
20 from .const
import CONF_FROM, CONF_TIME, CONF_TO, DOMAIN
23 from .
import TVFerryConfigEntry
25 _LOGGER = logging.getLogger(__name__)
30 """Return the date of the next time a specific weekday happen."""
31 days_ahead = weekday - fromdate.weekday()
38 """Calculate the next departuredate from an array input of short days."""
39 today_date = date.today()
40 today_weekday = date.weekday(today_date)
41 if WEEKDAYS[today_weekday]
in departure:
44 next_departure = WEEKDAYS.index(day)
45 if next_departure > today_weekday:
47 return next_weekday(today_date, WEEKDAYS.index(departure[0]))
51 """A Trafikverket Data Update Coordinator."""
53 config_entry: TVFerryConfigEntry
55 def __init__(self, hass: HomeAssistant) ->
None:
56 """Initialize the Trafikverket coordinator."""
61 update_interval=TIME_BETWEEN_UPDATES,
66 self._from: str = self.
config_entryconfig_entry.data[CONF_FROM]
67 self._to: str = self.
config_entryconfig_entry.data[CONF_TO]
68 self._time: time |
None = dt_util.parse_time(self.
config_entryconfig_entry.data[CONF_TIME])
69 self._weekdays: list[str] = self.
config_entryconfig_entry.data[CONF_WEEKDAY]
72 """Fetch data from Trafikverket."""
75 current_time = dt_util.now()
80 dt_util.get_default_time_zone(),
85 when =
max(when, current_time)
90 ] = await self.
_ferry_api_ferry_api.async_get_next_ferry_stops(
91 self._from, self._to, when, 3
93 except NoFerryFound
as error:
95 f
"Departure {when} encountered a problem: {error}"
97 except InvalidAuthentication
as error:
101 "departure_time": routedata[0].departure_time,
102 "departure_from": routedata[0].from_harbor_name,
103 "departure_to": routedata[0].to_harbor_name,
104 "departure_modified": routedata[0].modified_time,
105 "departure_information": routedata[0].other_information,
106 "departure_time_next": routedata[1].departure_time,
107 "departure_time_next_next": routedata[2].departure_time,
109 _LOGGER.debug(
"States: %s", states)
dict[str, Any] _async_update_data(self)
None __init__(self, HomeAssistant hass)
date next_departuredate(list[str] departure)
date next_weekday(date fromdate, int weekday)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)