This commit is contained in:
Paulus Schoutsen 2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View file

@ -5,44 +5,54 @@ import requests
import voluptuous as vol
from homeassistant.components.cover import (
CoverDevice, PLATFORM_SCHEMA, SUPPORT_OPEN, SUPPORT_CLOSE)
CoverDevice,
PLATFORM_SCHEMA,
SUPPORT_OPEN,
SUPPORT_CLOSE,
)
from homeassistant.const import (
CONF_DEVICE, CONF_NAME, STATE_UNKNOWN, STATE_CLOSED, STATE_OPEN,
CONF_COVERS, CONF_HOST, CONF_PORT)
CONF_DEVICE,
CONF_NAME,
STATE_UNKNOWN,
STATE_CLOSED,
STATE_OPEN,
CONF_COVERS,
CONF_HOST,
CONF_PORT,
)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
ATTR_DISTANCE_SENSOR = 'distance_sensor'
ATTR_DOOR_STATE = 'door_state'
ATTR_SIGNAL_STRENGTH = 'wifi_signal'
ATTR_DISTANCE_SENSOR = "distance_sensor"
ATTR_DOOR_STATE = "door_state"
ATTR_SIGNAL_STRENGTH = "wifi_signal"
CONF_DEVICE_ID = 'device_id'
CONF_DEVICE_KEY = 'device_key'
CONF_DEVICE_ID = "device_id"
CONF_DEVICE_KEY = "device_key"
DEFAULT_NAME = 'OpenGarage'
DEFAULT_NAME = "OpenGarage"
DEFAULT_PORT = 80
STATE_CLOSING = 'closing'
STATE_OFFLINE = 'offline'
STATE_OPENING = 'opening'
STATE_STOPPED = 'stopped'
STATE_CLOSING = "closing"
STATE_OFFLINE = "offline"
STATE_OPENING = "opening"
STATE_STOPPED = "stopped"
STATES_MAP = {
0: STATE_CLOSED,
1: STATE_OPEN,
}
STATES_MAP = {0: STATE_CLOSED, 1: STATE_OPEN}
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,
})
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,
}
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_COVERS): cv.schema_with_slug_keys(COVER_SCHEMA)}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
@ -56,7 +66,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
CONF_HOST: device_config.get(CONF_HOST),
CONF_PORT: device_config.get(CONF_PORT),
CONF_DEVICE_ID: device_config.get(CONF_DEVICE, device_id),
CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY)
CONF_DEVICE_KEY: device_config.get(CONF_DEVICE_KEY),
}
covers.append(OpenGarageCover(hass, args))
@ -69,11 +79,10 @@ class OpenGarageCover(CoverDevice):
def __init__(self, hass, args):
"""Initialize the cover."""
self.opengarage_url = 'http://{}:{}'.format(
args[CONF_HOST], args[CONF_PORT])
self.opengarage_url = "http://{}:{}".format(args[CONF_HOST], args[CONF_PORT])
self.hass = hass
self._name = args[CONF_NAME]
self.device_id = args['device_id']
self.device_id = args["device_id"]
self._device_key = args[CONF_DEVICE_KEY]
self._state = None
self._state_before_move = None
@ -133,9 +142,9 @@ class OpenGarageCover(CoverDevice):
try:
status = self._get_status()
if self._name is None:
if status['name'] is not None:
self._name = status['name']
state = STATES_MAP.get(status.get('door'), STATE_UNKNOWN)
if status["name"] is not None:
self._name = status["name"]
state = STATES_MAP.get(status.get("door"), STATE_UNKNOWN)
if self._state_before_move is not None:
if self._state_before_move != state:
self._state = state
@ -144,41 +153,43 @@ class OpenGarageCover(CoverDevice):
self._state = state
_LOGGER.debug("%s status: %s", self._name, self._state)
self.signal = status.get('rssi')
self.dist = status.get('dist')
self.signal = status.get("rssi")
self.dist = status.get("dist")
self._available = True
except requests.exceptions.RequestException as ex:
_LOGGER.error("Unable to connect to OpenGarage device: %(reason)s",
dict(reason=ex))
_LOGGER.error(
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
)
self._state = STATE_OFFLINE
def _get_status(self):
"""Get latest status."""
url = '{}/jc'.format(self.opengarage_url)
url = "{}/jc".format(self.opengarage_url)
ret = requests.get(url, timeout=10)
return ret.json()
def _push_button(self):
"""Send commands to API."""
url = '{}/cc?dkey={}&click=1'.format(
self.opengarage_url, self._device_key)
url = "{}/cc?dkey={}&click=1".format(self.opengarage_url, self._device_key)
try:
response = requests.get(url, timeout=10).json()
if response['result'] == 2:
_LOGGER.error("Unable to control %s: Device key is incorrect",
self._name)
if response["result"] == 2:
_LOGGER.error(
"Unable to control %s: Device key is incorrect", self._name
)
self._state = self._state_before_move
self._state_before_move = None
except requests.exceptions.RequestException as ex:
_LOGGER.error("Unable to connect to OpenGarage device: %(reason)s",
dict(reason=ex))
_LOGGER.error(
"Unable to connect to OpenGarage device: %(reason)s", dict(reason=ex)
)
self._state = self._state_before_move
self._state_before_move = None
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return 'garage'
return "garage"
@property
def supported_features(self):