Skip to content

Commit 8371464

Browse files
authored
Use Home location for non-GPS tracker without GPS data (pnbruckner#7)
Also round last_seen to nearest second.
1 parent bdc2406 commit 8371464

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

custom_components/composite/device_tracker.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
A Device Tracker platform that combines one or more device trackers.
33
"""
4-
from datetime import datetime
4+
from datetime import datetime, timedelta
55
import logging
66
import threading
77

@@ -28,7 +28,7 @@
2828

2929
_LOGGER = logging.getLogger(__name__)
3030

31-
__version__ = '1.10.1'
31+
__version__ = '1.11.0'
3232

3333
CONF_TIME_AS = 'time_as'
3434
CONF_REQ_MOVEMENT = 'require_movement'
@@ -71,6 +71,12 @@ def setup_scanner(hass, config, see, discovery_info=None):
7171
return True
7272

7373

74+
def nearest_second(time):
75+
"""Round time to nearest second."""
76+
return (time.replace(microsecond=0) +
77+
timedelta(seconds=0 if time.microsecond < 500000 else 1))
78+
79+
7480
class CompositeScanner:
7581
def __init__(self, hass, config, see):
7682
self._hass = hass
@@ -271,6 +277,15 @@ def _update_info(self, entity_id, old_state, new_state):
271277
# use it and make source_type gps.
272278
elif gps:
273279
source_type = SOURCE_TYPE_GPS
280+
# Otherwise, if new state is 'home' and old state is not 'home'
281+
# and no GPS data, then use HA's configured Home location and
282+
# make source_type gps.
283+
elif state == STATE_HOME and cur_state.state != STATE_HOME:
284+
gps = (
285+
self._hass.config.latitude,
286+
self._hass.config.longitude)
287+
gps_accuracy = 0
288+
source_type = SOURCE_TYPE_GPS
274289
# Otherwise, don't use any GPS data, but set location_name to
275290
# new state.
276291
else:
@@ -311,8 +326,7 @@ def _update_info(self, entity_id, old_state, new_state):
311326
if entity[ATTR_SOURCE_TYPE] is not None),
312327
ATTR_LAST_ENTITY_ID: entity_id,
313328
ATTR_LAST_SEEN:
314-
self._dt_attr_from_utc(last_seen.replace(microsecond=0),
315-
tz)
329+
self._dt_attr_from_utc(nearest_second(last_seen), tz)
316330
})
317331
if charging is not None:
318332
attrs[ATTR_BATTERY_CHARGING] = charging

0 commit comments

Comments
 (0)