Shipping v0.2.0

master 0.2.0
Matthieu DUVAL 5 years ago
commit b40c41e6b8

@ -4,11 +4,14 @@ from collections import defaultdict
import logging
from datetime import timedelta
import voluptuous as vol
import json
import jwt
from threading import Thread
from aiohttp.web import json_response
from georideapilib.objects import GeorideAccount
import georideapilib.api as GeorideApi
from georideapilib.socket import GeorideSocket
from homeassistant import config_entries
@ -44,68 +47,67 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup(hass, config):
"""Setup Georide component."""
hass.data[DOMAIN] = {"config": config[DOMAIN], "devices": {}, "unsub": None}
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={}
)
_LOGGER.info("Georide-setup ")
_LOGGER.info("Georide-setup success: %s", result)
# Return boolean to indicate that initialization was successful.
return True
def connect_socket(hass, component):
"""subscribe to georide socket"""
context = hass.data[DOMAIN]["context"]
async def async_setup_entry(hass, entry):
"""Set up Georide entry."""
def georide_update(event):
"""Update tracker info"""
nonlocal hass
_LOGGER.info('Georide update event %s', event)
georide_context = hass.data[DOMAIN]["context"]
token = georide_context.async_get_token()
trackers = GeorideApi.get_trackers(token)
georide_context.georide_trackers = trackers
ha_event.async_track_time_interval(hass, georide_update, timedelta(seconds=30))
socket = GeorideSocket()
socket.subscribe_locked(context.on_lock_callback)
socket.subscribe_device(context.on_device_callback)
socket.subscribe_position(context.on_position_callback)
socket.init()
socket.connect(context.async_get_token())
async def async_setup_entry(hass, entry):
"""Set up Georide entry."""
config = hass.data[DOMAIN]["config"]
email = config.get(CONF_EMAIL) or entry.data[CONF_EMAIL]
password = config.get(CONF_PASSWORD) or entry.data[CONF_PASSWORD]
if email is None or password is None:
return False
try:
account = GeorideApi.get_authorisation_token(email, password)
context = GeorideContext(
hass,
email,
password,
account.auth_token
)
account = GeorideApi.get_authorisation_token(email, password)
context = GeorideContext(
hass,
email,
password,
account.auth_token
)
hass.data[DOMAIN]["context"] = context
# We add trackers to the context
trackers = GeorideApi.get_trackers(account.auth_token)
context.georide_trackers = trackers
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "device_tracker"))
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "switch"))
hass.data[DOMAIN]["context"] = context
# We add trackers to the context
trackers = GeorideApi.get_trackers(account.auth_token)
context.georide_trackers = trackers
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "device_tracker"))
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "switch"))
thread = Thread(target=connect_socket, args=(hass, entry))
thread.start()
except:
return False
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")
await hass.config_entries.async_forward_entry_unload(entry, "switch")
@ -154,16 +156,19 @@ class GeorideContext:
@georide_trackers.setter
def georide_trackers(self, trackers):
""" georide tracker list """
self._georide_trackers = trackers
self._georide_trackers = trackers
@callback
def async_get_token(self):
""" here we return the current valid tocken, TODO: add here token expiration control"""
jwt_data = jwt.decode(self._token, verify=False)
exp_timestamp = jwt_data['exp']
_LOGGER.info("Token exp data: %s", exp_timestamp)
return self._token
@callback
def async_get_tracker(self, tracker_id):
""" here we return the current valid tocken, TODO: add here token expiration control"""
""" here we return last tracker by id"""
for tracker in self._georide_trackers:
if tracker.tracker_id == tracker_id:
return tracker
@ -174,3 +179,39 @@ class GeorideContext:
"""Send a see message to the device tracker."""
_LOGGER.info("sync_see")
self._pending_msg.append(data)
@callback
def on_lock_callback(self, data):
"""on lock callback"""
_LOGGER.info("On lock received")
for tracker in self._georide_trackers:
if tracker.tracker_id == data['trackerId']:
tracker.locked_latitude = data['lockedLatitude']
tracker.locked_longitude = data['lockedLongitude']
tracker.is_locked = data['isLocked']
return
@callback
def on_device_callback(self, data):
"""on device callback"""
_LOGGER.info("On device received")
for tracker in self._georide_trackers:
if tracker.tracker_id == data['trackerId']:
tracker.status = data['status']
return
@callback
def on_position_callback(self, data):
"""on position callback"""
_LOGGER.info("On position received")
for tracker in self._georide_trackers:
if tracker.tracker_id == data['trackerId']:
tracker.latitude = data['latitude']
tracker.longitude = data['longitude']
tracker.moving = data['moving']
tracker.speed = data['speed']
tracker.fixtime = data['fixtime']
return

@ -4,7 +4,8 @@
"config_flow": true,
"documentation": "https://git.tontontux.fr/mduval/GeorideHA",
"requirements": [
"georideapilib>=0.2.0"
"georideapilib>=0.4.1",
"pyjwt>=1.7.1"
],
"dependencies": [],
"codeowners": ["@ptimatth"]

@ -53,39 +53,37 @@ class GeorideLockSwitchEntity(SwitchDevice):
self._state = {}
async def async_turn_on(self, **kwargs):
def turn_on(self, **kwargs):
""" lock the georide tracker """
_LOGGER.info('async_turn_on %s', kwargs)
success = GeorideApi.lock_tracker(self._get_token_callback(), self._tracker_id)
if success:
self._data.is_locked = True
self._is_on = True
async def async_turn_off(self, **kwargs):
def turn_off(self, **kwargs):
""" unlock the georide tracker """
_LOGGER.info('async_turn_off %s', kwargs)
success = GeorideApi.unlock_tracker(self._get_token_callback(), self._tracker_id)
if success:
self._data.is_locked = False
self._is_on = False
async def async_toggle(self, **kwargs):
""" toggle lock the georide tracker """
_LOGGER.info('async_toggle %s', kwargs)
self._is_on = GeorideApi.toogle_lock_tracker(self._get_token_callback(),
self._tracker_id)
result = GeorideApi.toogle_lock_tracker(self._get_token_callback(),
self._tracker_id)
self._data.is_locked = result
self._is_on = result
@property
def should_poll(self):
"""No polling needed."""
return True
async def async_update(self):
def update(self):
""" update the current tracker"""
_LOGGER.info('async_update ')
self._data = self._get_tracker_callback(self._tracker_id)
self._name = self._data.tracker_name
self._is_on = self._data.is_locked
return
@property
def unique_id(self):

Loading…
Cancel
Save