From 8ee32a8fbd3b476abf4801e93e6157bd35f380ad Mon Sep 17 00:00:00 2001
From: Marcelo Moreira de Mello <tchello.mello@gmail.com>
Date: Tue, 30 May 2017 17:17:32 -0400
Subject: [PATCH] Added persistent error message if cover.myq fails to load
 (#7700)

* Show persistent error if cover.myq fails

* Fixed typo on getLogger()

* Added ValueError on except condition

* Make pylint happy

* Removed DEFAULT_ENTITY_NAMESPACE since it is not being used
---
 homeassistant/components/cover/myq.py | 41 +++++++++++++++++----------
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/homeassistant/components/cover/myq.py b/homeassistant/components/cover/myq.py
index 06ec7ca6211..5c79540a249 100644
--- a/homeassistant/components/cover/myq.py
+++ b/homeassistant/components/cover/myq.py
@@ -12,19 +12,25 @@ from homeassistant.components.cover import CoverDevice
 from homeassistant.const import (
     CONF_USERNAME, CONF_PASSWORD, CONF_TYPE, STATE_CLOSED)
 import homeassistant.helpers.config_validation as cv
+import homeassistant.loader as loader
 
 REQUIREMENTS = [
     'https://github.com/arraylabs/pymyq/archive/v0.0.8.zip'
     '#pymyq==0.0.8']
 
+_LOGGER = logging.getLogger(__name__)
+
+DEFAULT_NAME = 'myq'
+
+NOTIFICATION_ID = 'myq_notification'
+NOTIFICATION_TITLE = 'MyQ Cover Setup'
+
 COVER_SCHEMA = vol.Schema({
     vol.Required(CONF_TYPE): cv.string,
     vol.Required(CONF_USERNAME): cv.string,
     vol.Required(CONF_PASSWORD): cv.string
 })
 
-DEFAULT_NAME = 'myq'
-
 
 def setup_platform(hass, config, add_devices, discovery_info=None):
     """Set up the MyQ component."""
@@ -33,23 +39,28 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
     username = config.get(CONF_USERNAME)
     password = config.get(CONF_PASSWORD)
     brand = config.get(CONF_TYPE)
-
-    logger = logging.getLogger(__name__)
-
+    persistent_notification = loader.get_component('persistent_notification')
     myq = pymyq(username, password, brand)
 
-    if not myq.is_supported_brand():
-        logger.error("Unsupported type. See documentation")
-        return
-
-    if not myq.is_login_valid():
-        logger.error("Username or Password is incorrect")
-        return
-
     try:
+        if not myq.is_supported_brand():
+            raise ValueError("Unsupported type. See documentation")
+
+        if not myq.is_login_valid():
+            raise ValueError("Username or Password is incorrect")
+
         add_devices(MyQDevice(myq, door) for door in myq.get_garage_doors())
-    except (TypeError, KeyError, NameError) as ex:
-        logger.error("%s", ex)
+        return True
+
+    except (TypeError, KeyError, NameError, ValueError) as ex:
+        _LOGGER.error("%s", ex)
+        persistent_notification.create(
+            hass, 'Error: {}<br />'
+            'You will need to restart hass after fixing.'
+            ''.format(ex),
+            title=NOTIFICATION_TITLE,
+            notification_id=NOTIFICATION_ID)
+        return False
 
 
 class MyQDevice(CoverDevice):