[binary_sensor.arest] Fix name for sensor and shorten logger messages (#5460)

* Fix name for sensor and shorten logger messages

* Use variable as name if none is given
This commit is contained in:
Fabian Affolter 2017-01-24 19:54:14 +01:00 committed by GitHub
parent f5062b06a9
commit b2ecaa189a
3 changed files with 36 additions and 51 deletions

View file

@ -30,7 +30,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the aREST binary sensor.""" """Set up the aREST binary sensor."""
resource = config.get(CONF_RESOURCE) resource = config.get(CONF_RESOURCE)
pin = config.get(CONF_PIN) pin = config.get(CONF_PIN)
sensor_class = config.get(CONF_SENSOR_CLASS) sensor_class = config.get(CONF_SENSOR_CLASS)
@ -38,13 +38,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try: try:
response = requests.get(resource, timeout=10).json() response = requests.get(resource, timeout=10).json()
except requests.exceptions.MissingSchema: except requests.exceptions.MissingSchema:
_LOGGER.error('Missing resource or schema in configuration. ' _LOGGER.error("Missing resource or schema in configuration. "
'Add http:// to your URL.') "Add http:// to your URL")
return False return False
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.error('No route to device at %s. ' _LOGGER.error("No route to device at %s", resource)
'Please check the IP address in the configuration file.',
resource)
return False return False
arest = ArestData(resource, pin) arest = ArestData(resource, pin)
@ -67,10 +65,10 @@ class ArestBinarySensor(BinarySensorDevice):
self.update() self.update()
if self._pin is not None: if self._pin is not None:
request = requests.get('{}/mode/{}/i'.format request = requests.get(
(self._resource, self._pin), timeout=10) '{}/mode/{}/i'.format(self._resource, self._pin), timeout=10)
if request.status_code is not 200: if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?") _LOGGER.error("Can't set mode of %s", self._resource)
@property @property
def name(self): def name(self):
@ -109,5 +107,4 @@ class ArestData(object):
self._resource, self._pin), timeout=10) self._resource, self._pin), timeout=10)
self.data = {'state': response.json()['return_value']} self.data = {'state': response.json()['return_value']}
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device '%s'. Is device offline?", _LOGGER.error("No route to device '%s'", self._resource)
self._resource)

View file

@ -45,7 +45,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the aREST sensor.""" """Set up the aREST sensor."""
resource = config.get(CONF_RESOURCE) resource = config.get(CONF_RESOURCE)
var_conf = config.get(CONF_MONITORED_VARIABLES) var_conf = config.get(CONF_MONITORED_VARIABLES)
pins = config.get(CONF_PINS) pins = config.get(CONF_PINS)
@ -54,12 +54,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
response = requests.get(resource, timeout=10).json() response = requests.get(resource, timeout=10).json()
except requests.exceptions.MissingSchema: except requests.exceptions.MissingSchema:
_LOGGER.error("Missing resource or schema in configuration. " _LOGGER.error("Missing resource or schema in configuration. "
"Add http:// to your URL.") "Add http:// to your URL")
return False return False
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device at %s. " _LOGGER.error("No route to device at %s", resource)
"Please check the IP address in the configuration file.",
resource)
return False return False
arest = ArestData(resource) arest = ArestData(resource)
@ -75,7 +73,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try: try:
return value_template.async_render({'value': value}) return value_template.async_render({'value': value})
except TemplateError: except TemplateError:
_LOGGER.exception('Error parsing value') _LOGGER.exception("Error parsing value")
return value return value
return _render return _render
@ -91,7 +89,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
renderer = make_renderer(var_data.get(CONF_VALUE_TEMPLATE)) renderer = make_renderer(var_data.get(CONF_VALUE_TEMPLATE))
dev.append(ArestSensor( dev.append(ArestSensor(
arest, resource, config.get(CONF_NAME, response[CONF_NAME]), arest, resource, config.get(CONF_NAME, response[CONF_NAME]),
variable, variable=variable, var_data.get(CONF_NAME, variable), variable=variable,
unit_of_measurement=var_data.get(CONF_UNIT_OF_MEASUREMENT), unit_of_measurement=var_data.get(CONF_UNIT_OF_MEASUREMENT),
renderer=renderer)) renderer=renderer))
@ -127,7 +125,7 @@ class ArestSensor(Entity):
request = requests.get( 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 is not 200: if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?") _LOGGER.error("Can't set mode of %s", self._resource)
@property @property
def name(self): def name(self):
@ -184,15 +182,11 @@ class ArestData(object):
response = requests.get('{}/analog/{}'.format( response = requests.get('{}/analog/{}'.format(
self._resource, self._pin[1:]), timeout=10) self._resource, self._pin[1:]), timeout=10)
self.data = {'value': response.json()['return_value']} self.data = {'value': response.json()['return_value']}
else:
_LOGGER.error("Wrong pin naming. "
"Please check your configuration file.")
except TypeError: except TypeError:
response = requests.get('{}/digital/{}'.format( response = requests.get('{}/digital/{}'.format(
self._resource, self._pin), timeout=10) self._resource, self._pin), timeout=10)
self.data = {'value': response.json()['return_value']} self.data = {'value': response.json()['return_value']}
self.available = True self.available = True
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device %s. Is device offline?", _LOGGER.error("No route to device %s", self._resource)
self._resource)
self.available = False self.available = False

