1 """The waze_travel_time component."""
4 from collections.abc
import Collection
7 from pywaze.route_calculator
import CalcRoutesResponse, WazeRouteCalculator, WRCError
8 import voluptuous
as vol
29 CONF_AVOID_SUBSCRIPTION_ROADS,
30 CONF_AVOID_TOLL_ROADS,
48 PLATFORMS = [Platform.SENSOR]
50 SERVICE_GET_TRAVEL_TIMES =
"get_travel_times"
51 SERVICE_GET_TRAVEL_TIMES_SCHEMA = vol.Schema(
58 mode=SelectSelectorMode.DROPDOWN,
59 translation_key=CONF_REGION,
64 vol.Optional(CONF_VEHICLE_TYPE, default=DEFAULT_VEHICLE_TYPE):
SelectSelector(
66 options=VEHICLE_TYPES,
67 mode=SelectSelectorMode.DROPDOWN,
68 translation_key=CONF_VEHICLE_TYPE,
75 mode=SelectSelectorMode.DROPDOWN,
76 translation_key=CONF_UNITS,
81 vol.Optional(CONF_AVOID_SUBSCRIPTION_ROADS, default=
False):
BooleanSelector(),
86 _LOGGER = logging.getLogger(__name__)
90 """Load the saved entities."""
91 if SEMAPHORE
not in hass.data.setdefault(DOMAIN, {}):
92 hass.data.setdefault(DOMAIN, {})[SEMAPHORE] = asyncio.Semaphore(1)
94 await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
96 async
def async_get_travel_times_service(service: ServiceCall) -> ServiceResponse:
98 client = WazeRouteCalculator(
99 region=service.data[CONF_REGION].upper(), client=httpx_client
103 origin=service.data[CONF_ORIGIN],
104 destination=service.data[CONF_DESTINATION],
105 vehicle_type=service.data[CONF_VEHICLE_TYPE],
106 avoid_toll_roads=service.data[CONF_AVOID_TOLL_ROADS],
107 avoid_subscription_roads=service.data[CONF_AVOID_SUBSCRIPTION_ROADS],
108 avoid_ferries=service.data[CONF_AVOID_FERRIES],
109 realtime=service.data[CONF_REALTIME],
111 return {
"routes": [vars(route)
for route
in response]}
if response
else None
113 hass.services.async_register(
115 SERVICE_GET_TRAVEL_TIMES,
116 async_get_travel_times_service,
117 SERVICE_GET_TRAVEL_TIMES_SCHEMA,
118 supports_response=SupportsResponse.ONLY,
124 client: WazeRouteCalculator,
128 avoid_toll_roads: bool,
129 avoid_subscription_roads: bool,
132 incl_filters: Collection[str] |
None =
None,
133 excl_filters: Collection[str] |
None =
None,
134 ) -> list[CalcRoutesResponse] |
None:
135 """Get all available routes."""
137 incl_filters = incl_filters
or ()
138 excl_filters = excl_filters
or ()
141 "Getting update for origin: %s destination: %s",
146 vehicle_type =
"" if vehicle_type.upper() ==
"CAR" else vehicle_type.upper()
148 routes = await client.calc_routes(
151 vehicle_type=vehicle_type,
152 avoid_toll_roads=avoid_toll_roads,
153 avoid_subscription_roads=avoid_subscription_roads,
154 avoid_ferries=avoid_ferries,
158 _LOGGER.debug(
"Got routes: %s", routes)
160 incl_routes: list[CalcRoutesResponse] = []
162 def should_include_route(route: CalcRoutesResponse) -> bool:
163 if len(incl_filters) < 1:
165 should_include = any(
166 street_name
in incl_filters
or "" in incl_filters
167 for street_name
in route.street_names
169 if not should_include:
171 "Excluding route [%s], because no inclusive filter matched any streetname",
177 incl_routes = [route
for route
in routes
if should_include_route(route)]
179 filtered_routes: list[CalcRoutesResponse] = []
181 def should_exclude_route(route: CalcRoutesResponse) -> bool:
182 for street_name
in route.street_names:
183 for excl_filter
in excl_filters:
184 if excl_filter == street_name:
186 "Excluding route, because exclusive filter [%s] matched streetname: %s",
194 route
for route
in incl_routes
if not should_exclude_route(route)
197 if len(filtered_routes) < 1:
198 _LOGGER.warning(
"No routes found")
200 except WRCError
as exp:
201 _LOGGER.warning(
"Error on retrieving data: %s", exp)
205 return filtered_routes
209 """Unload a config entry."""
210 return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
214 """Migrate an old config entry."""
216 if config_entry.version == 1:
218 "Migrating from version %s.%s",
219 config_entry.version,
220 config_entry.minor_version,
222 options =
dict(config_entry.options)
223 if (incl_filters := options.pop(CONF_INCL_FILTER,
None))
not in {
None,
""}:
224 options[CONF_INCL_FILTER] = [incl_filters]
226 options[CONF_INCL_FILTER] = DEFAULT_FILTER
227 if (excl_filters := options.pop(CONF_EXCL_FILTER,
None))
not in {
None,
""}:
228 options[CONF_EXCL_FILTER] = [excl_filters]
230 options[CONF_EXCL_FILTER] = DEFAULT_FILTER
231 hass.config_entries.async_update_entry(config_entry, options=options, version=2)
233 "Migration to version %s.%s successful",
234 config_entry.version,
235 config_entry.minor_version,
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
list[CalcRoutesResponse]|None async_get_travel_times(WazeRouteCalculator client, str origin, str destination, str vehicle_type, bool avoid_toll_roads, bool avoid_subscription_roads, bool avoid_ferries, bool realtime, Collection[str]|None incl_filters=None, Collection[str]|None excl_filters=None)
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)