mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Add new checks and move var check to setup
This commit is contained in:
parent
7e066e11ad
commit
b0441aadc4
@ -38,6 +38,7 @@ https://home-assistant.io/components/sensor.rest.html
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from requests import get, exceptions
|
from requests import get, exceptions
|
||||||
|
from json import loads
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
@ -59,13 +60,28 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
response = get(resource, timeout=10)
|
response = get(resource, timeout=10)
|
||||||
|
if not response.ok:
|
||||||
|
_LOGGER.error('Response status is "%s"', response.status_code)
|
||||||
except exceptions.MissingSchema:
|
except 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 exceptions.ConnectionError:
|
except exceptions.ConnectionError:
|
||||||
_LOGGER.error("No route to resource/endpoint. "
|
_LOGGER.error('No route to resource/endpoint. '
|
||||||
"Please check the URL in the configuration file.")
|
'Please check the URL in the configuration file.')
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = loads(response.text)
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.error('No valid JSON in the response in: %s', data)
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
data[config.get('variable')]
|
||||||
|
except KeyError:
|
||||||
|
_LOGGER.error('Variable "%s" not found in response: "%s"',
|
||||||
|
config.get('variable'), data)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
rest = RestData(resource)
|
rest = RestData(resource)
|
||||||
@ -110,12 +126,7 @@ class RestSensor(Entity):
|
|||||||
if 'error' in value:
|
if 'error' in value:
|
||||||
self._state = value['error']
|
self._state = value['error']
|
||||||
else:
|
else:
|
||||||
try:
|
self._state = value[self._variable]
|
||||||
self._state = value[self._variable]
|
|
||||||
except KeyError:
|
|
||||||
_LOGGER.error('Variable "%s" not found in response: "%s".',
|
|
||||||
self._variable, value)
|
|
||||||
self._state = 'N/A'
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
|
Loading…
x
Reference in New Issue
Block a user