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

@ -6,9 +6,11 @@ import requests
import voluptuous as vol
from homeassistant.components.binary_sensor import (
BinarySensorDevice, PLATFORM_SCHEMA, DEVICE_CLASSES_SCHEMA)
from homeassistant.const import (
CONF_RESOURCE, CONF_PIN, CONF_NAME, CONF_DEVICE_CLASS)
BinarySensorDevice,
PLATFORM_SCHEMA,
DEVICE_CLASSES_SCHEMA,
)
from homeassistant.const import CONF_RESOURCE, CONF_PIN, CONF_NAME, CONF_DEVICE_CLASS
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
@ -16,12 +18,14 @@ _LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_PIN): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME): cv.string,
vol.Required(CONF_PIN): cv.string,
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
@ -33,8 +37,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
try:
response = requests.get(resource, timeout=10).json()
except requests.exceptions.MissingSchema:
_LOGGER.error("Missing resource or schema in configuration. "
"Add http:// to your URL")
_LOGGER.error(
"Missing resource or schema in configuration. " "Add http:// to your URL"
)
return False
except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device at %s", resource)
@ -42,9 +47,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
arest = ArestData(resource, pin)
add_entities([ArestBinarySensor(
arest, resource, config.get(CONF_NAME, response[CONF_NAME]),
device_class, pin)], True)
add_entities(
[
ArestBinarySensor(
arest,
resource,
config.get(CONF_NAME, response[CONF_NAME]),
device_class,
pin,
)
],
True,
)
class ArestBinarySensor(BinarySensorDevice):
@ -60,7 +74,8 @@ class ArestBinarySensor(BinarySensorDevice):
if self._pin is not None:
request = requests.get(
'{}/mode/{}/i'.format(self._resource, self._pin), timeout=10)
"{}/mode/{}/i".format(self._resource, self._pin), timeout=10
)
if request.status_code != 200:
_LOGGER.error("Can't set mode of %s", self._resource)
@ -72,7 +87,7 @@ class ArestBinarySensor(BinarySensorDevice):
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return bool(self.arest.data.get('state'))
return bool(self.arest.data.get("state"))
@property
def device_class(self):
@ -97,8 +112,9 @@ class ArestData:
def update(self):
"""Get the latest data from aREST device."""
try:
response = requests.get('{}/digital/{}'.format(
self._resource, self._pin), timeout=10)
self.data = {'state': response.json()['return_value']}
response = requests.get(
"{}/digital/{}".format(self._resource, self._pin), timeout=10
)
self.data = {"state": response.json()["return_value"]}
except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device '%s'", self._resource)