Add event emitter interval

master
Matthieu DUVAL 5 years ago
parent 55a0f08a0a
commit 7047ef71ae

@ -1,16 +1,20 @@
""" georide custom conpennt """ """ georide custom conpennt """
from collections import defaultdict from collections import defaultdict
import logging
from datetime import timedelta
import voluptuous as vol
from georideapilib.objects import GeorideAccount from georideapilib.objects import GeorideAccount
import georideapilib.api as GeorideApi import georideapilib.api as GeorideApi
import logging
import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_WEBHOOK_ID from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.core import callback from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import homeassistant.helpers.event as ha_event
from homeassistant.setup import async_when_setup from homeassistant.setup import async_when_setup
from .const import ( from .const import (
@ -47,6 +51,28 @@ async def async_setup(hass, config):
_LOGGER.info("Georide-setup ") _LOGGER.info("Georide-setup ")
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)
_LOGGER.info('Georide Trackers event %s', trackers)
georide_context.georide_trackers = trackers
hass.bus.fire(DOMAIN + '_trackers_update', {
'trackers': trackers
})
ha_event.async_track_time_interval(hass, georide_update, timedelta(seconds=30))
# Return boolean to indicate that initialization was successful. # Return boolean to indicate that initialization was successful.
return True return True
@ -70,6 +96,11 @@ async def async_setup_entry(hass, entry):
) )
hass.data[DOMAIN]["context"] = context hass.data[DOMAIN]["context"] = context
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, "device_tracker"))
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "switch")) hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, "switch"))
@ -123,12 +154,12 @@ class GeorideContext:
""" georide tracker list """ """ georide tracker list """
return self._georide_trackers return self._georide_trackers
@georide_trackers.setter
def georide_trackers(self, trackers):
""" georide tracker list """
self._georide_trackers = trackers
@callback @callback
def async_get_token(self): def async_get_token(self):
""" here we return the current valid tocken, TODO: add here token expiration control""" """ here we return the current valid tocken, TODO: add here token expiration control"""
return self._token return self._token
async def async_update(self):
""" update the current tracker"""
_LOGGER.info('async_update ')

@ -81,7 +81,12 @@ class GeorideTrackerEntity(TrackerEntity):
@property @property
def location_accuracy(self): def location_accuracy(self):
""" return the gps accuracy of georide (could not be aquired, then 10) """ """ return the gps accuracy of georide (could not be aquired, then 10) """
return 10 return 20
@property
def icon(self):
return "mdi:map-marker"
@property @property
def device_info(self): def device_info(self):

@ -2,6 +2,7 @@
import logging import logging
from homeassistant.core import callback
from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import SwitchDevice
from homeassistant.components.switch import ENTITY_ID_FORMAT from homeassistant.components.switch import ENTITY_ID_FORMAT
@ -17,11 +18,31 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
georide_context = hass.data[GEORIDE_DOMAIN]["context"] georide_context = hass.data[GEORIDE_DOMAIN]["context"]
if georide_context.token is None:
def handle_event(event):
"""Receive tracker update."""
nonlocal hass
_LOGGER.info('event recieve')
trackers = event.data.get('trackers')
# for tracker in trackers:
# entity = hass.data[GEORIDE_DOMAIN]["devices"].get(tracker.tracker_id)
# if entity is not None:
# entity.update_data(tracker)
# else:
# entity = GeorideLockSwitchEntity(tracker.tracker_id, tracker.tracker_name,
# georide_context.async_get_token, data=tracker)
# async_add_entities([entity])
#hass.bus.listen(GEORIDE_DOMAIN + '_trackers_update', handle_event)
if georide_context.async_get_token() is None:
return False return False
_LOGGER.info('Current georide token: %s', georide_context.async_get_token())
_LOGGER.info('Current georide token: %s', georide_context.async_get_token())
trackers = GeorideApi.get_trackers(georide_context.async_get_token()) trackers = GeorideApi.get_trackers(georide_context.async_get_token())
@ -37,15 +58,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
return True return True
class GeorideLockSwitchEntity(SwitchDevice): class GeorideLockSwitchEntity(SwitchDevice):
"""Represent a tracked device.""" """Represent a tracked device."""
def __init__(self, tracker_id, name, token_callback, data): def __init__(self, tracker_id, name, get_token_callback, data):
"""Set up Georide entity.""" """Set up Georide entity."""
self._tracker_id = tracker_id self._tracker_id = tracker_id
self._name = name self._name = name
self._data = data or {} self._data = data or {}
self._token_callback = token_callback self._get_token_callback = get_token_callback
self._is_on = data.is_locked self._is_on = data.is_locked
self.entity_id = ENTITY_ID_FORMAT.format("lock."+str(tracker_id)) self.entity_id = ENTITY_ID_FORMAT.format("lock."+str(tracker_id))
@ -53,25 +75,27 @@ class GeorideLockSwitchEntity(SwitchDevice):
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
""" lock the georide tracker """ """ lock the georide tracker """
_LOGGER.info('async_turn_on %s', kwargs) _LOGGER.info('async_turn_on %s', kwargs)
success = GeorideApi.lock_tracker(self.token_callback(), self._tracker_id) success = GeorideApi.lock_tracker(self._get_token_callback(), self._tracker_id)
if success: if success:
self._is_on = True self._is_on = True
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs):
""" unlock the georide tracker """ """ unlock the georide tracker """
_LOGGER.info('async_turn_off %s', kwargs) _LOGGER.info('async_turn_off %s', kwargs)
success = GeorideApi.unlock_tracker(self.token_callback(), self._tracker_id) success = GeorideApi.unlock_tracker(self._get_token_callback(), self._tracker_id)
if success: if success:
self._is_on = False self._is_on = False
async def async_toggle(self, **kwargs): async def async_toggle(self, **kwargs):
""" toggle lock the georide tracker """ """ toggle lock the georide tracker """
_LOGGER.info('async_toggle %s', kwargs) _LOGGER.info('async_toggle %s', kwargs)
self._is_on = GeorideApi.toogle_lock_tracker(self.token_callback(), self._tracker_id) self._is_on = GeorideApi.toogle_lock_tracker(self._get_token_callback(),
self._tracker_id)
async def async_update(self): async def async_update(self):
""" update the current tracker""" """ update the current tracker"""
_LOGGER.info('async_update ') _LOGGER.info('async_update ')
return
@property @property
@ -84,15 +108,16 @@ class GeorideLockSwitchEntity(SwitchDevice):
""" Georide switch name """ """ Georide switch name """
return self._name return self._name
@property @property
def is_on(self): def is_on(self):
""" Georide switch status """ """ Georide switch status """
return self._is_on return self._is_on
@property @property
def token_callback(self): def get_token_callback(self):
""" Georide switch token callback method """ """ Georide switch token callback method """
return self._token_callback return self._get_token_callback
@property @property
def icon(self): def icon(self):

Loading…
Cancel
Save