Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bc3da8f0e5 | |||
| ecf2a23c70 | |||
| 991370fcec |
@@ -62,25 +62,13 @@ async def async_setup(hass, config):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Return boolean to indicate that initialization was successful.
|
# Return boolean to indicate that initialization was successful.
|
||||||
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,6 +82,8 @@ 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
|
||||||
|
|
||||||
@@ -108,12 +98,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 +115,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,6 +140,7 @@ 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
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hass(self):
|
def hass(self):
|
||||||
@@ -191,6 +192,11 @@ 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"""
|
||||||
|
if not self._thread_started:
|
||||||
|
_LOGGER.info("Satr the thread")
|
||||||
|
self._hass.async_add_executor_job(connect_socket, self)
|
||||||
|
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
|
||||||
@@ -206,7 +212,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"""
|
||||||
|
|||||||
@@ -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