mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Improved exception handling. Don't stop updating from server because of exception. (#3724)
This commit is contained in:
parent
f1e5d32ef5
commit
fccc7e69d0
@ -39,28 +39,46 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
|
|
||||||
def setup_scanner(hass, config, see):
|
def setup_scanner(hass, config, see):
|
||||||
"""Validate the configuration and return a scanner."""
|
"""Validate the configuration and return a scanner."""
|
||||||
username = config.get(CONF_USERNAME)
|
session = requests.Session()
|
||||||
password = config.get(CONF_PASSWORD)
|
session.headers.update(HEADERS)
|
||||||
|
session.auth = (config.get(CONF_USERNAME),
|
||||||
|
config.get(CONF_PASSWORD))
|
||||||
|
|
||||||
interval = max(MIN_TIME_BETWEEN_SCANS.seconds,
|
interval = max(MIN_TIME_BETWEEN_SCANS.seconds,
|
||||||
config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
|
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):
|
def query(ref, rel=SERVICE_URL):
|
||||||
"""Perform a query to the online service."""
|
"""Perform a query to the online service."""
|
||||||
url = urljoin(rel, ref)
|
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:
|
try:
|
||||||
_LOGGER.debug("Request for %s", url)
|
_LOGGER.debug("Updating")
|
||||||
res = session.get(url)
|
status = query("status", vehicle_url)
|
||||||
res.raise_for_status()
|
position = query("position", vehicle_url)
|
||||||
_LOGGER.debug("Received %s", res.json())
|
see(dev_id=dev_id,
|
||||||
return res.json()
|
host_name=host_name,
|
||||||
except requests.exceptions.RequestException:
|
gps=(position["position"]["latitude"],
|
||||||
_LOGGER.exception("Could not make query to %s", url)
|
position["position"]["longitude"]),
|
||||||
raise
|
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:
|
try:
|
||||||
_LOGGER.info('Logging in to service')
|
_LOGGER.info('Logging in to service')
|
||||||
@ -73,33 +91,10 @@ def setup_scanner(hass, config, see):
|
|||||||
host_name = "%s %s/%s" % (attributes["registrationNumber"],
|
host_name = "%s %s/%s" % (attributes["registrationNumber"],
|
||||||
attributes["vehicleType"],
|
attributes["vehicleType"],
|
||||||
attributes["modelYear"])
|
attributes["modelYear"])
|
||||||
except requests.exceptions.RequestException:
|
update(utcnow())
|
||||||
|
return True
|
||||||
|
except requests.exceptions.RequestException as error:
|
||||||
_LOGGER.error("Could not log in to service. "
|
_LOGGER.error("Could not log in to service. "
|
||||||
"Please check configuration.")
|
"Please check configuration: "
|
||||||
|
"%s", error)
|
||||||
return False
|
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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user