2017-02-13 10:20:07 +00:00
|
|
|
"""
|
|
|
|
Support for MyQ-Enabled Garage Doors.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation
|
|
|
|
https://home-assistant.io/components/cover.myq/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
from homeassistant.components.cover import (
|
|
|
|
CoverDevice, SUPPORT_CLOSE, SUPPORT_OPEN)
|
2017-02-13 10:20:07 +00:00
|
|
|
from homeassistant.const import (
|
2018-09-19 20:03:47 +00:00
|
|
|
CONF_PASSWORD, CONF_TYPE, CONF_USERNAME, STATE_CLOSED, STATE_OPEN,
|
|
|
|
STATE_CLOSING, STATE_OPENING)
|
2017-02-13 10:20:07 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
REQUIREMENTS = ['pymyq==0.0.15']
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2017-05-30 21:17:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'myq'
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
MYQ_TO_HASS = {
|
|
|
|
'closed': STATE_CLOSED,
|
2018-09-19 20:03:47 +00:00
|
|
|
'open': STATE_OPEN,
|
2018-08-29 12:33:09 +00:00
|
|
|
'closing': STATE_CLOSING,
|
|
|
|
'opening': STATE_OPENING
|
|
|
|
}
|
|
|
|
|
2017-05-30 21:17:32 +00:00
|
|
|
NOTIFICATION_ID = 'myq_notification'
|
|
|
|
NOTIFICATION_TITLE = 'MyQ Cover Setup'
|
|
|
|
|
2017-02-13 10:20:07 +00:00
|
|
|
COVER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_TYPE): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the MyQ component."""
|
2017-02-13 10:20:07 +00:00
|
|
|
from pymyq import MyQAPI as pymyq
|
|
|
|
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
brand = config.get(CONF_TYPE)
|
|
|
|
myq = pymyq(username, password, brand)
|
|
|
|
|
2017-05-30 21:17:32 +00:00
|
|
|
try:
|
|
|
|
if not myq.is_supported_brand():
|
|
|
|
raise ValueError("Unsupported type. See documentation")
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2017-05-30 21:17:32 +00:00
|
|
|
if not myq.is_login_valid():
|
|
|
|
raise ValueError("Username or Password is incorrect")
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(MyQDevice(myq, door) for door in myq.get_garage_doors())
|
2017-05-30 21:17:32 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
except (TypeError, KeyError, NameError, ValueError) as ex:
|
|
|
|
_LOGGER.error("%s", ex)
|
2017-07-16 19:39:38 +00:00
|
|
|
hass.components.persistent_notification.create(
|
|
|
|
'Error: {}<br />'
|
2017-05-30 21:17:32 +00:00
|
|
|
'You will need to restart hass after fixing.'
|
|
|
|
''.format(ex),
|
|
|
|
title=NOTIFICATION_TITLE,
|
|
|
|
notification_id=NOTIFICATION_ID)
|
|
|
|
return False
|
2017-02-13 10:20:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyQDevice(CoverDevice):
|
|
|
|
"""Representation of a MyQ cover."""
|
|
|
|
|
|
|
|
def __init__(self, myq, device):
|
|
|
|
"""Initialize with API object, device id."""
|
|
|
|
self.myq = myq
|
|
|
|
self.device_id = device['deviceid']
|
|
|
|
self._name = device['name']
|
|
|
|
self._status = STATE_CLOSED
|
|
|
|
|
2018-05-24 05:58:35 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Define this cover as a garage door."""
|
|
|
|
return 'garage'
|
|
|
|
|
2017-02-13 10:20:07 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Poll for state."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the garage door if any."""
|
|
|
|
return self._name if self._name else DEFAULT_NAME
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return true if cover is closed, else False."""
|
2018-08-29 12:33:09 +00:00
|
|
|
return MYQ_TO_HASS[self._status] == STATE_CLOSED
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closing(self):
|
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
return MYQ_TO_HASS[self._status] == STATE_CLOSING
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_opening(self):
|
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
return MYQ_TO_HASS[self._status] == STATE_OPENING
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def close_cover(self, **kwargs):
|
2017-02-13 10:20:07 +00:00
|
|
|
"""Issue close command to cover."""
|
|
|
|
self.myq.close_device(self.device_id)
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def open_cover(self, **kwargs):
|
2017-02-13 10:20:07 +00:00
|
|
|
"""Issue open command to cover."""
|
|
|
|
self.myq.open_device(self.device_id)
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_OPEN | SUPPORT_CLOSE
|
|
|
|
|
2018-08-31 14:47:37 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique, HASS-friendly identifier for this entity."""
|
|
|
|
return self.device_id
|
|
|
|
|
2017-02-13 10:20:07 +00:00
|
|
|
def update(self):
|
|
|
|
"""Update status of cover."""
|
|
|
|
self._status = self.myq.get_status(self.device_id)
|