This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+83 -48
View File
@@ -7,8 +7,12 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE, CONF_RESOURCE,
CONF_MONITORED_VARIABLES, CONF_NAME)
CONF_UNIT_OF_MEASUREMENT,
CONF_VALUE_TEMPLATE,
CONF_RESOURCE,
CONF_MONITORED_VARIABLES,
CONF_NAME,
)
from homeassistant.exceptions import TemplateError
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
@@ -18,25 +22,31 @@ _LOGGER = logging.getLogger(__name__)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
CONF_FUNCTIONS = 'functions'
CONF_PINS = 'pins'
CONF_FUNCTIONS = "functions"
CONF_PINS = "pins"
DEFAULT_NAME = 'aREST sensor'
DEFAULT_NAME = "aREST sensor"
PIN_VARIABLE_SCHEMA = vol.Schema({
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
})
PIN_VARIABLE_SCHEMA = vol.Schema(
{
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PINS, default={}):
vol.Schema({cv.string: PIN_VARIABLE_SCHEMA}),
vol.Optional(CONF_MONITORED_VARIABLES, default={}):
vol.Schema({cv.string: PIN_VARIABLE_SCHEMA}),
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_RESOURCE): cv.url,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PINS, default={}): vol.Schema(
{cv.string: PIN_VARIABLE_SCHEMA}
),
vol.Optional(CONF_MONITORED_VARIABLES, default={}): vol.Schema(
{cv.string: PIN_VARIABLE_SCHEMA}
),
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
@@ -48,8 +58,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)
@@ -66,7 +77,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
def _render(value):
try:
return value_template.async_render({'value': value})
return value_template.async_render({"value": value})
except TemplateError:
_LOGGER.exception("Error parsing value")
return value
@@ -77,25 +88,37 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
if var_conf is not None:
for variable, var_data in var_conf.items():
if variable not in response['variables']:
if variable not in response["variables"]:
_LOGGER.error("Variable: %s does not exist", variable)
continue
renderer = make_renderer(var_data.get(CONF_VALUE_TEMPLATE))
dev.append(ArestSensor(
arest, resource, config.get(CONF_NAME, response[CONF_NAME]),
var_data.get(CONF_NAME, variable), variable=variable,
unit_of_measurement=var_data.get(CONF_UNIT_OF_MEASUREMENT),
renderer=renderer))
dev.append(
ArestSensor(
arest,
resource,
config.get(CONF_NAME, response[CONF_NAME]),
var_data.get(CONF_NAME, variable),
variable=variable,
unit_of_measurement=var_data.get(CONF_UNIT_OF_MEASUREMENT),
renderer=renderer,
)
)
if pins is not None:
for pinnum, pin in pins.items():
renderer = make_renderer(pin.get(CONF_VALUE_TEMPLATE))
dev.append(ArestSensor(
ArestData(resource, pinnum), resource,
config.get(CONF_NAME, response[CONF_NAME]), pin.get(CONF_NAME),
pin=pinnum, unit_of_measurement=pin.get(
CONF_UNIT_OF_MEASUREMENT), renderer=renderer))
dev.append(
ArestSensor(
ArestData(resource, pinnum),
resource,
config.get(CONF_NAME, response[CONF_NAME]),
pin.get(CONF_NAME),
pin=pinnum,
unit_of_measurement=pin.get(CONF_UNIT_OF_MEASUREMENT),
renderer=renderer,
)
)
add_entities(dev, True)
@@ -103,12 +126,21 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class ArestSensor(Entity):
"""Implementation of an aREST sensor for exposed variables."""
def __init__(self, arest, resource, location, name, variable=None,
pin=None, unit_of_measurement=None, renderer=None):
def __init__(
self,
arest,
resource,
location,
name,
variable=None,
pin=None,
unit_of_measurement=None,
renderer=None,
):
"""Initialize the sensor."""
self.arest = arest
self._resource = resource
self._name = '{} {}'.format(location.title(), name.title())
self._name = "{} {}".format(location.title(), name.title())
self._variable = variable
self._pin = pin
self._state = None
@@ -117,7 +149,8 @@ class ArestSensor(Entity):
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)
@@ -136,11 +169,10 @@ class ArestSensor(Entity):
"""Return the state of the sensor."""
values = self.arest.data
if 'error' in values:
return values['error']
if "error" in values:
return values["error"]
value = self._renderer(
values.get('value', values.get(self._variable, None)))
value = self._renderer(values.get("value", values.get(self._variable, None)))
return value
def update(self):
@@ -169,17 +201,20 @@ class ArestData:
try:
if self._pin is None:
response = requests.get(self._resource, timeout=10)
self.data = response.json()['variables']
self.data = response.json()["variables"]
else:
try:
if str(self._pin[0]) == 'A':
response = requests.get('{}/analog/{}'.format(
self._resource, self._pin[1:]), timeout=10)
self.data = {'value': response.json()['return_value']}
if str(self._pin[0]) == "A":
response = requests.get(
"{}/analog/{}".format(self._resource, self._pin[1:]),
timeout=10,
)
self.data = {"value": response.json()["return_value"]}
except TypeError:
response = requests.get('{}/digital/{}'.format(
self._resource, self._pin), timeout=10)
self.data = {'value': response.json()['return_value']}
response = requests.get(
"{}/digital/{}".format(self._resource, self._pin), timeout=10
)
self.data = {"value": response.json()["return_value"]}
self.available = True
except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device %s", self._resource)