Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ef783db79b | |||
| 18de851e4d | |||
|
|
cbed2cebb3 | ||
| e5bd90ae6f | |||
| a2e65694fc | |||
| 66bd335a8e | |||
| 14300c5278 | |||
| bc3da8f0e5 | |||
| ecf2a23c70 | |||
| 991370fcec |
@@ -1,9 +1,8 @@
|
|||||||
# Georide Home assistant
|
# Georide Home assistant
|
||||||

|

|
||||||
|
|
||||||
⚠️ This is not an official implementation
|
⚠️ This is not an official implementation
|
||||||
|
[](https://github.com/custom-components/hacs)
|
||||||
[](https://github.com/custom-components/hacs)
|
|
||||||
[](https://www.gnu.org/licenses/gpl-3.0)
|
[](https://www.gnu.org/licenses/gpl-3.0)
|
||||||
|
|
||||||
Official GeoRide website: https://georide.fr/
|
Official GeoRide website: https://georide.fr/
|
||||||
|
|||||||
@@ -16,10 +16,10 @@
|
|||||||
"unknown": "Unknown error"
|
"unknown": "Unknown error"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"one_instance_allowed": "Only a single instance is allowed."
|
"one_instance_allowed": "Only one instance is allowed."
|
||||||
},
|
},
|
||||||
"create_entry": {
|
"create_entry": {
|
||||||
"default": "\n\nLogin succes"
|
"default": "\n\nLogin success"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
"step": {
|
"step": {
|
||||||
"georide_login": {
|
"georide_login": {
|
||||||
"title": "Configuration de GeoRide",
|
"title": "Configuration de GeoRide",
|
||||||
"description": "T'es un mortard ! V",
|
"description": "T'es un motard ! V",
|
||||||
"data": {
|
"data": {
|
||||||
"email": "email",
|
"email": "email",
|
||||||
"password": "password"
|
"password": "password"
|
||||||
@@ -12,11 +12,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"faulty_credentials": "Connexion \u00e9chou\u00e9",
|
"faulty_credentials": "Connexion \u00e9chou\u00e9e",
|
||||||
"unkonwn": "Erreur inconue"
|
"unkonwn": "Erreur inconnue"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"one_instance_allowed": "Seulement un instance est authoris\u00e9e"
|
"one_instance_allowed": "Seulement une instance est autoris\u00e9e"
|
||||||
},
|
},
|
||||||
"create_entry": {
|
"create_entry": {
|
||||||
"default": "\n\nConnexion r\u00e9ussie !"
|
"default": "\n\nConnexion r\u00e9ussie !"
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ from .const import (
|
|||||||
CONF_TOKEN,
|
CONF_TOKEN,
|
||||||
TRACKER_ID,
|
TRACKER_ID,
|
||||||
TOKEN_SAFE_DAY,
|
TOKEN_SAFE_DAY,
|
||||||
|
MIN_UNTIL_REFRESH,
|
||||||
DOMAIN
|
DOMAIN
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -66,21 +67,6 @@ async def async_setup(hass, config):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def connect_socket(hass, component):
|
|
||||||
"""subscribe to georide socket"""
|
|
||||||
context = hass.data[DOMAIN]["context"]
|
|
||||||
|
|
||||||
socket = GeorideSocket()
|
|
||||||
socket.subscribe_locked(context.on_lock_callback)
|
|
||||||
socket.subscribe_device(context.on_device_callback)
|
|
||||||
socket.subscribe_position(context.on_position_callback)
|
|
||||||
|
|
||||||
context.socket = socket
|
|
||||||
|
|
||||||
socket.init()
|
|
||||||
socket.connect(context.async_get_token())
|
|
||||||
|
|
||||||
|
|
||||||
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"]
|
||||||
@@ -94,12 +80,13 @@ async def async_setup_entry(hass, entry):
|
|||||||
token
|
token
|
||||||
)
|
)
|
||||||
|
|
||||||
|
_LOGGER.info("Context-setup and start the thread")
|
||||||
|
_LOGGER.info("Thread started")
|
||||||
|
|
||||||
hass.data[DOMAIN]["context"] = context
|
hass.data[DOMAIN]["context"] = context
|
||||||
|
|
||||||
# We add trackers to the context
|
# We add trackers to the context
|
||||||
trackers = GeorideApi.get_trackers(token)
|
context.refresh_trackers()
|
||||||
context.georide_trackers = trackers
|
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.async_forward_entry_setup(entry, "device_tracker"))
|
hass.config_entries.async_forward_entry_setup(entry, "device_tracker"))
|
||||||
@@ -108,12 +95,9 @@ async def async_setup_entry(hass, entry):
|
|||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.async_forward_entry_setup(entry, "sensor"))
|
hass.config_entries.async_forward_entry_setup(entry, "sensor"))
|
||||||
|
|
||||||
thread = Thread(target=connect_socket, args=(hass, entry))
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass, entry):
|
async def async_unload_entry(hass, entry):
|
||||||
"""Unload an Georide config entry."""
|
"""Unload an Georide config entry."""
|
||||||
|
|
||||||
@@ -128,6 +112,19 @@ async def async_unload_entry(hass, entry):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def connect_socket(context):
|
||||||
|
"""subscribe to georide socket"""
|
||||||
|
_LOGGER.info("Georide socket connexion")
|
||||||
|
socket = GeorideSocket()
|
||||||
|
socket.subscribe_locked(context.on_lock_callback)
|
||||||
|
socket.subscribe_device(context.on_device_callback)
|
||||||
|
socket.subscribe_position(context.on_position_callback)
|
||||||
|
|
||||||
|
context.socket = socket
|
||||||
|
|
||||||
|
socket.init()
|
||||||
|
socket.connect(context.get_token())
|
||||||
|
|
||||||
|
|
||||||
class GeorideContext:
|
class GeorideContext:
|
||||||
"""Hold the current Georide context."""
|
"""Hold the current Georide context."""
|
||||||
@@ -140,7 +137,8 @@ class GeorideContext:
|
|||||||
self._georide_trackers = defaultdict(set)
|
self._georide_trackers = defaultdict(set)
|
||||||
self._token = token
|
self._token = token
|
||||||
self._socket = None
|
self._socket = None
|
||||||
|
self._thread_started = False
|
||||||
|
self._previous_refresh = math.floor(time.time()/60)
|
||||||
@property
|
@property
|
||||||
def hass(self):
|
def hass(self):
|
||||||
""" hass """
|
""" hass """
|
||||||
@@ -177,7 +175,6 @@ class GeorideContext:
|
|||||||
exp_timestamp = jwt_data['exp']
|
exp_timestamp = jwt_data['exp']
|
||||||
|
|
||||||
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)
|
||||||
@@ -191,11 +188,29 @@ class GeorideContext:
|
|||||||
|
|
||||||
def get_tracker(self, tracker_id):
|
def get_tracker(self, tracker_id):
|
||||||
""" here we return last tracker by id"""
|
""" here we return last tracker by id"""
|
||||||
|
epoch_min = math.floor(time.time()/60)
|
||||||
|
if (epoch_min % MIN_UNTIL_REFRESH) == 0:
|
||||||
|
if epoch_min != self._previous_refresh:
|
||||||
|
self._previous_refresh = epoch_min
|
||||||
|
self.refresh_trackers()
|
||||||
|
|
||||||
|
if not self._thread_started:
|
||||||
|
_LOGGER.info("Start the thread")
|
||||||
|
self._hass.async_add_executor_job(connect_socket, self)
|
||||||
|
# We refresh the tracker list each hours
|
||||||
|
self._thread_started = True
|
||||||
|
|
||||||
for tracker in self._georide_trackers:
|
for tracker in self._georide_trackers:
|
||||||
if tracker.tracker_id == tracker_id:
|
if tracker.tracker_id == tracker_id:
|
||||||
return tracker
|
return tracker
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def refresh_trackers(self):
|
||||||
|
"""Used to refresh the tracker list"""
|
||||||
|
_LOGGER.info("Tracker list refresh")
|
||||||
|
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 """
|
||||||
@@ -206,7 +221,6 @@ class GeorideContext:
|
|||||||
"""set the georide socket"""
|
"""set the georide socket"""
|
||||||
self._socket = socket
|
self._socket = socket
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def on_lock_callback(self, data):
|
def on_lock_callback(self, data):
|
||||||
"""on lock callback"""
|
"""on lock callback"""
|
||||||
|
|||||||
@@ -41,27 +41,32 @@ class GeorideConfigFlow(config_entries.ConfigFlow):
|
|||||||
|
|
||||||
async def async_step_georide_login(self, user_input):
|
async def async_step_georide_login(self, user_input):
|
||||||
""" try to seupt GeoRide Account """
|
""" try to seupt GeoRide Account """
|
||||||
errors = {}
|
|
||||||
|
schema = vol.Schema({
|
||||||
|
vol.Required(CONF_EMAIL): vol.All(str, vol.Length(min=3)),
|
||||||
|
vol.Required(CONF_PASSWORD): vol.All(str)
|
||||||
|
})
|
||||||
|
|
||||||
|
if user_input is None:
|
||||||
|
return self.async_show_form(step_id='georide_login', data_schema=schema)
|
||||||
|
|
||||||
|
email = user_input[CONF_EMAIL]
|
||||||
|
password = user_input[CONF_PASSWORD]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
account = GeorideApi.get_authorisation_token(
|
account = GeorideApi.get_authorisation_token(email, password)
|
||||||
user_input[CONF_EMAIL],
|
|
||||||
user_input[CONF_PASSWORD])
|
|
||||||
return self.async_create_entry(
|
|
||||||
title=user_input[CONF_EMAIL],
|
|
||||||
data = {
|
data = {
|
||||||
CONF_EMAIL: user_input[CONF_EMAIL],
|
CONF_EMAIL: email,
|
||||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
CONF_PASSWORD: password,
|
||||||
CONF_TOKEN: account.auth_token
|
CONF_TOKEN: account.auth_token
|
||||||
}
|
}
|
||||||
)
|
return self.async_create_entry(title=email, data=data)
|
||||||
except (GeorideException.SeverException, GeorideException.LoginException):
|
except (GeorideException.SeverException, GeorideException.LoginException):
|
||||||
_LOGGER.error("Invalid credentials provided, config not created")
|
_LOGGER.error("Invalid credentials provided, config not created")
|
||||||
errors["base"] = "faulty_credentials"
|
errors = {"base": "faulty_credentials"}
|
||||||
return self.async_show_form(step_id="georide_login", errors=errors)
|
return self.async_show_form(step_id="georide_login", data_schema=schema, errors=errors)
|
||||||
except:
|
except:
|
||||||
_LOGGER.error("Unknown error")
|
_LOGGER.error("Unknown error")
|
||||||
errors["base"] = "faulty_credentials"
|
errors = {"base": "unkonwn"}
|
||||||
return self.async_show_form(step_id="georide_login", errors=errors)
|
return self.async_show_form(step_id="georide_login", data_schema=schema, errors=errors)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -8,4 +8,6 @@ CONF_TOKEN = "token"
|
|||||||
|
|
||||||
TRACKER_ID = "trackerId"
|
TRACKER_ID = "trackerId"
|
||||||
|
|
||||||
|
MIN_UNTIL_REFRESH = 10
|
||||||
|
|
||||||
TOKEN_SAFE_DAY = 432000 # five days
|
TOKEN_SAFE_DAY = 432000 # five days
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.device_tracker.const import ENTITY_ID_FORMAT, SOURCE_TYPE_GPS
|
from homeassistant.components.device_tracker.const import DOMAIN, SOURCE_TYPE_GPS
|
||||||
from homeassistant.components.device_tracker.config_entry import TrackerEntity
|
from homeassistant.components.device_tracker.config_entry import TrackerEntity
|
||||||
|
|
||||||
import georideapilib.api as GeorideApi
|
import georideapilib.api as GeorideApi
|
||||||
@@ -50,7 +50,7 @@ class GeorideTrackerEntity(TrackerEntity):
|
|||||||
self._get_tracker_callback = get_tracker_callback
|
self._get_tracker_callback = get_tracker_callback
|
||||||
self._name = tracker.tracker_name
|
self._name = tracker.tracker_name
|
||||||
self._data = tracker or {}
|
self._data = tracker or {}
|
||||||
self.entity_id = ENTITY_ID_FORMAT.format(tracker_id)
|
self.entity_id = DOMAIN + ".{}".format(tracker_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
@@ -117,9 +117,9 @@ class GeorideTrackerEntity(TrackerEntity):
|
|||||||
"""No polling needed."""
|
"""No polling needed."""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def async_update(self):
|
def update(self):
|
||||||
""" update the current tracker"""
|
""" update the current tracker"""
|
||||||
|
_LOGGER.info('update')
|
||||||
self._data = self._get_tracker_callback(self._tracker_id)
|
self._data = self._get_tracker_callback(self._tracker_id)
|
||||||
self._name = self._data.tracker_name
|
self._name = self._data.tracker_name
|
||||||
return
|
|
||||||
|
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
"documentation": "https://git.tontontux.fr/mduval/GeorideHA",
|
"documentation": "https://git.tontontux.fr/mduval/GeorideHA",
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"georideapilib>=0.4.3",
|
"georideapilib>=0.4.4",
|
||||||
"pyjwt>=1.7.1"
|
"pyjwt>=1.7.1"
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class GeorideOdometerSensorEntity(SwitchDevice):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" update the current tracker"""
|
""" update the current tracker"""
|
||||||
_LOGGER.info('async_update ')
|
_LOGGER.info('update')
|
||||||
self._data = self._get_tracker_callback(self._tracker_id)
|
self._data = self._get_tracker_callback(self._tracker_id)
|
||||||
self._name = self._data.tracker_name
|
self._name = self._data.tracker_name
|
||||||
self._state = self._data.odometer
|
self._state = self._data.odometer
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ class GeorideLockSwitchEntity(SwitchDevice):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
""" update the current tracker"""
|
""" update the current tracker"""
|
||||||
_LOGGER.info('async_update ')
|
_LOGGER.info('update')
|
||||||
self._data = self._get_tracker_callback(self._tracker_id)
|
self._data = self._get_tracker_callback(self._tracker_id)
|
||||||
self._name = self._data.tracker_name
|
self._name = self._data.tracker_name
|
||||||
self._is_on = self._data.is_locked
|
self._is_on = self._data.is_locked
|
||||||
|
|||||||
Reference in New Issue
Block a user