From a5309cf62f2446ae51ff3e8785a969d333954fb4 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Sun, 27 Oct 2019 12:06:49 +0100 Subject: [PATCH] add suport of device_tracker --- .../georide/.translations/en.json | 2 +- custom_components/georide/__init__.py | 11 +- custom_components/georide/device_tracker.py | 117 ++++++++++++++++++ custom_components/georide/strings.json | 2 +- 4 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 custom_components/georide/device_tracker.py diff --git a/custom_components/georide/.translations/en.json b/custom_components/georide/.translations/en.json index bcf76b3..8331263 100644 --- a/custom_components/georide/.translations/en.json +++ b/custom_components/georide/.translations/en.json @@ -17,7 +17,7 @@ }, "create_entry": { - "default": "\n\nLogin succes ({auth_token})" + "default": "\n\nLogin succes" } } } \ No newline at end of file diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index ba57c93..7743236 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -69,15 +69,18 @@ async def async_setup_entry(hass, entry): account.auth_token ) - entry["description_placeholders"] = { - "auth_token": account.auth_token or "null" - } - hass.data[DOMAIN]["context"] = context hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "device_tracker")) return True +async def async_unload_entry(hass, entry): + """Unload an Georide config entry.""" + await hass.config_entries.async_forward_entry_unload(entry, "device_tracker") + hass.data[DOMAIN]["unsub"]() + + return True + class GeorideContext: """Hold the current Georide context.""" diff --git a/custom_components/georide/device_tracker.py b/custom_components/georide/device_tracker.py new file mode 100644 index 0000000..8651b1e --- /dev/null +++ b/custom_components/georide/device_tracker.py @@ -0,0 +1,117 @@ +""" device tracker for Georide object """ + +import logging + +from homeassistant.core import callback +from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE +from homeassistant.components.device_tracker.const import ENTITY_ID_FORMAT, SOURCE_TYPE_GPS +from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.components.device_tracker.config_entry import TrackerEntity + +import georideapilib.api as GeorideApi + +from .const import ( + CONF_TOKEN +) + +from . import DOMAIN as GEORIDE_DOMAIN + + +_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.""" + + georide_context = hass.data[GEORIDE_DOMAIN]["context"] + + if georide_context.token is None: + return False + + _LOGGER.info('Current georide token: %s', georide_context.token) + + """TODO: add here token expiration control""" + + trackers = GeorideApi.get_trackers(georide_context.token) + + + tracker_entities = [] + for tracker in trackers: + entity = GeorideTrackerEntity(tracker.tracker_id, tracker.tracker_name, data=tracker) + hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity + tracker_entities.append(entity) + + async_add_entities(tracker_entities) + + return True + + +class GeorideTrackerEntity(TrackerEntity, RestoreEntity): + """Represent a tracked device.""" + + def __init__(self, tracker_id, name, data=None): + """Set up Georide entity.""" + self._tracker_id = tracker_id + self._name = name + self._data = data or {} + self.entity_id = ENTITY_ID_FORMAT.format(tracker_id) + + @property + def unique_id(self): + """Return the unique ID.""" + return self._tracker_id + + @property + def name(self): + return self._name + + @property + def latitude(self): + """Return latitude value of the device.""" + if self._data.latitude: + return self._data.latitude + + return None + + @property + def longitude(self): + """Return longitude value of the device.""" + if self._data.longitude: + return self._data.longitude + + return None + + @property + def source_type(self): + """Return the source type, eg gps or router, of the device.""" + return SOURCE_TYPE_GPS + + @property + def device_info(self): + """Return the device info.""" + return {"name": self.name, "identifiers": {(GEORIDE_DOMAIN, self._tracker_id)}} + + async def async_added_to_hass(self): + """Call when entity about to be added to Home Assistant.""" + await super().async_added_to_hass() + + # Don't restore if we got set up with data. + if self._data: + return + + state = await self.async_get_last_state() + + if state is None: + return + + attr = state.attributes + self._data = { + "host_name": state.name, + "gps": (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE)), + } + + @callback + def update_data(self, data): + """Mark the device as seen.""" + self._data = data + self.async_write_ha_state() + diff --git a/custom_components/georide/strings.json b/custom_components/georide/strings.json index bcf76b3..8331263 100644 --- a/custom_components/georide/strings.json +++ b/custom_components/georide/strings.json @@ -17,7 +17,7 @@ }, "create_entry": { - "default": "\n\nLogin succes ({auth_token})" + "default": "\n\nLogin succes" } } } \ No newline at end of file