From bd6a17a3a5104659534e8a58c4fc819a7b40f6cf Mon Sep 17 00:00:00 2001 From: "Craig J. Ward" Date: Sun, 3 Dec 2017 21:34:58 -0600 Subject: [PATCH] Dominos no order fix (#10935) * 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 --- homeassistant/components/dominos.py | 55 ++++++++++++++++++----------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/dominos.py b/homeassistant/components/dominos.py index 867bdfafc6b..633ea1b0c5e 100644 --- a/homeassistant/components/dominos.py +++ b/homeassistant/components/dominos.py @@ -58,7 +58,8 @@ CONFIG_SCHEMA = vol.Schema({ 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) @@ -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 @@ -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), @@ -105,7 +108,10 @@ class Dominos(): *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.""" @@ -123,29 +129,31 @@ class Dominos(): 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 + return [] + else: + menu = self.closest_store.get_menu() + product_entries = [] - 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): @@ -192,7 +200,7 @@ class DominosOrder(Entity): @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' @@ -217,6 +225,11 @@ class DominosOrder(Entity): 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,