Add lock/unlock support
This commit is contained in:
@@ -71,12 +71,17 @@ 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"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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."""
|
||||||
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")
|
||||||
|
|
||||||
hass.data[DOMAIN]["unsub"]()
|
hass.data[DOMAIN]["unsub"]()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@@ -118,29 +123,9 @@ class GeorideContext:
|
|||||||
""" georide tracker list """
|
""" georide tracker list """
|
||||||
return self._georide_trackers
|
return self._georide_trackers
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_see_beacons(self, hass, dev_id, kwargs_param):
|
def async_get_token(self):
|
||||||
"""Set active beacons to the current location."""
|
""" here we return the current valid tocken, TODO: add here token expiration control"""
|
||||||
kwargs = kwargs_param.copy()
|
return self._token
|
||||||
|
|
||||||
# Mobile beacons should always be set to the location of the
|
|
||||||
# tracking device. I get the device state and make the necessary
|
|
||||||
# changes to kwargs.
|
|
||||||
device_tracker_state = hass.states.get(f"device_tracker.{dev_id}")
|
|
||||||
|
|
||||||
if device_tracker_state is not None:
|
|
||||||
lat = device_tracker_state.attributes.get("latitude")
|
|
||||||
lon = device_tracker_state.attributes.get("longitude")
|
|
||||||
|
|
||||||
if lat is not None and lon is not None:
|
|
||||||
kwargs["gps"] = (lat, lon)
|
|
||||||
else:
|
|
||||||
kwargs["gps"] = None
|
|
||||||
|
|
||||||
for tracker in self.georide_trackers[dev_id]:
|
|
||||||
kwargs["dev_id"] = f"{TRACKER_ID}_{tracker}"
|
|
||||||
kwargs["host_name"] = tracker
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,17 +3,11 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
|
||||||
from homeassistant.components.device_tracker.const import ENTITY_ID_FORMAT, SOURCE_TYPE_GPS
|
from homeassistant.components.device_tracker.const import ENTITY_ID_FORMAT, SOURCE_TYPE_GPS
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
|
||||||
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
|
||||||
|
|
||||||
from .const import (
|
|
||||||
CONF_TOKEN
|
|
||||||
)
|
|
||||||
|
|
||||||
from . import DOMAIN as GEORIDE_DOMAIN
|
from . import DOMAIN as GEORIDE_DOMAIN
|
||||||
|
|
||||||
|
|
||||||
@@ -27,11 +21,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
|
|||||||
if georide_context.token is None:
|
if georide_context.token is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
_LOGGER.info('Current georide token: %s', georide_context.token)
|
_LOGGER.info('Current georide token: %s', georide_context.async_get_token())
|
||||||
|
|
||||||
"""TODO: add here token expiration control"""
|
|
||||||
|
|
||||||
trackers = GeorideApi.get_trackers(georide_context.token)
|
trackers = GeorideApi.get_trackers(georide_context.async_get_token())
|
||||||
|
|
||||||
|
|
||||||
tracker_entities = []
|
tracker_entities = []
|
||||||
@@ -45,7 +38,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: d
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class GeorideTrackerEntity(TrackerEntity, RestoreEntity):
|
class GeorideTrackerEntity(TrackerEntity):
|
||||||
"""Represent a tracked device."""
|
"""Represent a tracked device."""
|
||||||
|
|
||||||
def __init__(self, tracker_id, name, data=None):
|
def __init__(self, tracker_id, name, data=None):
|
||||||
@@ -85,30 +78,21 @@ class GeorideTrackerEntity(TrackerEntity, RestoreEntity):
|
|||||||
"""Return the source type, eg gps or router, of the device."""
|
"""Return the source type, eg gps or router, of the device."""
|
||||||
return SOURCE_TYPE_GPS
|
return SOURCE_TYPE_GPS
|
||||||
|
|
||||||
|
@property
|
||||||
|
def location_accuracy(self):
|
||||||
|
""" return the gps accuracy of georide (could not be aquired, then 10) """
|
||||||
|
return 10
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return the device info."""
|
"""Return the device info."""
|
||||||
return {"name": self.name, "identifiers": {(GEORIDE_DOMAIN, self._tracker_id)}}
|
return {
|
||||||
|
"name": self.name,
|
||||||
async def async_added_to_hass(self):
|
"identifiers": {(GEORIDE_DOMAIN, self._tracker_id)},
|
||||||
"""Call when entity about to be added to Home Assistant."""
|
"manufacturer": "GeoRide"
|
||||||
await super().async_added_to_hass()
|
|
||||||
|
|
||||||
# Don't restore if we got set up with data.
|
|
||||||
if self._data:
|
|
||||||
return
|
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
|
||||||
|
|
||||||
if state is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
attr = state.attributes
|
|
||||||
self._data = {
|
|
||||||
"host_name": state.name,
|
|
||||||
"gps": (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_data(self, data):
|
def update_data(self, data):
|
||||||
"""Mark the device as seen."""
|
"""Mark the device as seen."""
|
||||||
|
|||||||
@@ -4,8 +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.2.0",
|
"georideapilib>=0.2.0"
|
||||||
"urllib3>=1.25.6"
|
|
||||||
],
|
],
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": ["@ptimatth"]
|
"codeowners": ["@ptimatth"]
|
||||||
|
|||||||
105
custom_components/georide/switch.py
Normal file
105
custom_components/georide/switch.py
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
""" device tracker for Georide object """
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.components.switch import SwitchDevice
|
||||||
|
from homeassistant.components.switch import ENTITY_ID_FORMAT
|
||||||
|
|
||||||
|
import georideapilib.api as GeorideApi
|
||||||
|
|
||||||
|
from . import DOMAIN as GEORIDE_DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
async def async_setup_entry(hass, config_entry, async_add_entities): # pylint: disable=W0613
|
||||||
|
"""Set up Georide tracker based off an entry."""
|
||||||
|
|
||||||
|
georide_context = hass.data[GEORIDE_DOMAIN]["context"]
|
||||||
|
|
||||||
|
if georide_context.token is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
_LOGGER.info('Current georide token: %s', georide_context.async_get_token())
|
||||||
|
|
||||||
|
trackers = GeorideApi.get_trackers(georide_context.async_get_token())
|
||||||
|
|
||||||
|
|
||||||
|
lock_switch_entities = []
|
||||||
|
for tracker in trackers:
|
||||||
|
entity = GeorideLockSwitchEntity(tracker.tracker_id, tracker.tracker_name,
|
||||||
|
georide_context.async_get_token, data=tracker)
|
||||||
|
hass.data[GEORIDE_DOMAIN]["devices"][tracker.tracker_id] = entity
|
||||||
|
lock_switch_entities.append(entity)
|
||||||
|
|
||||||
|
async_add_entities(lock_switch_entities)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class GeorideLockSwitchEntity(SwitchDevice):
|
||||||
|
"""Represent a tracked device."""
|
||||||
|
|
||||||
|
def __init__(self, tracker_id, name, token_callback, data):
|
||||||
|
"""Set up Georide entity."""
|
||||||
|
self._tracker_id = tracker_id
|
||||||
|
self._name = name
|
||||||
|
self._data = data or {}
|
||||||
|
self._token_callback = token_callback
|
||||||
|
self._is_on = data.is_locked
|
||||||
|
self.entity_id = ENTITY_ID_FORMAT.format("lock."+str(tracker_id))
|
||||||
|
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs):
|
||||||
|
""" lock the georide tracker """
|
||||||
|
_LOGGER.info('async_turn_on %s', kwargs)
|
||||||
|
success = GeorideApi.lock_tracker(self.token_callback(), self._tracker_id)
|
||||||
|
if success:
|
||||||
|
self._is_on = True
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs):
|
||||||
|
""" unlock the georide tracker """
|
||||||
|
_LOGGER.info('async_turn_off %s', kwargs)
|
||||||
|
success = GeorideApi.unlock_tracker(self.token_callback(), self._tracker_id)
|
||||||
|
if success:
|
||||||
|
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.token_callback(), self._tracker_id)
|
||||||
|
|
||||||
|
async def async_update(self):
|
||||||
|
""" update the current tracker"""
|
||||||
|
_LOGGER.info('async_update ')
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return the unique ID."""
|
||||||
|
return self._tracker_id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
""" Georide switch name """
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
""" Georide switch status """
|
||||||
|
return self._is_on
|
||||||
|
|
||||||
|
@property
|
||||||
|
def token_callback(self):
|
||||||
|
""" Georide switch token callback method """
|
||||||
|
return self._token_callback
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self):
|
||||||
|
"""Return the device info."""
|
||||||
|
return {
|
||||||
|
"name": self.name,
|
||||||
|
"identifiers": {(GEORIDE_DOMAIN, self._tracker_id)},
|
||||||
|
"manufacturer": "GeoRide"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user