Skip to content

Commit

Permalink
Handle traccar connection errors (home-assistant#23289)
Browse files Browse the repository at this point in the history
* Handle connection errors

* Fix lint issue E127

* Remove periods from logs

* Merge connection checks

* Fail with bad credentials

* Move stuff around for async_init

* Fix E128 linting issue

* Simplify
  • Loading branch information
ludeeus authored and cgarwood committed Apr 23, 2019
1 parent 5b0ee47 commit 2871a65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions homeassistant/components/traccar/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,38 @@ def __init__(self, api, hass, async_see, scan_interval,
self._scan_interval = scan_interval
self._async_see = async_see
self._api = api
self.connected = False
self._hass = hass

async def async_init(self):
"""Further initialize connection to Traccar."""
await self._api.test_connection()
if self._api.authenticated:
await self._async_update()
async_track_time_interval(self._hass,
self._async_update,
self._scan_interval)
if self._api.connected and not self._api.authenticated:
_LOGGER.error("Authentication for Traccar failed")
return False

return self._api.authenticated
await self._async_update()
async_track_time_interval(self._hass,
self._async_update,
self._scan_interval)
return True

async def _async_update(self, now=None):
"""Update info from Traccar."""
_LOGGER.debug('Updating device data.')
if not self.connected:
_LOGGER.debug('Testing connection to Traccar')
await self._api.test_connection()
self.connected = self._api.connected
if self.connected:
_LOGGER.info("Connection to Traccar restored")
else:
return
_LOGGER.debug('Updating device data')
await self._api.get_device_info(self._custom_attributes)
self._hass.async_create_task(self.import_device_data())
if self._event_types:
self._hass.async_create_task(self.import_events())
self.connected = self._api.connected

async def import_device_data(self):
"""Import device data from Traccar."""
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/traccar/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Traccar",
"documentation": "https://www.home-assistant.io/components/traccar",
"requirements": [
"pytraccar==0.7.0",
"pytraccar==0.8.0",
"stringcase==1.2.0"
],
"dependencies": [],
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ pytile==2.0.6
pytouchline==0.7

# homeassistant.components.traccar
pytraccar==0.7.0
pytraccar==0.8.0

# homeassistant.components.trackr
pytrackr==0.0.5
Expand Down

0 comments on commit 2871a65

Please sign in to comment.