From 3c6f39f7ba8f24cd7f5652d7779886da40e0f6fd Mon Sep 17 00:00:00 2001 From: Matthieu Date: Sat, 10 Apr 2021 11:34:39 +0200 Subject: [PATCH] Add async for refresh_tken --- custom_components/georide/__init__.py | 31 ++++- custom_components/georide/binary_sensor.py | 151 +++++++++++++++++++++ 2 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 custom_components/georide/binary_sensor.py diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index 1e6e968..0f6457f 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -118,6 +118,7 @@ def connect_socket(context): socket.subscribe_locked(context.on_lock_callback) socket.subscribe_device(context.on_device_callback) socket.subscribe_position(context.on_position_callback) + socket.subscribe_alarm(context.on_alarm_callback) context.socket = socket @@ -176,7 +177,8 @@ class GeoRideContext: epoch = math.ceil(time.time()) if (exp_timestamp - TOKEN_SAFE_DAY) < epoch: _LOGGER.info("Time reached, renew token") - account = GeoRideApi.get_authorisation_token(self._email, self._password) + + account = await self._hass.async_add_executor_job(GeoRideApi.get_authorisation_token, [self._email, self._password]) config = self._hass.data[DOMAIN]["config"] config[CONF_TOKEN] = account.auth_token self._token = account.auth_token @@ -209,7 +211,6 @@ class GeoRideContext: _LOGGER.info("Tracker list refresh") self._georide_trackers = GeoRideApi.get_trackers(self.get_token()) - @property def socket(self): """ hold the GeoRide socket """ @@ -239,6 +240,30 @@ class GeoRideContext: if tracker.tracker_id == data['trackerId']: tracker.status = data['status'] return + @callback + def on_alarm_callback(self, data): + """on device callback""" + _LOGGER.info("On alarm received") + for tracker in self._georide_trackers: + if tracker.tracker_id == data['trackerId']: + if data.name == 'vibration': + _LOGGER.info("Vibration detected") + elif data.name == 'exitZone': + _LOGGER.info("Exit zone detected") + elif data.name == 'crash': + _LOGGER.info("Crash detected") + elif data.name == 'crashParking': + _LOGGER.info("Crash parking detected") + elif data.name == 'deviceOffline': + _LOGGER.info("Device offline detected") + elif data.name == 'deviceOnline': + _LOGGER.info("Device online detected") + elif data.name == 'powerCut': + _LOGGER.info("powerCut detected") + else: + _LOGGER.warning("Unamanged alarm: ", data.name) + + return @callback def on_position_callback(self, data): @@ -253,5 +278,3 @@ class GeoRideContext: tracker.fixtime = data['fixtime'] return - - diff --git a/custom_components/georide/binary_sensor.py b/custom_components/georide/binary_sensor.py new file mode 100644 index 0000000..58032d5 --- /dev/null +++ b/custom_components/georide/binary_sensor.py @@ -0,0 +1,151 @@ +""" odometter sensor for GeoRide object """ + +import logging + +from homeassistant.core import callback +from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.components.binary_sensor import ENTITY_ID_FORMAT + +import georideapilib.api as GeoRideApi + +from .const 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.get_token() is None: + return False + + trackers = GeoRideApi.get_trackers(georide_context.get_token()) + + binary_sensor_entities = [] + for tracker in trackers: + stolen_entity = GeoRideStolenBinarySensorEntity(tracker.tracker_id, georide_context.get_token, + georide_context.get_tracker, data=tracker) + hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = stolen_entity + crashed_entity = GeoRideCrashedBinarySensorEntity(tracker.tracker_id, georide_context.get_token, + georide_context.get_tracker, data=tracker) + hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = crashed_entity + binary_sensor_entities.append(stolen_entity) + binary_sensor_entities.append(crashed_entity) + async_add_entities(binary_sensor_entities) + + return True + +class GeoRideStolenBinarySensorEntity(BinarySensorEntity): + """Represent a tracked device.""" + + def __init__(self, tracker_id, get_token_callback, get_tracker_callback, data): + """Set up Georide entity.""" + self._tracker_id = tracker_id + self._data = data or {} + self._get_token_callback = get_token_callback + self._get_tracker_callback = get_tracker_callback + self._name = data.tracker_name + + self.entity_id = ENTITY_ID_FORMAT.format("is_stolen") + "." + str(tracker_id) + self._state = 0 + + + def update(self): + """ update the current tracker""" + _LOGGER.info('update') + self._data = self._get_tracker_callback(self._tracker_id) + self._name = self._data.tracker_name + self._state = self._data.is_stolen + + @property + def unique_id(self): + """Return the unique ID.""" + return self._tracker_id + + @property + def name(self): + """ GeoRide odometer name """ + return self._name + + @property + def state(self): + return self._state + + @property + def get_token_callback(self): + """ 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 device_info(self): + """Return the device info.""" + return { + "name": self.name, + "identifiers": {(GEORIDE_DOMAIN, self._tracker_id)}, + "manufacturer": "GeoRide" + } + + +class GeoRideCrashedBinarySensorEntity(BinarySensorEntity): + """Represent a tracked device.""" + + def __init__(self, tracker_id, get_token_callback, get_tracker_callback, data): + """Set up Georide entity.""" + self._tracker_id = tracker_id + self._data = data or {} + self._get_token_callback = get_token_callback + self._get_tracker_callback = get_tracker_callback + self._name = data.tracker_name + + self.entity_id = ENTITY_ID_FORMAT.format("is_crashed") + "." + str(tracker_id) + self._state = 0 + + + def update(self): + """ update the current tracker""" + _LOGGER.info('update') + self._data = self._get_tracker_callback(self._tracker_id) + self._name = self._data.tracker_name + self._state = self._data.is_crashed + + @property + def unique_id(self): + """Return the unique ID.""" + return self._tracker_id + + @property + def name(self): + """ GeoRide odometer name """ + return self._name + + @property + def state(self): + return self._state + + @property + def get_token_callback(self): + """ 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 device_info(self): + """Return the device info.""" + return { + "name": self.name, + "identifiers": {(GEORIDE_DOMAIN, self._tracker_id)}, + "manufacturer": "GeoRide" + } + +