From 7f64e7c7d7c9b0b14ad47184da484f4f668f0e79 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Mon, 28 Oct 2019 01:15:29 +0100 Subject: [PATCH] Add tracker polling support --- custom_components/georide/__init__.py | 16 ++----- custom_components/georide/device_tracker.py | 42 +++++++++++++----- custom_components/georide/switch.py | 48 ++++++--------------- 3 files changed, 47 insertions(+), 59 deletions(-) diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index c6921ac..2dfad34 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -52,8 +52,6 @@ async def async_setup(hass, config): _LOGGER.info("Georide-setup ") - - # Return boolean to indicate that initialization was successful. return True @@ -62,20 +60,14 @@ async def async_setup(hass, config): async def async_setup_entry(hass, entry): """Set up Georide entry.""" - - def georide_update(event): """Update tracker info""" nonlocal hass _LOGGER.info('Georide update event %s', event) - georide_context = hass.data[DOMAIN]["context"] - token = georide_context.async_get_token() trackers = GeorideApi.get_trackers(token) 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)) @@ -98,11 +90,12 @@ async def async_setup_entry(hass, entry): 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, "switch")) @@ -176,7 +169,6 @@ class GeorideContext: return tracker return None - @callback def async_see(self, **data): """Send a see message to the device tracker.""" diff --git a/custom_components/georide/device_tracker.py b/custom_components/georide/device_tracker.py index cd1526d..d4893d0 100644 --- a/custom_components/georide/device_tracker.py +++ b/custom_components/georide/device_tracker.py @@ -29,7 +29,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d tracker_entities = [] 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 tracker_entities.append(entity) @@ -41,11 +44,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d class GeorideTrackerEntity(TrackerEntity): """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.""" self._tracker_id = tracker_id - self._name = name - self._data = data or {} + self._get_token_callback = get_token_callback + 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) @property @@ -62,7 +67,6 @@ class GeorideTrackerEntity(TrackerEntity): """Return latitude value of the device.""" if self._data.latitude: return self._data.latitude - return None @property @@ -98,10 +102,26 @@ class GeorideTrackerEntity(TrackerEntity): "odometer": "{} km".format(self._data.odometer) } + @property + def get_tracker_callback(self): + """ get tracker callaback""" + return self._get_tracker_callback + + @property + def get_token_callback(self): + """ get token callaback""" + return self._get_token_callback + - @callback - def update_data(self, data): - """Mark the device as seen.""" - self._data = data - 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 + \ No newline at end of file diff --git a/custom_components/georide/switch.py b/custom_components/georide/switch.py index 743ca19..49b43a8 100644 --- a/custom_components/georide/switch.py +++ b/custom_components/georide/switch.py @@ -16,27 +16,7 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613 """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: 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()) trackers = GeorideApi.get_trackers(georide_context.async_get_token()) - lock_switch_entities = [] for tracker in trackers: 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._tracker_id) + + @property + def should_poll(self): + """No polling needed.""" + return True + async def async_update(self): """ update the current tracker""" _LOGGER.info('async_update ') @@ -102,7 +87,6 @@ class GeorideLockSwitchEntity(SwitchDevice): self._is_on = self._data.is_locked return - @property def unique_id(self): """Return the unique ID.""" @@ -113,7 +97,6 @@ class GeorideLockSwitchEntity(SwitchDevice): """ Georide switch name """ return self._name - @property def is_on(self): """ Georide switch status """ @@ -124,6 +107,11 @@ class GeorideLockSwitchEntity(SwitchDevice): """ Georide switch token callback method """ return self._get_token_callback + @property + def get_tracker_callback(self): + """ Georide switch token callback method """ + return self._get_tracker_callback + @property def icon(self): 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()