Waze Travel Time: optional inclusive/exclusive filters (#14000)
* Waze Travel Time: optional inclusive/exclusive filters Added optional `inc_filter` and `excl_filter' params that allow to refine the reported routes: the first is not always the best/desired. A simple case-insensitive filtering (no regular expression) is used. * fix line lenght * fix spaces * Rename var * Fix typo * Fix missing var
This commit is contained in:
parent
f516cc7dc6
commit
6231394614
1 changed files with 17 additions and 2 deletions
|
@ -26,6 +26,8 @@ ATTR_ROUTE = 'route'
|
|||
CONF_ATTRIBUTION = "Data provided by the Waze.com"
|
||||
CONF_DESTINATION = 'destination'
|
||||
CONF_ORIGIN = 'origin'
|
||||
CONF_INCL_FILTER = 'incl_filter'
|
||||
CONF_EXCL_FILTER = 'excl_filter'
|
||||
|
||||
DEFAULT_NAME = 'Waze Travel Time'
|
||||
|
||||
|
@ -40,6 +42,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
vol.Required(CONF_DESTINATION): cv.string,
|
||||
vol.Required(CONF_REGION): vol.In(REGIONS),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_INCL_FILTER): cv.string,
|
||||
vol.Optional(CONF_EXCL_FILTER): cv.string,
|
||||
})
|
||||
|
||||
|
||||
|
@ -49,9 +53,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||
name = config.get(CONF_NAME)
|
||||
origin = config.get(CONF_ORIGIN)
|
||||
region = config.get(CONF_REGION)
|
||||
incl_filter = config.get(CONF_INCL_FILTER)
|
||||
excl_filter = config.get(CONF_EXCL_FILTER)
|
||||
|
||||
try:
|
||||
waze_data = WazeRouteData(origin, destination, region)
|
||||
waze_data = WazeRouteData(
|
||||
origin, destination, region, incl_filter, excl_filter)
|
||||
except requests.exceptions.HTTPError as error:
|
||||
_LOGGER.error("%s", error)
|
||||
return
|
||||
|
@ -109,11 +116,13 @@ class WazeTravelTime(Entity):
|
|||
class WazeRouteData(object):
|
||||
"""Get data from Waze."""
|
||||
|
||||
def __init__(self, origin, destination, region):
|
||||
def __init__(self, origin, destination, region, incl_filter, excl_filter):
|
||||
"""Initialize the data object."""
|
||||
self._destination = destination
|
||||
self._origin = origin
|
||||
self._region = region
|
||||
self._incl_filter = incl_filter
|
||||
self._excl_filter = excl_filter
|
||||
self.data = {}
|
||||
|
||||
@Throttle(SCAN_INTERVAL)
|
||||
|
@ -125,6 +134,12 @@ class WazeRouteData(object):
|
|||
params = WazeRouteCalculator.WazeRouteCalculator(
|
||||
self._origin, self._destination, self._region, None)
|
||||
results = params.calc_all_routes_info()
|
||||
if self._incl_filter is not None:
|
||||
results = {k: v for k, v in results.items() if
|
||||
self._incl_filter.lower() in k.lower()}
|
||||
if self._excl_filter is not None:
|
||||
results = {k: v for k, v in results.items() if
|
||||
self._excl_filter.lower() not in k.lower()}
|
||||
best_route = next(iter(results))
|
||||
(duration, distance) = results[best_route]
|
||||
best_route_str = bytes(best_route, 'ISO-8859-1').decode('UTF-8')
|
||||
|
|
Loading…
Add table
Reference in a new issue