Skip to content

Commit

Permalink
Dominos no order fix (#10935)
Browse files Browse the repository at this point in the history
* check for none

* fix error from no store being set

* typo

* Lint

* fix default as per notes. Lint fix and make closest store None not False

* update default
  • Loading branch information
craigjmidwinter authored and balloob committed Dec 4, 2017
1 parent 29fad3f commit bd6a17a
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions homeassistant/components/dominos.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
vol.Required(ATTR_PHONE): cv.string,
vol.Required(ATTR_ADDRESS): cv.string,
vol.Optional(ATTR_SHOW_MENU): cv.boolean,
vol.Optional(ATTR_ORDERS): vol.All(cv.ensure_list, [_ORDERS_SCHEMA]),
vol.Optional(ATTR_ORDERS, default=[]): vol.All(
cv.ensure_list, [_ORDERS_SCHEMA]),
}),
}, extra=vol.ALLOW_EXTRA)

Expand All @@ -81,7 +82,8 @@ def setup(hass, config):
order = DominosOrder(order_info, dominos)
entities.append(order)

component.add_entities(entities)
if entities:
component.add_entities(entities)

# Return boolean to indicate that initialization was successfully.
return True
Expand All @@ -93,7 +95,8 @@ class Dominos():
def __init__(self, hass, config):
"""Set up main service."""
conf = config[DOMAIN]
from pizzapi import Address, Customer, Store
from pizzapi import Address, Customer
from pizzapi.address import StoreException
self.hass = hass
self.customer = Customer(
conf.get(ATTR_FIRST_NAME),
Expand All @@ -105,7 +108,10 @@ def __init__(self, hass, config):
*self.customer.address.split(','),
country=conf.get(ATTR_COUNTRY))
self.country = conf.get(ATTR_COUNTRY)
self.closest_store = Store()
try:
self.closest_store = self.address.closest_store()
except StoreException:
self.closest_store = None

def handle_order(self, call):
"""Handle ordering pizza."""
Expand All @@ -123,29 +129,31 @@ def update_closest_store(self):
from pizzapi.address import StoreException
try:
self.closest_store = self.address.closest_store()
return True
except StoreException:
self.closest_store = False
self.closest_store = None
return False

def get_menu(self):
"""Return the products from the closest stores menu."""
if self.closest_store is False:
if self.closest_store is None:
_LOGGER.warning('Cannot get menu. Store may be closed')
return

menu = self.closest_store.get_menu()
product_entries = []
return []
else:
menu = self.closest_store.get_menu()
product_entries = []

for product in menu.products:
item = {}
if isinstance(product.menu_data['Variants'], list):
variants = ', '.join(product.menu_data['Variants'])
else:
variants = product.menu_data['Variants']
item['name'] = product.name
item['variants'] = variants
product_entries.append(item)
for product in menu.products:
item = {}
if isinstance(product.menu_data['Variants'], list):
variants = ', '.join(product.menu_data['Variants'])
else:
variants = product.menu_data['Variants']
item['name'] = product.name
item['variants'] = variants
product_entries.append(item)

return product_entries
return product_entries


class DominosProductListView(http.HomeAssistantView):
Expand Down Expand Up @@ -192,7 +200,7 @@ def orderable(self):
@property
def state(self):
"""Return the state either closed, orderable or unorderable."""
if self.dominos.closest_store is False:
if self.dominos.closest_store is None:
return 'closed'
else:
return 'orderable' if self._orderable else 'unorderable'
Expand All @@ -217,6 +225,11 @@ def update(self):
def order(self):
"""Create the order object."""
from pizzapi import Order
from pizzapi.address import StoreException

if self.dominos.closest_store is None:
raise StoreException

order = Order(
self.dominos.closest_store,
self.dominos.customer,
Expand Down

0 comments on commit bd6a17a

Please sign in to comment.