View file

@ -36,19 +36,17 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the aREST switches.""" """Set up the aREST switches."""
resource = config.get(CONF_RESOURCE) resource = config.get(CONF_RESOURCE)
try: try:
response = requests.get(resource, timeout=10) response = requests.get(resource, timeout=10)
except requests.exceptions.MissingSchema: except requests.exceptions.MissingSchema:
_LOGGER.error("Missing resource or schema in configuration. " _LOGGER.error("Missing resource or schema in configuration. "
"Add http:// to your URL.") "Add http:// to your URL")
return False return False
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.error("No route to device at %s. " _LOGGER.error("No route to device at %s", resource)
"Please check the IP address in the configuration file.",
resource)
return False return False
dev = [] dev = []
@ -105,16 +103,15 @@ class ArestSwitchFunction(ArestSwitchBase):
'{}/{}'.format(self._resource, self._func), timeout=10) '{}/{}'.format(self._resource, self._func), timeout=10)
if request.status_code is not 200: if request.status_code is not 200:
_LOGGER.error("Can't find function. Is device offline?") _LOGGER.error("Can't find function")
return return
try: try:
request.json()['return_value'] request.json()['return_value']
except KeyError: except KeyError:
_LOGGER.error("No return_value received. " _LOGGER.error("No return_value received")
"Is the function name correct.")
except ValueError: except ValueError:
_LOGGER.error("Response invalid. Is the function name correct?") _LOGGER.error("Response invalid")
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
@ -125,8 +122,8 @@ class ArestSwitchFunction(ArestSwitchBase):
if request.status_code == 200: if request.status_code == 200:
self._state = True self._state = True
else: else:
_LOGGER.error("Can't turn on function %s at %s. " _LOGGER.error(
"Is device offline?", self._func, self._resource) "Can't turn on function %s at %s", self._func, self._resource)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
@ -137,19 +134,18 @@ class ArestSwitchFunction(ArestSwitchBase):
if request.status_code == 200: if request.status_code == 200:
self._state = False self._state = False
else: else:
_LOGGER.error("Can't turn off function %s at %s. " _LOGGER.error(
"Is device offline?", self._func, self._resource) "Can't turn off function %s at %s", self._func, self._resource)
def update(self): def update(self):
"""Get the latest data from aREST API and update the state.""" """Get the latest data from aREST API and update the state."""
try: try:
request = requests.get('{}/{}'.format(self._resource, request = requests.get(
self._func), timeout=10) '{}/{}'.format(self._resource, self._func), timeout=10)
self._state = request.json()['return_value'] != 0 self._state = request.json()['return_value'] != 0
self._available = True self._available = True
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.warning("No route to device %s. Is device offline?", _LOGGER.warning("No route to device %s", self._resource)
self._resource)
self._available = False self._available = False
@ -164,7 +160,7 @@ class ArestSwitchPin(ArestSwitchBase):
request = requests.get( request = requests.get(
'{}/mode/{}/o'.format(self._resource, self._pin), timeout=10) '{}/mode/{}/o'.format(self._resource, self._pin), timeout=10)
if request.status_code is not 200: if request.status_code is not 200:
_LOGGER.error("Can't set mode. Is device offline?") _LOGGER.error("Can't set mode")
self._available = False self._available = False
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
@ -174,8 +170,8 @@ class ArestSwitchPin(ArestSwitchBase):
if request.status_code == 200: if request.status_code == 200:
self._state = True self._state = True
else: else:
_LOGGER.error("Can't turn on pin %s at %s. Is device offline?", _LOGGER.error(
self._pin, self._resource) "Can't turn on pin %s at %s", self._pin, self._resource)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
@ -184,18 +180,16 @@ class ArestSwitchPin(ArestSwitchBase):
if request.status_code == 200: if request.status_code == 200:
self._state = False self._state = False
else: else:
_LOGGER.error("Can't turn off pin %s at %s. Is device offline?", _LOGGER.error(
self._pin, self._resource) "Can't turn off pin %s at %s", self._pin, self._resource)
def update(self): def update(self):
"""Get the latest data from aREST API and update the state.""" """Get the latest data from aREST API and update the state."""
try: try:
request = requests.get('{}/digital/{}'.format(self._resource, request = requests.get(
self._pin), '{}/digital/{}'.format(self._resource, self._pin), timeout=10)
timeout=10)
self._state = request.json()['return_value'] != 0 self._state = request.json()['return_value'] != 0
self._available = True self._available = True
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.warning("No route to device %s. Is device offline?", _LOGGER.warning("No route to device %s", self._resource)
self._resource)
self._available = False self._available = False