Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved exception handling. Don't stop updating from server because … #3724

Merged
merged 1 commit into from
Oct 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions homeassistant/components/device_tracker/volvooncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,46 @@

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)
_LOGGER.debug("Request for %s", url)
res = session.get(url, timeout=15)
res.raise_for_status()
_LOGGER.debug("Received %s", res.json())
return res.json()

def update(now):
"""Update status from the online service."""
try:
_LOGGER.debug("Request for %s", url)
res = session.get(url)
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
_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"],
position["position"]["longitude"]),
attributes=dict(
tank_volume=attributes["fuelTankVolume"],
washer_fluid=status["washerFluidLevel"],
brake_fluid=status["brakeFluid"],
service_warning=status["serviceWarningStatus"],
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')
Expand All @@ -73,33 +91,10 @@ def query(ref, rel=SERVICE_URL):
host_name = "%s %s/%s" % (attributes["registrationNumber"],
attributes["vehicleType"],
attributes["modelYear"])
except requests.exceptions.RequestException:
update(utcnow())
return True
except requests.exceptions.RequestException as error:
_LOGGER.error("Could not log in to service. "
"Please check configuration.")
"Please check configuration: "
"%s", error)
return False

def update(now):
"""Update status from the online service."""
_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"],
position["position"]["longitude"]),
attributes=dict(
tank_volume=attributes["fuelTankVolume"],
washer_fluid=status["washerFluidLevel"],
brake_fluid=status["brakeFluid"],
service_warning=status["serviceWarningStatus"],
fuel=status["fuelAmount"],
odometer=status["odometer"],
range=status["distanceToEmpty"]))

track_point_in_utc_time(hass, update,
now + timedelta(seconds=interval))

update(utcnow())
return True