Add async for refresh_tken
This commit is contained in:
@@ -118,6 +118,7 @@ def connect_socket(context):
|
|||||||
socket.subscribe_locked(context.on_lock_callback)
|
socket.subscribe_locked(context.on_lock_callback)
|
||||||
socket.subscribe_device(context.on_device_callback)
|
socket.subscribe_device(context.on_device_callback)
|
||||||
socket.subscribe_position(context.on_position_callback)
|
socket.subscribe_position(context.on_position_callback)
|
||||||
|
socket.subscribe_alarm(context.on_alarm_callback)
|
||||||
|
|
||||||
context.socket = socket
|
context.socket = socket
|
||||||
|
|
||||||
@@ -176,7 +177,8 @@ class GeoRideContext:
|
|||||||
epoch = math.ceil(time.time())
|
epoch = math.ceil(time.time())
|
||||||
if (exp_timestamp - TOKEN_SAFE_DAY) < epoch:
|
if (exp_timestamp - TOKEN_SAFE_DAY) < epoch:
|
||||||
_LOGGER.info("Time reached, renew token")
|
_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 = self._hass.data[DOMAIN]["config"]
|
||||||
config[CONF_TOKEN] = account.auth_token
|
config[CONF_TOKEN] = account.auth_token
|
||||||
self._token = account.auth_token
|
self._token = account.auth_token
|
||||||
@@ -209,7 +211,6 @@ class GeoRideContext:
|
|||||||
_LOGGER.info("Tracker list refresh")
|
_LOGGER.info("Tracker list refresh")
|
||||||
self._georide_trackers = GeoRideApi.get_trackers(self.get_token())
|
self._georide_trackers = GeoRideApi.get_trackers(self.get_token())
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def socket(self):
|
def socket(self):
|
||||||
""" hold the GeoRide socket """
|
""" hold the GeoRide socket """
|
||||||
@@ -239,6 +240,30 @@ class GeoRideContext:
|
|||||||
if tracker.tracker_id == data['trackerId']:
|
if tracker.tracker_id == data['trackerId']:
|
||||||
tracker.status = data['status']
|
tracker.status = data['status']
|
||||||
return
|
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
|
@callback
|
||||||
def on_position_callback(self, data):
|
def on_position_callback(self, data):
|
||||||
@@ -253,5 +278,3 @@ class GeoRideContext:
|
|||||||
tracker.fixtime = data['fixtime']
|
tracker.fixtime = data['fixtime']
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
151
custom_components/georide/binary_sensor.py
Normal file
151
custom_components/georide/binary_sensor.py
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user