1 """Support for Waze travel time sensor."""
3 from __future__
import annotations
6 from datetime
import timedelta
11 from pywaze.route_calculator
import WazeRouteCalculator
22 EVENT_HOMEASSISTANT_STARTED,
33 from .
import async_get_travel_times
36 CONF_AVOID_SUBSCRIPTION_ROADS,
37 CONF_AVOID_TOLL_ROADS,
51 _LOGGER = logging.getLogger(__name__)
57 SECONDS_BETWEEN_API_CALLS = 0.5
62 config_entry: ConfigEntry,
63 async_add_entities: AddEntitiesCallback,
65 """Set up a Waze travel time sensor entry."""
66 destination = config_entry.data[CONF_DESTINATION]
67 origin = config_entry.data[CONF_ORIGIN]
68 region = config_entry.data[CONF_REGION]
69 name = config_entry.data.get(CONF_NAME, DEFAULT_NAME)
77 sensor =
WazeTravelTime(config_entry.entry_id, name, origin, destination, data)
83 """Representation of a Waze travel time sensor."""
85 _attr_attribution =
"Powered by Waze"
86 _attr_native_unit_of_measurement = UnitOfTime.MINUTES
87 _attr_device_class = SensorDeviceClass.DURATION
88 _attr_state_class = SensorStateClass.MEASUREMENT
90 entry_type=DeviceEntryType.SERVICE,
92 identifiers={(DOMAIN, DOMAIN)},
93 configuration_url=
"https://www.waze.com",
95 _attr_translation_key =
"waze_travel_time"
103 waze_data: WazeTravelTimeData,
105 """Initialize the Waze travel time sensor."""
114 """Handle when entity is added."""
115 if self.
hasshass.state
is not CoreState.running:
116 self.
hasshass.bus.async_listen_once(
117 EVENT_HOMEASSISTANT_STARTED, self.
first_updatefirst_update
124 """Return the state of the sensor."""
125 if self.
_waze_data_waze_data.duration
is not None:
126 return round(self.
_waze_data_waze_data.duration)
132 """Return the state attributes of the last update."""
133 if self.
_waze_data_waze_data.duration
is None:
137 "duration": self.
_waze_data_waze_data.duration,
138 "distance": self.
_waze_data_waze_data.distance,
141 "destination": self.
_waze_data_waze_data.destination,
145 """Run first update and write state."""
150 """Fetch new state data for the sensor."""
151 _LOGGER.debug(
"Fetching Route for %s", self.
_attr_name_attr_name)
154 await self.
hasshass.data[DOMAIN][SEMAPHORE].acquire()
157 await asyncio.sleep(SECONDS_BETWEEN_API_CALLS)
159 self.
hasshass.data[DOMAIN][SEMAPHORE].release()
163 """WazeTravelTime Data object."""
166 self, region: str, client: httpx.AsyncClient, config_entry: ConfigEntry
168 """Set up WazeRouteCalculator."""
170 self.
clientclient = WazeRouteCalculator(region=region, client=client)
171 self.origin: str |
None =
None
172 self.destination: str |
None =
None
178 """Update WazeRouteCalculator Sensor."""
180 "Getting update for origin: %s destination: %s",
184 if self.origin
is not None and self.destination
is not None:
186 incl_filter = self.
config_entryconfig_entry.options[CONF_INCL_FILTER]
187 excl_filter = self.
config_entryconfig_entry.options[CONF_EXCL_FILTER]
188 realtime = self.
config_entryconfig_entry.options[CONF_REALTIME]
189 vehicle_type = self.
config_entryconfig_entry.options[CONF_VEHICLE_TYPE]
190 avoid_toll_roads = self.
config_entryconfig_entry.options[CONF_AVOID_TOLL_ROADS]
191 avoid_subscription_roads = self.
config_entryconfig_entry.options[
192 CONF_AVOID_SUBSCRIPTION_ROADS
194 avoid_ferries = self.
config_entryconfig_entry.options[CONF_AVOID_FERRIES]
201 avoid_subscription_roads,
210 _LOGGER.warning(
"No routes found")
213 self.
durationduration = route.duration
214 distance = route.distance
216 if self.
config_entryconfig_entry.options[CONF_UNITS] == IMPERIAL_UNITS:
218 self.
distancedistance = DistanceConverter.convert(
219 distance, UnitOfLength.KILOMETERS, UnitOfLength.MILES
224 self.
routeroute = route.name
None __init__(self, str region, httpx.AsyncClient client, ConfigEntry config_entry)
None first_update(self, _=None)
dict[str, Any]|None extra_state_attributes(self)
float|None native_value(self)
None async_added_to_hass(self)
None __init__(self, str unique_id, str name, str origin, str destination, WazeTravelTimeData waze_data)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
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)
str|None find_coordinates(HomeAssistant hass, str name, list|None recursion_history=None)