Merge pull request #1644 from robbiet480/uber-fixes

Uber sensor breaking fix
pull/1645/head
Paulus Schoutsen 2016-03-29 22:20:21 -07:00
commit d0e613194e
1 changed files with 63 additions and 41 deletions

View File

@ -47,7 +47,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
(product_id not in wanted_product_ids): (product_id not in wanted_product_ids):
continue continue
dev.append(UberSensor('time', timeandpriceest, product_id, product)) dev.append(UberSensor('time', timeandpriceest, product_id, product))
dev.append(UberSensor('price', timeandpriceest, product_id, product)) if 'price_details' in product:
dev.append(UberSensor('price', timeandpriceest,
product_id, product))
add_devices(dev) add_devices(dev)
@ -68,13 +70,18 @@ class UberSensor(Entity):
time_estimate = self._product.get('time_estimate_seconds', 0) time_estimate = self._product.get('time_estimate_seconds', 0)
self._state = int(time_estimate / 60) self._state = int(time_estimate / 60)
elif self._sensortype == "price": elif self._sensortype == "price":
price_details = self._product['price_details'] if 'price_details' in self._product:
if price_details['low_estimate'] is None: price_details = self._product['price_details']
self._unit_of_measurement = price_details['currency_code'] self._unit_of_measurement = price_details.get('currency_code',
self._state = int(price_details['minimum']) 'N/A')
if 'low_estimate' in price_details:
statekey = 'minimum'
else:
statekey = 'low_estimate'
self._state = int(price_details.get(statekey, 0))
else: else:
self._unit_of_measurement = price_details['currency_code'] self._unit_of_measurement = 'N/A'
self._state = int(price_details['low_estimate']) self._state = 0
self.update() self.update()
@property @property
@ -95,37 +102,44 @@ class UberSensor(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
price_details = self._product['price_details'] time_estimate = self._product.get('time_estimate_seconds', 'N/A')
distance_key = 'Trip distance (in {}s)'.format(price_details[ params = {
'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 {
'Product ID': self._product['product_id'], 'Product ID': self._product['product_id'],
'Product short description': self._product['short_description'], 'Product short description': self._product['short_description'],
'Product display name': self._product['display_name'], 'Product display name': self._product['display_name'],
'Product description': self._product['description'], 'Product description': self._product['description'],
'Pickup time estimate (in seconds)': time_estimate, 'Pickup time estimate (in seconds)': time_estimate,
'Trip duration (in seconds)': self._product.get('duration', 'N/A'), 'Trip duration (in seconds)': self._product.get('duration', 'N/A'),
distance_key: distance_val, 'Vehicle Capacity': self._product['capacity']
'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')
} }
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'],
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')
else:
distance_key = 'Trip distance (in miles)'
distance_val = self._product.get('distance', 'N/A')
params[distance_key] = distance_val
return params
@property @property
def icon(self): def icon(self):
"""Icon to use in the frontend, if any.""" """Icon to use in the frontend, if any."""
@ -140,9 +154,12 @@ class UberSensor(Entity):
time_estimate = self._product.get('time_estimate_seconds', 0) time_estimate = self._product.get('time_estimate_seconds', 0)
self._state = int(time_estimate / 60) self._state = int(time_estimate / 60)
elif self._sensortype == "price": elif self._sensortype == "price":
price_details = self._product['price_details'] price_details = self._product.get('price_details')
min_price = price_details['minimum'] if price_details is not None:
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 = 0
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
@ -184,17 +201,22 @@ class UberEstimate(object):
self.end_latitude, self.end_latitude,
self.end_longitude) self.end_longitude)
prices = price_response.json.get('prices') prices = price_response.json.get('prices', [])
for price in prices: for price in prices:
product = self.products[price['product_id']] product = self.products[price['product_id']]
price_details = product["price_details"] price_details = product.get("price_details")
product["duration"] = price['duration'] product["duration"] = price.get('duration', '0')
product["distance"] = price['distance'] product["distance"] = price.get('distance', '0')
price_details["estimate"] = price['estimate'] if price_details is not None:
price_details["high_estimate"] = price['high_estimate'] price_details["estimate"] = price.get('estimate',
price_details["low_estimate"] = price['low_estimate'] '0')
price_details["surge_multiplier"] = price['surge_multiplier'] 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( estimate_response = client.get_pickup_time_estimates(
self.start_latitude, self.start_longitude) self.start_latitude, self.start_longitude)