Handle traccar connection errors (#23289)
* 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
This commit is contained in:
parent
5b0ee473b6
commit
2871a650f6
3 changed files with 21 additions and 9 deletions
|
@ -109,26 +109,38 @@ class TraccarScanner:
|
||||||
self._scan_interval = scan_interval
|
self._scan_interval = scan_interval
|
||||||
self._async_see = async_see
|
self._async_see = async_see
|
||||||
self._api = api
|
self._api = api
|
||||||
|
self.connected = False
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
|
||||||
async def async_init(self):
|
async def async_init(self):
|
||||||
"""Further initialize connection to Traccar."""
|
"""Further initialize connection to Traccar."""
|
||||||
await self._api.test_connection()
|
await self._api.test_connection()
|
||||||
if self._api.authenticated:
|
if self._api.connected and not self._api.authenticated:
|
||||||
await self._async_update()
|
_LOGGER.error("Authentication for Traccar failed")
|
||||||
async_track_time_interval(self._hass,
|
return False
|
||||||
self._async_update,
|
|
||||||
self._scan_interval)
|
|
||||||
|
|
||||||
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):
|
async def _async_update(self, now=None):
|
||||||
"""Update info from Traccar."""
|
"""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)
|
await self._api.get_device_info(self._custom_attributes)
|
||||||
self._hass.async_create_task(self.import_device_data())
|
self._hass.async_create_task(self.import_device_data())
|
||||||
if self._event_types:
|
if self._event_types:
|
||||||
self._hass.async_create_task(self.import_events())
|
self._hass.async_create_task(self.import_events())
|
||||||
|
self.connected = self._api.connected
|
||||||
|
|
||||||
async def import_device_data(self):
|
async def import_device_data(self):
|
||||||
"""Import device data from Traccar."""
|
"""Import device data from Traccar."""
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"name": "Traccar",
|
"name": "Traccar",
|
||||||
"documentation": "https://www.home-assistant.io/components/traccar",
|
"documentation": "https://www.home-assistant.io/components/traccar",
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"pytraccar==0.7.0",
|
"pytraccar==0.8.0",
|
||||||
"stringcase==1.2.0"
|
"stringcase==1.2.0"
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
|
|
|
@ -1432,7 +1432,7 @@ pytile==2.0.6
|
||||||
pytouchline==0.7
|
pytouchline==0.7
|
||||||
|
|
||||||
# homeassistant.components.traccar
|
# homeassistant.components.traccar
|
||||||
pytraccar==0.7.0
|
pytraccar==0.8.0
|
||||||
|
|
||||||
# homeassistant.components.trackr
|
# homeassistant.components.trackr
|
||||||
pytrackr==0.0.5
|
pytrackr==0.0.5
|
||||||
|
|
Loading…
Add table
Reference in a new issue