From 0ac34aaa52c9188ddd5357477281d87647387bc7 Mon Sep 17 00:00:00 2001 From: Robbie Trencheny Date: Tue, 29 Mar 2016 20:14:27 -0700 Subject: [PATCH] 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)