Add tracker polling support
This commit is contained in:
@@ -52,8 +52,6 @@ async def async_setup(hass, config):
|
|||||||
|
|
||||||
_LOGGER.info("Georide-setup ")
|
_LOGGER.info("Georide-setup ")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Return boolean to indicate that initialization was successful.
|
# Return boolean to indicate that initialization was successful.
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -62,20 +60,14 @@ async def async_setup(hass, config):
|
|||||||
async def async_setup_entry(hass, entry):
|
async def async_setup_entry(hass, entry):
|
||||||
"""Set up Georide entry."""
|
"""Set up Georide entry."""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def georide_update(event):
|
def georide_update(event):
|
||||||
"""Update tracker info"""
|
"""Update tracker info"""
|
||||||
nonlocal hass
|
nonlocal hass
|
||||||
_LOGGER.info('Georide update event %s', event)
|
_LOGGER.info('Georide update event %s', event)
|
||||||
|
|
||||||
georide_context = hass.data[DOMAIN]["context"]
|
georide_context = hass.data[DOMAIN]["context"]
|
||||||
|
|
||||||
token = georide_context.async_get_token()
|
token = georide_context.async_get_token()
|
||||||
trackers = GeorideApi.get_trackers(token)
|
trackers = GeorideApi.get_trackers(token)
|
||||||
georide_context.georide_trackers = trackers
|
georide_context.georide_trackers = trackers
|
||||||
hass.helpers.dispatcher.async_dispatcher_send(DOMAIN, hass, georide_context, json_response([]))
|
|
||||||
|
|
||||||
|
|
||||||
ha_event.async_track_time_interval(hass, georide_update, timedelta(seconds=30))
|
ha_event.async_track_time_interval(hass, georide_update, timedelta(seconds=30))
|
||||||
|
|
||||||
@@ -98,11 +90,12 @@ async def async_setup_entry(hass, entry):
|
|||||||
account.auth_token
|
account.auth_token
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
hass.data[DOMAIN]["context"] = context
|
hass.data[DOMAIN]["context"] = context
|
||||||
|
|
||||||
|
# We add trackers to the context
|
||||||
|
trackers = GeorideApi.get_trackers(account.auth_token)
|
||||||
|
context.georide_trackers = trackers
|
||||||
|
|
||||||
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "device_tracker"))
|
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "device_tracker"))
|
||||||
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "switch"))
|
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "switch"))
|
||||||
@@ -176,7 +169,6 @@ class GeorideContext:
|
|||||||
return tracker
|
return tracker
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_see(self, **data):
|
def async_see(self, **data):
|
||||||
"""Send a see message to the device tracker."""
|
"""Send a see message to the device tracker."""
|
||||||
|
|||||||
@@ -29,7 +29,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
|
|||||||
|
|
||||||
tracker_entities = []
|
tracker_entities = []
|
||||||
for tracker in trackers:
|
for tracker in trackers:
|
||||||
entity = GeorideTrackerEntity(tracker.tracker_id, tracker.tracker_name, data=tracker)
|
entity = GeorideTrackerEntity(tracker.tracker_id, georide_context.async_get_token,
|
||||||
|
georide_context.async_get_tracker, tracker)
|
||||||
|
|
||||||
|
|
||||||
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity
|
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity
|
||||||
tracker_entities.append(entity)
|
tracker_entities.append(entity)
|
||||||
|
|
||||||
@@ -41,11 +44,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
|
|||||||
class GeorideTrackerEntity(TrackerEntity):
|
class GeorideTrackerEntity(TrackerEntity):
|
||||||
"""Represent a tracked device."""
|
"""Represent a tracked device."""
|
||||||
|
|
||||||
def __init__(self, tracker_id, name, data=None):
|
def __init__(self, tracker_id, get_token_callback, get_tracker_callback, tracker):
|
||||||
"""Set up Georide entity."""
|
"""Set up Georide entity."""
|
||||||
self._tracker_id = tracker_id
|
self._tracker_id = tracker_id
|
||||||
self._name = name
|
self._get_token_callback = get_token_callback
|
||||||
self._data = data or {}
|
self._get_tracker_callback = get_tracker_callback
|
||||||
|
self._name = tracker.tracker_name
|
||||||
|
self._data = tracker or {}
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(tracker_id)
|
self.entity_id = ENTITY_ID_FORMAT.format(tracker_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -62,7 +67,6 @@ class GeorideTrackerEntity(TrackerEntity):
|
|||||||
"""Return latitude value of the device."""
|
"""Return latitude value of the device."""
|
||||||
if self._data.latitude:
|
if self._data.latitude:
|
||||||
return self._data.latitude
|
return self._data.latitude
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -98,10 +102,26 @@ class GeorideTrackerEntity(TrackerEntity):
|
|||||||
"odometer": "{} km".format(self._data.odometer)
|
"odometer": "{} km".format(self._data.odometer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_tracker_callback(self):
|
||||||
|
""" get tracker callaback"""
|
||||||
|
return self._get_tracker_callback
|
||||||
|
|
||||||
@callback
|
@property
|
||||||
def update_data(self, data):
|
def get_token_callback(self):
|
||||||
"""Mark the device as seen."""
|
""" get token callaback"""
|
||||||
self._data = data
|
return self._get_token_callback
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def should_poll(self):
|
||||||
|
"""No polling needed."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def async_update(self):
|
||||||
|
""" update the current tracker"""
|
||||||
|
_LOGGER.info('async_update ')
|
||||||
|
self._data = self._get_tracker_callback(self._tracker_id)
|
||||||
|
self._name = self._data.tracker_name
|
||||||
|
return
|
||||||
|
|
||||||
@@ -16,28 +16,8 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613
|
async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613
|
||||||
"""Set up Georide tracker based off an entry."""
|
"""Set up Georide tracker based off an entry."""
|
||||||
|
|
||||||
# @callback
|
|
||||||
# def _receive_data(dev_id, **data):
|
|
||||||
# """Receive set location."""
|
|
||||||
# _LOGGER.info("Datariceived from parrent")
|
|
||||||
#
|
|
||||||
# entity = hass.data[GEORIDE_DOMAIN]["devices"].get(dev_id)
|
|
||||||
#
|
|
||||||
# if entity is not None:
|
|
||||||
# entity.update_data(data)
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# entity = GeorideLockSwitchEntity(dev_id, georide_context.async_get_token, data)
|
|
||||||
# hass.data[GEORIDE_DOMAIN]["devices"][dev_id] = entity
|
|
||||||
# async_add_entities([entity])
|
|
||||||
#
|
|
||||||
# hass.data[GEORIDE_DOMAIN]["context"].set_async_see(_receive_data)
|
|
||||||
|
|
||||||
|
|
||||||
georide_context = hass.data[GEORIDE_DOMAIN]["context"]
|
georide_context = hass.data[GEORIDE_DOMAIN]["context"]
|
||||||
|
|
||||||
|
|
||||||
if georide_context.async_get_token() is None:
|
if georide_context.async_get_token() is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -45,7 +25,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
|
|||||||
_LOGGER.info('Current georide token: %s', georide_context.async_get_token())
|
_LOGGER.info('Current georide token: %s', georide_context.async_get_token())
|
||||||
trackers = GeorideApi.get_trackers(georide_context.async_get_token())
|
trackers = GeorideApi.get_trackers(georide_context.async_get_token())
|
||||||
|
|
||||||
|
|
||||||
lock_switch_entities = []
|
lock_switch_entities = []
|
||||||
for tracker in trackers:
|
for tracker in trackers:
|
||||||
entity = GeorideLockSwitchEntity(tracker.tracker_id, georide_context.async_get_token,
|
entity = GeorideLockSwitchEntity(tracker.tracker_id, georide_context.async_get_token,
|
||||||
@@ -94,6 +73,12 @@ class GeorideLockSwitchEntity(SwitchDevice):
|
|||||||
self._is_on = GeorideApi.toogle_lock_tracker(self._get_token_callback(),
|
self._is_on = GeorideApi.toogle_lock_tracker(self._get_token_callback(),
|
||||||
self._tracker_id)
|
self._tracker_id)
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def should_poll(self):
|
||||||
|
"""No polling needed."""
|
||||||
|
return True
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
""" update the current tracker"""
|
""" update the current tracker"""
|
||||||
_LOGGER.info('async_update ')
|
_LOGGER.info('async_update ')
|
||||||
@@ -102,7 +87,6 @@ class GeorideLockSwitchEntity(SwitchDevice):
|
|||||||
self._is_on = self._data.is_locked
|
self._is_on = self._data.is_locked
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return the unique ID."""
|
"""Return the unique ID."""
|
||||||
@@ -113,7 +97,6 @@ class GeorideLockSwitchEntity(SwitchDevice):
|
|||||||
""" Georide switch name """
|
""" Georide switch name """
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
""" Georide switch status """
|
""" Georide switch status """
|
||||||
@@ -124,6 +107,11 @@ class GeorideLockSwitchEntity(SwitchDevice):
|
|||||||
""" Georide switch token callback method """
|
""" Georide switch token callback method """
|
||||||
return self._get_token_callback
|
return self._get_token_callback
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_tracker_callback(self):
|
||||||
|
""" Georide switch token callback method """
|
||||||
|
return self._get_tracker_callback
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
if self._is_on:
|
if self._is_on:
|
||||||
@@ -141,15 +129,3 @@ class GeorideLockSwitchEntity(SwitchDevice):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No polling needed."""
|
|
||||||
return True
|
|
||||||
|
|
||||||
@callback
|
|
||||||
def update_data(self, data):
|
|
||||||
"""Mark the device as seen."""
|
|
||||||
self._data = data
|
|
||||||
self._name = data.tracker_name
|
|
||||||
self._is_on = data.is_locked
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user