Improved exception handling. Don't stop updating from server because of exception. (#3724)

This commit is contained in:
Erik Eriksson 2016-10-08 02:24:02 +02:00 committed by Paulus Schoutsen
parent f1e5d32ef5
commit fccc7e69d0

View file

@ -39,52 +39,29 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_scanner(hass, config, see):
"""Validate the configuration and return a scanner."""
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
session = requests.Session()
session.headers.update(HEADERS)
session.auth = (config.get(CONF_USERNAME),
config.get(CONF_PASSWORD))
interval = max(MIN_TIME_BETWEEN_SCANS.seconds,
config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
session = requests.Session()
session.headers.update(HEADERS)
session.auth = (username, password)
def query(ref, rel=SERVICE_URL):
"""Perform a query to the online service."""
url = urljoin(rel, ref)
try:
_LOGGER.debug("Request for %s", url)
res = session.get(url)
res = session.get(url, timeout=15)
res.raise_for_status()
_LOGGER.debug("Received %s", res.json())
return res.json()
except requests.exceptions.RequestException:
_LOGGER.exception("Could not make query to %s", url)
raise
try:
_LOGGER.info('Logging in to service')
user = query("customeraccounts")
rel = query(user["accountVehicleRelations"][0])
vehicle_url = rel["vehicle"] + '/'
attributes = query("attributes", vehicle_url)
dev_id = "volvo_" + attributes["registrationNumber"]
host_name = "%s %s/%s" % (attributes["registrationNumber"],
attributes["vehicleType"],
attributes["modelYear"])
except requests.exceptions.RequestException:
_LOGGER.error("Could not log in to service. "
"Please check configuration.")
return False
def update(now):
"""Update status from the online service."""
try:
_LOGGER.debug("Updating")
status = query("status", vehicle_url)
position = query("position", vehicle_url)
see(dev_id=dev_id,
host_name=host_name,
gps=(position["position"]["latitude"],
@ -97,9 +74,27 @@ def setup_scanner(hass, config, see):
fuel=status["fuelAmount"],
odometer=status["odometer"],
range=status["distanceToEmpty"]))
except requests.exceptions.RequestException as error:
_LOGGER.error("Could not query server: %s", error)
finally:
track_point_in_utc_time(hass, update,
now + timedelta(seconds=interval))
try:
_LOGGER.info('Logging in to service')
user = query("customeraccounts")
rel = query(user["accountVehicleRelations"][0])
vehicle_url = rel["vehicle"] + '/'
attributes = query("attributes", vehicle_url)
dev_id = "volvo_" + attributes["registrationNumber"]
host_name = "%s %s/%s" % (attributes["registrationNumber"],
attributes["vehicleType"],
attributes["modelYear"])
update(utcnow())
return True
except requests.exceptions.RequestException as error:
_LOGGER.error("Could not log in to service. "
"Please check configuration: "
"%s", error)
return False