|
|
@ -11,11 +11,10 @@ import voluptuous as vol
|
|
|
|
import jwt
|
|
|
|
import jwt
|
|
|
|
|
|
|
|
|
|
|
|
from aiohttp.web import json_response
|
|
|
|
from aiohttp.web import json_response
|
|
|
|
from georideapilib.objects import GeorideAccount
|
|
|
|
from georideapilib.objects import GeorideAccount as GeoRideAccount
|
|
|
|
import georideapilib.api as GeorideApi
|
|
|
|
import georideapilib.api as GeoRideApi
|
|
|
|
|
|
|
|
|
|
|
|
from georideapilib.socket import GeorideSocket
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from georideapilib.socket import GeorideSocket as GeoRideSocket
|
|
|
|
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_WEBHOOK_ID
|
|
|
|
from homeassistant.const import CONF_WEBHOOK_ID
|
|
|
@ -51,7 +50,7 @@ CONFIG_SCHEMA = vol.Schema(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Setup Georide component."""
|
|
|
|
"""Setup GeoRide component."""
|
|
|
|
hass.data[DOMAIN] = {"config": config[DOMAIN], "devices": {}, "unsub": None}
|
|
|
|
hass.data[DOMAIN] = {"config": config[DOMAIN], "devices": {}, "unsub": None}
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
@ -68,12 +67,12 @@ 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."""
|
|
|
|
config = hass.data[DOMAIN]["config"]
|
|
|
|
config = hass.data[DOMAIN]["config"]
|
|
|
|
email = config.get(CONF_EMAIL) or entry.data[CONF_EMAIL]
|
|
|
|
email = config.get(CONF_EMAIL) or entry.data[CONF_EMAIL]
|
|
|
|
password = config.get(CONF_PASSWORD) or entry.data[CONF_PASSWORD]
|
|
|
|
password = config.get(CONF_PASSWORD) or entry.data[CONF_PASSWORD]
|
|
|
|
token = config.get(CONF_TOKEN) or entry.data[CONF_TOKEN]
|
|
|
|
token = config.get(CONF_TOKEN) or entry.data[CONF_TOKEN]
|
|
|
|
context = GeorideContext(
|
|
|
|
context = GeoRideContext(
|
|
|
|
hass,
|
|
|
|
hass,
|
|
|
|
email,
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
password,
|
|
|
@ -99,7 +98,7 @@ async def async_setup_entry(hass, entry):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload an Georide config entry."""
|
|
|
|
"""Unload an GeoRide config entry."""
|
|
|
|
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, "device_tracker")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, "device_tracker")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, "switch")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, "switch")
|
|
|
@ -113,9 +112,9 @@ async def async_unload_entry(hass, entry):
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def connect_socket(context):
|
|
|
|
def connect_socket(context):
|
|
|
|
"""subscribe to georide socket"""
|
|
|
|
"""subscribe to GeoRide socket"""
|
|
|
|
_LOGGER.info("Georide socket connexion")
|
|
|
|
_LOGGER.info("GeoRide socket connexion")
|
|
|
|
socket = GeorideSocket()
|
|
|
|
socket = GeoRideSocket()
|
|
|
|
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)
|
|
|
@ -126,11 +125,11 @@ def connect_socket(context):
|
|
|
|
socket.connect(context.get_token())
|
|
|
|
socket.connect(context.get_token())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GeorideContext:
|
|
|
|
class GeoRideContext:
|
|
|
|
"""Hold the current Georide context."""
|
|
|
|
"""Hold the current GeoRide context."""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, hass, email, password, token):
|
|
|
|
def __init__(self, hass, email, password, token):
|
|
|
|
"""Initialize an Georide context."""
|
|
|
|
"""Initialize an GeoRide context."""
|
|
|
|
self._hass = hass
|
|
|
|
self._hass = hass
|
|
|
|
self._email = email
|
|
|
|
self._email = email
|
|
|
|
self._password = password
|
|
|
|
self._password = password
|
|
|
@ -161,12 +160,12 @@ class GeorideContext:
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def georide_trackers(self):
|
|
|
|
def georide_trackers(self):
|
|
|
|
""" georide tracker list """
|
|
|
|
""" GeoRide tracker list """
|
|
|
|
return self._georide_trackers
|
|
|
|
return self._georide_trackers
|
|
|
|
|
|
|
|
|
|
|
|
@georide_trackers.setter
|
|
|
|
@georide_trackers.setter
|
|
|
|
def georide_trackers(self, trackers):
|
|
|
|
def georide_trackers(self, trackers):
|
|
|
|
""" georide tracker list """
|
|
|
|
""" GeoRide tracker list """
|
|
|
|
self._georide_trackers = trackers
|
|
|
|
self._georide_trackers = trackers
|
|
|
|
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
def get_token(self):
|
|
|
@ -177,7 +176,7 @@ 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 = 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
|
|
|
@ -208,17 +207,17 @@ class GeorideContext:
|
|
|
|
def refresh_trackers(self):
|
|
|
|
def refresh_trackers(self):
|
|
|
|
"""Used to refresh the tracker list"""
|
|
|
|
"""Used to refresh the tracker list"""
|
|
|
|
_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 """
|
|
|
|
return self._socket
|
|
|
|
return self._socket
|
|
|
|
|
|
|
|
|
|
|
|
@socket.setter
|
|
|
|
@socket.setter
|
|
|
|
def socket(self, socket):
|
|
|
|
def socket(self, socket):
|
|
|
|
"""set the georide socket"""
|
|
|
|
"""set the GeoRide socket"""
|
|
|
|
self._socket = socket
|
|
|
|
self._socket = socket
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@callback
|
|
|
|