2019-04-03 15:40:03 +00:00
|
|
|
"""Support for MyQ-Enabled Garage Doors."""
|
2017-02-13 10:20:07 +00:00
|
|
|
import logging
|
2019-10-21 20:46:39 +00:00
|
|
|
|
|
|
|
from pymyq import login
|
|
|
|
from pymyq.errors import MyQError
|
2017-02-13 10:20:07 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
from homeassistant.components.cover import (
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_CLOSE,
|
|
|
|
SUPPORT_OPEN,
|
2019-12-09 13:20:40 +00:00
|
|
|
CoverDevice,
|
2019-04-10 21:24:12 +00:00
|
|
|
)
|
2017-02-13 10:20:07 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_TYPE,
|
|
|
|
CONF_USERNAME,
|
|
|
|
STATE_CLOSED,
|
|
|
|
STATE_CLOSING,
|
|
|
|
STATE_OPEN,
|
|
|
|
STATE_OPENING,
|
2019-04-10 21:24:12 +00:00
|
|
|
)
|
2018-11-18 17:37:03 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2017-05-30 21:17:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-08-29 12:33:09 +00:00
|
|
|
MYQ_TO_HASS = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"closed": STATE_CLOSED,
|
|
|
|
"closing": STATE_CLOSING,
|
|
|
|
"open": STATE_OPEN,
|
|
|
|
"opening": STATE_OPENING,
|
2018-08-29 12:33:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
2019-10-21 20:46:39 +00:00
|
|
|
# This parameter is no longer used; keeping it to avoid a breaking change in
|
|
|
|
# a hotfix, but in a future main release, this should be removed:
|
|
|
|
vol.Optional(CONF_TYPE): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2017-02-13 10:20:07 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-11-18 17:37:03 +00:00
|
|
|
"""Set up the platform."""
|
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2018-11-18 17:37:03 +00:00
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2018-11-18 17:37:03 +00:00
|
|
|
try:
|
2019-10-21 20:46:39 +00:00
|
|
|
myq = await login(username, password, websession)
|
2018-11-18 17:37:03 +00:00
|
|
|
except MyQError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("There was an error while logging in: %s", err)
|
2018-11-18 17:37:03 +00:00
|
|
|
return
|
2017-05-30 21:17:32 +00:00
|
|
|
|
2019-10-21 20:46:39 +00:00
|
|
|
async_add_entities([MyQDevice(device) for device in myq.covers.values()], True)
|
2017-02-13 10:20:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyQDevice(CoverDevice):
|
|
|
|
"""Representation of a MyQ cover."""
|
|
|
|
|
2018-11-18 17:37:03 +00:00
|
|
|
def __init__(self, device):
|
2017-02-13 10:20:07 +00:00
|
|
|
"""Initialize with API object, device id."""
|
2018-11-18 17:37:03 +00:00
|
|
|
self._device = device
|
2017-02-13 10:20:07 +00:00
|
|
|
|
2018-05-24 05:58:35 +00:00
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Define this cover as a garage door."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "garage"
|
2018-05-24 05:58:35 +00:00
|
|
|
|
2017-02-13 10:20:07 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the garage door if any."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return self._device.name
|
2017-02-13 10:20:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return true if cover is closed, else False."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_CLOSED
|
2018-08-29 12:33:09 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closing(self):
|
|
|
|
"""Return if the cover is closing or not."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_CLOSING
|
2018-08-29 12:33:09 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_opening(self):
|
|
|
|
"""Return if the cover is opening or not."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return MYQ_TO_HASS.get(self._device.state) == STATE_OPENING
|
2017-02-13 10:20:07 +00:00
|
|
|
|
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):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2018-11-18 17:37:03 +00:00
|
|
|
return self._device.device_id
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs):
|
|
|
|
"""Issue close command to cover."""
|
|
|
|
await self._device.close()
|
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs):
|
|
|
|
"""Issue open command to cover."""
|
|
|
|
await self._device.open()
|
2018-08-31 14:47:37 +00:00
|
|
|
|
2018-11-18 17:37:03 +00:00
|
|
|
async def async_update(self):
|
2017-02-13 10:20:07 +00:00
|
|
|
"""Update status of cover."""
|
2018-11-18 17:37:03 +00:00
|
|
|
await self._device.update()
|