Skip to content

Commit

Permalink
Request (#9)
Browse files Browse the repository at this point in the history
* Remove unnecessary request, update logging

* Automated changes
  • Loading branch information
Danielhiversen authored Oct 18, 2020
1 parent 79840bd commit 4424113
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions custom_components/tractive/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio
import json
import logging
from datetime import timedelta
from datetime import datetime, timedelta

import aiohttp
import async_timeout
Expand Down Expand Up @@ -159,33 +159,15 @@ async def _fetch_tracker_data(self, tracker_id):
except asyncio.TimeoutError:
pass

try:
with async_timeout.timeout(self._timeout):
resp = await self._session.get(
f"{API_URL}/tracker/{tracker_id}/positions/range", headers=headers
)
if resp.status != 200:
_LOGGER.error(
"Error connecting to Tractive, resp code: %s %s",
resp.status,
resp.reason,
)
return [], None
result = await resp.json()
except aiohttp.ClientError as err:
_LOGGER.error("Error connecting to Tractives: %s ", err, exc_info=True)
raise
except asyncio.TimeoutError:
return [], None

time_from = max(result["first"], result["last"] - 3600 * 24)
now = datetime.timestamp(datetime.now())
time_from = now - 3600 * 6

try:
with async_timeout.timeout(self._timeout):
resp = await self._session.get(
f"{API_URL}/tracker/{tracker_id}//positions?"
f"time_from={time_from}&"
f"time_to={result['last']}&"
f"time_to={now}&"
f"format=json_segments",
headers=headers,
)
Expand Down Expand Up @@ -230,8 +212,10 @@ async def _async_update(self, now=None):
hw_report.pop("_id")
hw_report.pop("_type")
hw_report.pop("report_id")
hw_report.pop("time")
point.update(hw_report)
_LOGGER.debug("point data %s", point)
point["time"] = datetime.fromtimestamp(point["time"])
_LOGGER.debug("point data %s (%s, %s)", point, latitude, longitude)
await self.async_see(
dev_id=tracker_id,
source_type=point.pop("sensor_used"),
Expand Down

0 comments on commit 4424113

Please sign in to comment.