2019-04-03 15:40:03 +00:00
|
|
|
"""Platform for the opengarage.io cover component."""
|
2017-05-02 15:46:56 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
2017-05-02 20:47:20 +00:00
|
|
|
import voluptuous as vol
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
from homeassistant.components.cover import (
|
2019-08-25 10:05:42 +00:00
|
|
|
DEVICE_CLASS_GARAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_CLOSE,
|
2019-12-09 13:26:53 +00:00
|
|
|
SUPPORT_OPEN,
|
2020-04-25 16:07:15 +00:00
|
|
|
CoverEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-05-02 15:46:56 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_COVERS,
|
|
|
|
CONF_HOST,
|
2019-12-09 13:26:53 +00:00
|
|
|
CONF_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PORT,
|
2019-11-25 23:47:18 +00:00
|
|
|
CONF_SSL,
|
|
|
|
CONF_VERIFY_SSL,
|
2019-12-09 13:26:53 +00:00
|
|
|
STATE_CLOSED,
|
2019-08-25 10:05:42 +00:00
|
|
|
STATE_CLOSING,
|
2019-12-09 13:26:53 +00:00
|
|
|
STATE_OPEN,
|
2019-08-25 10:05:42 +00:00
|
|
|
STATE_OPENING,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-05-02 15:46:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2017-05-02 20:47:20 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DISTANCE_SENSOR = "distance_sensor"
|
|
|
|
ATTR_DOOR_STATE = "door_state"
|
|
|
|
ATTR_SIGNAL_STRENGTH = "wifi_signal"
|
2017-05-02 20:47:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE_KEY = "device_key"
|
2017-05-02 20:47:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "OpenGarage"
|
2017-05-02 20:47:20 +00:00
|
|
|
DEFAULT_PORT = 80
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
STATES_MAP = {0: STATE_CLOSED, 1: STATE_OPEN}
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
COVER_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_DEVICE_KEY): cv.string,
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
2019-11-25 23:47:18 +00:00
|
|
|
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA)}
|
|
|
|
)
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-04-06 16:06:47 +00:00
|
|
|
"""Set up the OpenGarage covers."""
|
2017-05-02 15:46:56 +00:00
|
|
|
covers = []
|
|
|
|
devices = config.get(CONF_COVERS)
|
|
|
|
|
2019-08-25 10:05:42 +00:00
|
|
|
for device_config in devices.values():
|
2017-05-02 15:46:56 +00:00
|
|
|
args = {
|
|
|
|
CONF_NAME: device_config.get(CONF_NAME),
|
|
|
|
CONF_HOST: device_config.get(CONF_HOST),
|
|
|
|
CONF_PORT: device_config.get(CONF_PORT),
|
2020-04-10 20:01:57 +00:00
|
|
|
CONF_SSL: device_config[CONF_SSL],
|
2019-11-25 23:47:18 +00:00
|
|
|
CONF_VERIFY_SSL: device_config.get(CONF_VERIFY_SSL),
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY),
|
2017-05-02 15:46:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 10:05:42 +00:00
|
|
|
covers.append(OpenGarageCover(args))
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(covers, True)
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:07:15 +00:00
|
|
|
class OpenGarageCover(CoverEntity):
|
2017-05-02 15:46:56 +00:00
|
|
|
"""Representation of a OpenGarage cover."""
|
|
|
|
|
2019-08-25 10:05:42 +00:00
|
|
|
def __init__(self, args):
|
2017-05-02 15:46:56 +00:00
|
|
|
"""Initialize the cover."""
|
2020-04-07 21:14:28 +00:00
|
|
|
self.opengarage_url = (
|
2020-04-10 20:01:57 +00:00
|
|
|
f"{'https' if args[CONF_SSL] else 'http'}://"
|
2020-04-07 21:14:28 +00:00
|
|
|
f"{args[CONF_HOST]}:{args[CONF_PORT]}"
|
2019-11-25 23:47:18 +00:00
|
|
|
)
|
2017-05-02 15:46:56 +00:00
|
|
|
self._name = args[CONF_NAME]
|
2018-04-06 16:06:47 +00:00
|
|
|
self._device_key = args[CONF_DEVICE_KEY]
|
|
|
|
self._state = None
|
2017-05-02 15:46:56 +00:00
|
|
|
self._state_before_move = None
|
2019-08-25 10:05:42 +00:00
|
|
|
self._device_state_attributes = {}
|
2017-05-02 15:46:56 +00:00
|
|
|
self._available = True
|
2019-11-25 23:47:18 +00:00
|
|
|
self._verify_ssl = args[CONF_VERIFY_SSL]
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the cover."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
2019-08-25 10:05:42 +00:00
|
|
|
return self._device_state_attributes
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_closed(self):
|
|
|
|
"""Return if the cover is closed."""
|
2019-08-25 10:05:42 +00:00
|
|
|
if self._state is None:
|
2017-05-02 15:46:56 +00:00
|
|
|
return None
|
2017-07-06 06:30:01 +00:00
|
|
|
return self._state in [STATE_CLOSED, STATE_OPENING]
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def close_cover(self, **kwargs):
|
2017-05-02 15:46:56 +00:00
|
|
|
"""Close the cover."""
|
2019-08-25 10:05:42 +00:00
|
|
|
if self._state in [STATE_CLOSED, STATE_CLOSING]:
|
|
|
|
return
|
|
|
|
self._state_before_move = self._state
|
|
|
|
self._state = STATE_CLOSING
|
|
|
|
self._push_button()
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def open_cover(self, **kwargs):
|
2017-05-02 15:46:56 +00:00
|
|
|
"""Open the cover."""
|
2019-08-25 10:05:42 +00:00
|
|
|
if self._state in [STATE_OPEN, STATE_OPENING]:
|
|
|
|
return
|
|
|
|
self._state_before_move = self._state
|
|
|
|
self._state = STATE_OPENING
|
|
|
|
self._push_button()
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get updated status from API."""
|
|
|
|
try:
|
2019-09-03 18:35:00 +00:00
|
|
|
status = requests.get(f"{self.opengarage_url}/jc", timeout=10).json()
|
2018-04-06 16:06:47 +00:00
|
|
|
except requests.exceptions.RequestException as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
|
|
|
|
)
|
2019-08-25 10:05:42 +00:00
|
|
|
self._available = False
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._name is None and status["name"] is not None:
|
|
|
|
self._name = status["name"]
|
|
|
|
state = STATES_MAP.get(status.get("door"))
|
|
|
|
if self._state_before_move is not None:
|
|
|
|
if self._state_before_move != state:
|
|
|
|
self._state = state
|
|
|
|
self._state_before_move = None
|
|
|
|
else:
|
|
|
|
self._state = state
|
|
|
|
|
|
|
|
_LOGGER.debug("%s status: %s", self._name, self._state)
|
|
|
|
if status.get("rssi") is not None:
|
|
|
|
self._device_state_attributes[ATTR_SIGNAL_STRENGTH] = status.get("rssi")
|
|
|
|
if status.get("dist") is not None:
|
|
|
|
self._device_state_attributes[ATTR_DISTANCE_SENSOR] = status.get("dist")
|
|
|
|
if self._state is not None:
|
|
|
|
self._device_state_attributes[ATTR_DOOR_STATE] = self._state
|
2017-05-02 15:46:56 +00:00
|
|
|
|
2019-08-25 10:05:42 +00:00
|
|
|
self._available = True
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
def _push_button(self):
|
|
|
|
"""Send commands to API."""
|
2019-08-25 10:05:42 +00:00
|
|
|
result = -1
|
2017-05-02 15:46:56 +00:00
|
|
|
try:
|
2019-08-25 10:05:42 +00:00
|
|
|
result = requests.get(
|
2019-11-25 23:47:18 +00:00
|
|
|
f"{self.opengarage_url}/cc?dkey={self._device_key}&click=1",
|
|
|
|
timeout=10,
|
|
|
|
verify=self._verify_ssl,
|
2019-08-25 10:05:42 +00:00
|
|
|
).json()["result"]
|
2018-04-06 16:06:47 +00:00
|
|
|
except requests.exceptions.RequestException as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
|
|
|
|
)
|
2019-08-25 10:05:42 +00:00
|
|
|
if result == 1:
|
|
|
|
return
|
|
|
|
|
|
|
|
if result == 2:
|
|
|
|
_LOGGER.error("Unable to control %s: Device key is incorrect", self._name)
|
|
|
|
elif result > 2:
|
|
|
|
_LOGGER.error("Unable to control %s: Error code %s", self._name, result)
|
|
|
|
|
|
|
|
self._state = self._state_before_move
|
|
|
|
self._state_before_move = None
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
2019-08-25 10:05:42 +00:00
|
|
|
return DEVICE_CLASS_GARAGE
|
2017-05-02 15:46:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_OPEN | SUPPORT_CLOSE
|