From 0ac34aaa52c9188ddd5357477281d87647387bc7 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 29 Mar 2016 20:14:27 -0700 Subject: [PATCH 1/4] Fix for when you have an Uber product that doesnt give a price estimate --- homeassistant/components/sensor/uber.py | 101 ++++++++++++++---------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/homeassistant/components/sensor/uber.py b/homeassistant/components/sensor/uber.py index 8c3a7d01e0e..adfdcd51fda 100644 --- a/homeassistant/components/sensor/uber.py +++ b/homeassistant/components/sensor/uber.py @@ -49,7 +49,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): (product_id not in wanted_product_ids): continue dev.append(UberSensor('time', timeandpriceest, product_id, product)) - dev.append(UberSensor('price', timeandpriceest, product_id, product)) + if product.get('price_details') is not None: + dev.append(UberSensor('price', timeandpriceest, product_id, product)) add_devices(dev) @@ -70,13 +71,18 @@ class UberSensor(Entity): time_estimate = self._product.get('time_estimate_seconds', 0) self._state = int(time_estimate / 60) elif self._sensortype == "price": - price_details = self._product['price_details'] - if price_details['low_estimate'] is None: - self._unit_of_measurement = price_details['currency_code'] - self._state = int(price_details['minimum']) + price_details = self._product.get('price_details', {}) + if price_details is not None: + self._unit_of_measurement = price_details.get('currency_code', + 'N/A') + if price_details.get('low_estimate') is None: + statekey = 'minimum' + else: + statekey = 'low_estimate' + self._state = int(price_details.get(statekey, 0)) else: - self._unit_of_measurement = price_details['currency_code'] - self._state = int(price_details['low_estimate']) + self._unit_of_measurement = 'N/A' + self._state = int(0) self.update() @property @@ -97,16 +103,16 @@ class UberSensor(Entity): @property def device_state_attributes(self): """Return the state attributes.""" - price_details = self._product['price_details'] - distance_key = 'Trip distance (in {}s)'.format(price_details[ - 'distance_unit']) - distance_val = self._product.get('distance') - if (price_details.get('distance_unit') is None) or \ - (self._product.get('distance') is None): - distance_key = 'Trip distance' - distance_val = 'N/A' - time_estimate = self._product['time_estimate_seconds'] - return { + price_details = self._product.get('price_details') + if price_details is None: + distance_key = 'Trip distance (in miles)' + distance_val = self._product.get('distance', 'N/A') + else: + distance_key = 'Trip distance (in {}s)'.format(price_details[ + 'distance_unit']) + distance_val = self._product.get('distance') + time_estimate = self._product.get('time_estimate_seconds', 'N/A') + params = { 'Product ID': self._product['product_id'], 'Product short description': self._product['short_description'], 'Product display name': self._product['display_name'], @@ -114,20 +120,27 @@ class UberSensor(Entity): 'Pickup time estimate (in seconds)': time_estimate, 'Trip duration (in seconds)': self._product.get('duration', 'N/A'), distance_key: distance_val, - 'Vehicle Capacity': self._product['capacity'], - 'Minimum price': price_details['minimum'], - 'Cost per minute': price_details['cost_per_minute'], - 'Distance units': price_details['distance_unit'], - 'Cancellation fee': price_details['cancellation_fee'], - 'Cost per distance unit': price_details['cost_per_distance'], - 'Base price': price_details['base'], - 'Price estimate': price_details.get('estimate', 'N/A'), - 'Price currency code': price_details.get('currency_code'), - 'High price estimate': price_details.get('high_estimate', 'N/A'), - 'Low price estimate': price_details.get('low_estimate', 'N/A'), - 'Surge multiplier': price_details.get('surge_multiplier', 'N/A') + 'Vehicle Capacity': self._product['capacity'] } + if price_details is not None: + params['Minimum price'] = price_details['minimum'], + params['Cost per minute'] = price_details['cost_per_minute'], + params['Distance units'] = price_details['distance_unit'], + params['Cancellation fee'] = price_details['cancellation_fee'], + params['Cost per distance unit'] = price_details['cost_per_distance'], + params['Base price'] = price_details['base'], + params['Price estimate'] = price_details.get('estimate', 'N/A'), + params['Price currency code'] = price_details.get('currency_code'), + params['High price estimate'] = price_details.get('high_estimate', + 'N/A'), + params['Low price estimate'] = price_details.get('low_estimate', + 'N/A'), + params['Surge multiplier'] = price_details.get('surge_multiplier', + 'N/A') + + return params + @property def icon(self): """Icon to use in the frontend, if any.""" @@ -142,9 +155,12 @@ class UberSensor(Entity): time_estimate = self._product.get('time_estimate_seconds', 0) self._state = int(time_estimate / 60) elif self._sensortype == "price": - price_details = self._product['price_details'] - min_price = price_details['minimum'] - self._state = int(price_details.get('low_estimate', min_price)) + price_details = self._product.get('price_details') + if price_details is not None: + min_price = price_details.get('minimum') + self._state = int(price_details.get('low_estimate', min_price)) + else: + self._state = int(0) # pylint: disable=too-few-public-methods @@ -186,17 +202,22 @@ class UberEstimate(object): self.end_latitude, self.end_longitude) - prices = price_response.json.get('prices') + prices = price_response.json.get('prices', []) for price in prices: product = self.products[price['product_id']] - price_details = product["price_details"] - product["duration"] = price['duration'] - product["distance"] = price['distance'] - price_details["estimate"] = price['estimate'] - price_details["high_estimate"] = price['high_estimate'] - price_details["low_estimate"] = price['low_estimate'] - price_details["surge_multiplier"] = price['surge_multiplier'] + price_details = product.get("price_details", {}) + product["duration"] = price.get('duration', '0') + product["distance"] = price.get('distance', '0') + if price_details is not None: + price_details["estimate"] = price.get('estimate', + '0') + price_details["high_estimate"] = price.get('high_estimate', + '0') + price_details["low_estimate"] = price.get('low_estimate', + '0') + price_details["surge_multiplier"] = price.get('surge_multiplier', + '0') estimate_response = client.get_pickup_time_estimates( self.start_latitude, self.start_longitude) From 60b427accccc0f7c0bda5ea0391d2dca8f41cbd1 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 29 Mar 2016 20:36:04 -0700 Subject: [PATCH 2/4] Flake8 and Pylint fixes --- homeassistant/components/sensor/uber.py | 63 +++++++++++++------------ 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/sensor/uber.py b/homeassistant/components/sensor/uber.py index adfdcd51fda..948a9240ec2 100644 --- a/homeassistant/components/sensor/uber.py +++ b/homeassistant/components/sensor/uber.py @@ -50,7 +50,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None): continue dev.append(UberSensor('time', timeandpriceest, product_id, product)) if product.get('price_details') is not None: - dev.append(UberSensor('price', timeandpriceest, product_id, product)) + dev.append(UberSensor('price', timeandpriceest, + product_id, product)) add_devices(dev) @@ -81,8 +82,8 @@ class UberSensor(Entity): statekey = 'low_estimate' self._state = int(price_details.get(statekey, 0)) else: - self._unit_of_measurement = 'N/A' - self._state = int(0) + self._unit_of_measurement = 'N/A' + self._state = int(0) self.update() @property @@ -108,9 +109,9 @@ class UberSensor(Entity): distance_key = 'Trip distance (in miles)' distance_val = self._product.get('distance', 'N/A') else: - distance_key = 'Trip distance (in {}s)'.format(price_details[ - 'distance_unit']) - distance_val = self._product.get('distance') + distance_key = 'Trip distance (in {}s)'.format(price_details[ + 'distance_unit']) + distance_val = self._product.get('distance') time_estimate = self._product.get('time_estimate_seconds', 'N/A') params = { 'Product ID': self._product['product_id'], @@ -124,20 +125,20 @@ class UberSensor(Entity): } if price_details is not None: - params['Minimum price'] = price_details['minimum'], - params['Cost per minute'] = price_details['cost_per_minute'], - params['Distance units'] = price_details['distance_unit'], - params['Cancellation fee'] = price_details['cancellation_fee'], - params['Cost per distance unit'] = price_details['cost_per_distance'], - params['Base price'] = price_details['base'], - params['Price estimate'] = price_details.get('estimate', 'N/A'), - params['Price currency code'] = price_details.get('currency_code'), - params['High price estimate'] = price_details.get('high_estimate', - 'N/A'), - params['Low price estimate'] = price_details.get('low_estimate', - 'N/A'), - params['Surge multiplier'] = price_details.get('surge_multiplier', - 'N/A') + params['Minimum price'] = price_details['minimum'], + params['Cost per minute'] = price_details['cost_per_minute'], + params['Distance units'] = price_details['distance_unit'], + params['Cancellation fee'] = price_details['cancellation_fee'], + params['Cost per distance'] = price_details['cost_per_distance'], + params['Base price'] = price_details['base'], + params['Price estimate'] = price_details.get('estimate', 'N/A'), + params['Price currency code'] = price_details.get('currency_code'), + params['High price estimate'] = price_details.get('high_estimate', + 'N/A'), + params['Low price estimate'] = price_details.get('low_estimate', + 'N/A'), + params['Surge multiplier'] = price_details.get('surge_multiplier', + 'N/A') return params @@ -157,10 +158,10 @@ class UberSensor(Entity): elif self._sensortype == "price": price_details = self._product.get('price_details') if price_details is not None: - min_price = price_details.get('minimum') - self._state = int(price_details.get('low_estimate', min_price)) + min_price = price_details.get('minimum') + self._state = int(price_details.get('low_estimate', min_price)) else: - self._state = int(0) + self._state = int(0) # pylint: disable=too-few-public-methods @@ -210,14 +211,14 @@ class UberEstimate(object): product["duration"] = price.get('duration', '0') product["distance"] = price.get('distance', '0') if price_details is not None: - price_details["estimate"] = price.get('estimate', - '0') - price_details["high_estimate"] = price.get('high_estimate', - '0') - price_details["low_estimate"] = price.get('low_estimate', - '0') - price_details["surge_multiplier"] = price.get('surge_multiplier', - '0') + price_details["estimate"] = price.get('estimate', + '0') + price_details["high_estimate"] = price.get('high_estimate', + '0') + price_details["low_estimate"] = price.get('low_estimate', + '0') + surge_multiplier = price.get('surge_multiplier', '0') + price_details["surge_multiplier"] = surge_multiplier estimate_response = client.get_pickup_time_estimates( self.start_latitude, self.start_longitude) From 55daf51108ec9781962ecd286ac27b7acdbeaeca Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 29 Mar 2016 20:42:15 -0700 Subject: [PATCH 3/4] Dont set default value for price_details to empty dict since we want to check price_details for is None --- homeassistant/components/sensor/uber.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/uber.py b/homeassistant/components/sensor/uber.py index 948a9240ec2..59c1c50f85c 100644 --- a/homeassistant/components/sensor/uber.py +++ b/homeassistant/components/sensor/uber.py @@ -72,7 +72,7 @@ class UberSensor(Entity): time_estimate = self._product.get('time_estimate_seconds', 0) self._state = int(time_estimate / 60) elif self._sensortype == "price": - price_details = self._product.get('price_details', {}) + price_details = self._product.get('price_details') if price_details is not None: self._unit_of_measurement = price_details.get('currency_code', 'N/A') @@ -207,7 +207,7 @@ class UberEstimate(object): for price in prices: product = self.products[price['product_id']] - price_details = product.get("price_details", {}) + price_details = product.get("price_details") product["duration"] = price.get('duration', '0') product["distance"] = price.get('distance', '0') if price_details is not None: From 56e64d477a4969c0531669dff548077c332eec0a Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 29 Mar 2016 21:02:17 -0700 Subject: [PATCH 4/4] Little fixes --- homeassistant/components/sensor/uber.py | 32 ++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/sensor/uber.py b/homeassistant/components/sensor/uber.py index 59c1c50f85c..0c17f4a98a2 100644 --- a/homeassistant/components/sensor/uber.py +++ b/homeassistant/components/sensor/uber.py @@ -49,7 +49,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): (product_id not in wanted_product_ids): continue dev.append(UberSensor('time', timeandpriceest, product_id, product)) - if product.get('price_details') is not None: + if 'price_details' in product: dev.append(UberSensor('price', timeandpriceest, product_id, product)) add_devices(dev) @@ -72,18 +72,18 @@ class UberSensor(Entity): time_estimate = self._product.get('time_estimate_seconds', 0) self._state = int(time_estimate / 60) elif self._sensortype == "price": - price_details = self._product.get('price_details') - if price_details is not None: + if 'price_details' in self._product: + price_details = self._product['price_details'] self._unit_of_measurement = price_details.get('currency_code', 'N/A') - if price_details.get('low_estimate') is None: + if 'low_estimate' in price_details: statekey = 'minimum' else: statekey = 'low_estimate' self._state = int(price_details.get(statekey, 0)) else: self._unit_of_measurement = 'N/A' - self._state = int(0) + self._state = 0 self.update() @property @@ -104,14 +104,6 @@ class UberSensor(Entity): @property def device_state_attributes(self): """Return the state attributes.""" - price_details = self._product.get('price_details') - if price_details is None: - distance_key = 'Trip distance (in miles)' - distance_val = self._product.get('distance', 'N/A') - else: - distance_key = 'Trip distance (in {}s)'.format(price_details[ - 'distance_unit']) - distance_val = self._product.get('distance') time_estimate = self._product.get('time_estimate_seconds', 'N/A') params = { 'Product ID': self._product['product_id'], @@ -120,11 +112,14 @@ class UberSensor(Entity): 'Product description': self._product['description'], 'Pickup time estimate (in seconds)': time_estimate, 'Trip duration (in seconds)': self._product.get('duration', 'N/A'), - distance_key: distance_val, 'Vehicle Capacity': self._product['capacity'] } - if price_details is not None: + if 'price_details' in self._product: + price_details = self._product['price_details'] + distance_key = 'Trip distance (in {}s)'.format(price_details[ + 'distance_unit']) + distance_val = self._product.get('distance') params['Minimum price'] = price_details['minimum'], params['Cost per minute'] = price_details['cost_per_minute'], params['Distance units'] = price_details['distance_unit'], @@ -139,6 +134,11 @@ class UberSensor(Entity): 'N/A'), params['Surge multiplier'] = price_details.get('surge_multiplier', 'N/A') + else: + distance_key = 'Trip distance (in miles)' + distance_val = self._product.get('distance', 'N/A') + + params[distance_key] = distance_val return params @@ -161,7 +161,7 @@ class UberSensor(Entity): min_price = price_details.get('minimum') self._state = int(price_details.get('low_estimate', min_price)) else: - self._state = int(0) + self._state = 0 # pylint: disable=too-few-public-methods