From 62313946149154362189c1979720e5e58dc59ed6 Mon Sep 17 00:00:00 2001 From: Mario Di Raimondo Date: Wed, 9 May 2018 00:35:03 +0200 Subject: [PATCH] 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 --- .../components/sensor/waze_travel_time.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/waze_travel_time.py b/homeassistant/components/sensor/waze_travel_time.py index 47589f33530..dbcfcb9cc27 100644 --- a/homeassistant/components/sensor/waze_travel_time.py +++ b/homeassistant/components/sensor/waze_travel_time.py @@ -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')