Skip automatic events older than latest data (#9230)
* Skip automatic events older than latest data * Update test
This commit is contained in:
parent
60342b4738
commit
7d281fd224
2 changed files with 25 additions and 8 deletions
|
@ -205,6 +205,7 @@ class AutomaticData(object):
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.devices = devices
|
self.devices = devices
|
||||||
self.vehicle_info = {}
|
self.vehicle_info = {}
|
||||||
|
self.vehicle_seen = {}
|
||||||
self.client = client
|
self.client = client
|
||||||
self.session = session
|
self.session = session
|
||||||
self.async_see = async_see
|
self.async_see = async_see
|
||||||
|
@ -236,6 +237,14 @@ class AutomaticData(object):
|
||||||
return
|
return
|
||||||
yield from self.get_vehicle_info(vehicle)
|
yield from self.get_vehicle_info(vehicle)
|
||||||
|
|
||||||
|
if event.created_at < self.vehicle_seen[event.vehicle.id]:
|
||||||
|
# Skip events received out of order
|
||||||
|
_LOGGER.debug("Skipping out of order event. Event Created %s. "
|
||||||
|
"Last seen event: %s.", event.created_at,
|
||||||
|
self.vehicle_seen[event.vehicle.id])
|
||||||
|
return
|
||||||
|
self.vehicle_seen[event.vehicle.id] = event.created_at
|
||||||
|
|
||||||
kwargs = self.vehicle_info[event.vehicle.id]
|
kwargs = self.vehicle_info[event.vehicle.id]
|
||||||
if kwargs is None:
|
if kwargs is None:
|
||||||
# Ignored device
|
# Ignored device
|
||||||
|
@ -323,15 +332,17 @@ class AutomaticData(object):
|
||||||
if self.devices is not None and name not in self.devices:
|
if self.devices is not None and name not in self.devices:
|
||||||
self.vehicle_info[vehicle.id] = None
|
self.vehicle_info[vehicle.id] = None
|
||||||
return
|
return
|
||||||
else:
|
|
||||||
self.vehicle_info[vehicle.id] = kwargs = {
|
self.vehicle_info[vehicle.id] = kwargs = {
|
||||||
ATTR_DEV_ID: vehicle.id,
|
ATTR_DEV_ID: vehicle.id,
|
||||||
ATTR_HOST_NAME: name,
|
ATTR_HOST_NAME: name,
|
||||||
ATTR_MAC: vehicle.id,
|
ATTR_MAC: vehicle.id,
|
||||||
ATTR_ATTRIBUTES: {
|
ATTR_ATTRIBUTES: {
|
||||||
ATTR_FUEL_LEVEL: vehicle.fuel_level_percent,
|
ATTR_FUEL_LEVEL: vehicle.fuel_level_percent,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
self.vehicle_seen[vehicle.id] = \
|
||||||
|
vehicle.updated_at or vehicle.created_at
|
||||||
|
|
||||||
if vehicle.latest_location is not None:
|
if vehicle.latest_location is not None:
|
||||||
location = vehicle.latest_location
|
location = vehicle.latest_location
|
||||||
|
@ -352,4 +363,7 @@ class AutomaticData(object):
|
||||||
kwargs[ATTR_GPS] = (location.lat, location.lon)
|
kwargs[ATTR_GPS] = (location.lat, location.lon)
|
||||||
kwargs[ATTR_GPS_ACCURACY] = location.accuracy_m
|
kwargs[ATTR_GPS_ACCURACY] = location.accuracy_m
|
||||||
|
|
||||||
|
if trips[0].ended_at >= self.vehicle_seen[vehicle.id]:
|
||||||
|
self.vehicle_seen[vehicle.id] = trips[0].ended_at
|
||||||
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
"""Test the automatic device tracker platform."""
|
"""Test the automatic device tracker platform."""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
from unittest.mock import patch, MagicMock
|
from unittest.mock import patch, MagicMock
|
||||||
import aioautomatic
|
import aioautomatic
|
||||||
|
@ -71,10 +72,12 @@ def test_valid_credentials(
|
||||||
vehicle.display_name = 'mock_display_name'
|
vehicle.display_name = 'mock_display_name'
|
||||||
vehicle.fuel_level_percent = 45.6
|
vehicle.fuel_level_percent = 45.6
|
||||||
vehicle.latest_location = None
|
vehicle.latest_location = None
|
||||||
|
vehicle.updated_at = datetime(2017, 8, 13, 1, 2, 3)
|
||||||
|
|
||||||
trip.end_location.lat = 45.567
|
trip.end_location.lat = 45.567
|
||||||
trip.end_location.lon = 34.345
|
trip.end_location.lon = 34.345
|
||||||
trip.end_location.accuracy_m = 5.6
|
trip.end_location.accuracy_m = 5.6
|
||||||
|
trip.ended_at = datetime(2017, 8, 13, 1, 2, 4)
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def get_session(*args, **kwargs):
|
def get_session(*args, **kwargs):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue