From a78b5413b145437d50add5201f10ad81bb334a98 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Sat, 26 Oct 2019 14:48:00 +0200 Subject: [PATCH] Prepare fist 'runnable' version --- .gitignore | 5 + .travis.yml | 3 + README.md | 4 +- custom_components/georide/__init__.py | 118 ++++++++++++++------ custom_components/georide/device_tracker.py | 16 --- custom_components/georide/manifest.json | 13 +++ hacs.json | 4 +- 7 files changed, 107 insertions(+), 56 deletions(-) create mode 100644 .travis.yml delete mode 100644 custom_components/georide/device_tracker.py create mode 100644 custom_components/georide/manifest.json diff --git a/.gitignore b/.gitignore index e69de29..304cbaa 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,5 @@ +#python venv +lib64/ +bin/ +include/ +lib/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8f52fc0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: python +python: + - "3.7" diff --git a/README.md b/README.md index 3f43c9d..588849c 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,6 @@ ⚠️ this is not an official implementation -Official georide website: https://georide.fr/ \ No newline at end of file +Official georide website: https://georide.fr/ + + diff --git a/custom_components/georide/__init__.py b/custom_components/georide/__init__.py index ad4bb94..de03e3c 100644 --- a/custom_components/georide/__init__.py +++ b/custom_components/georide/__init__.py @@ -1,72 +1,116 @@ +""" georide custom conpennt """ +from collections import defaultdict + from georideapilib.objects import GeorideAccount import georideapilib.api as GeorideApi + +import logging import voluptuous as vol from homeassistant import config_entries +from homeassistant.const import CONF_WEBHOOK_ID +from homeassistant.core import callback +import homeassistant.helpers.config_validation as cv +from homeassistant.setup import async_when_setup +_LOGGER = logging.getLogger(__name__) DOMAIN = "georide" CONF_EMAIL = "email" CONF_PASSWORD = "password" +TRACKER_ID = "trackerId" CONFIG_SCHEMA = vol.Schema( { vol.Required(DOMAIN, default={}): { - vol.Required(CONF_EMAIL): vol.All(str, Length(min=3)), + vol.Required(CONF_EMAIL): vol.All(str, vol.Length(min=3)), vol.Required(CONF_PASSWORD): vol.All(str) } }, extra=vol.ALLOW_EXTRA, ) -async def async_setup(hass, config): - """Initialize OwnTracks component.""" + +def setup(hass, config): + """Initialize Georide component.""" hass.data[DOMAIN] = {"config": config[DOMAIN], "devices": {}, "unsub": None} - if not hass.config_entries.async_entries(DOMAIN): - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={} - ) + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data={} ) + ) + + _LOGGER.info("Georide-setup ") + + # Return boolean to indicate that initialization was successful. return True -async def async_setup_entry(hass, entry): - """Set up Georide entry.""" - config = hass.data[DOMAIN]["config"] - email = config.get(CONF_EMAIL) - password = config.get(CONF_PASSWORD) - trackerId = config.get(CONF_TRACKER_ID) - token = config.get(CONF_TOKEN) - - context = GeorideContext( - hass, - email, - password, - trackerId, - token - ) - webhook_id = config.get(CONF_WEBHOOK_ID) or entry.data[CONF_WEBHOOK_ID] - return True + class GeorideContext: """Hold the current Georide context.""" - def __init__( - hass, - email, - password, - trackerId, - token - ): + def __init__(self, hass, email, password): """Initialize an Georide context.""" - self.hass = hass - self.email = email - self.password = password - self.trackerId = trackerId - self.token = token + self._hass = hass + self._email = email + self._password = password + self._georide_trackers = defaultdict(set) + self._token = None + + @property + def hass(self): + """ hass """ + return self._hass + + @property + def email(self): + """ current email """ + return self._email + + @property + def password(self): + """ password """ + return self._password + + @property + def token(self): + """ current jwt token """ + return self._token + + @property + def georide_trackers(self): + """ georide tracker list """ + return self._georide_trackers + + @callback + def async_see_beacons(self, hass, dev_id, kwargs_param): + """Set active beacons to the current location.""" + kwargs = kwargs_param.copy() + + # 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 + + + diff --git a/custom_components/georide/device_tracker.py b/custom_components/georide/device_tracker.py deleted file mode 100644 index 5d4e791..0000000 --- a/custom_components/georide/device_tracker.py +++ /dev/null @@ -1,16 +0,0 @@ -""" device tracker for Georide object """ -import logging - -from homeassistant.core import callback -from homeassistant.const import ( - ATTR_GPS_ACCURACY, - ATTR_LATITUDE, - ATTR_LONGITUDE, - ATTR_BATTERY_LEVEL, -) - -from homeassistant.components.device_tracker.const import ( - ENTITY_ID_FORMAT, - ATTR_SOURCE_TYPE, - SOURCE_TYPE_GPS, -) diff --git a/custom_components/georide/manifest.json b/custom_components/georide/manifest.json new file mode 100644 index 0000000..fd82b24 --- /dev/null +++ b/custom_components/georide/manifest.json @@ -0,0 +1,13 @@ +{ + "domain": "georide", + "name": "Georide", + "config_flow": false, + "documentation": "https://git.tontontux.fr/mduval/GeorideHA", + "requirements": [ + "georideapilib==0.1.0" + ], + "dependencies": [ + "urllib3", + ], + "codeowners": ["ptimatth"] +} \ No newline at end of file diff --git a/hacs.json b/hacs.json index f178b1b..fe7f203 100644 --- a/hacs.json +++ b/hacs.json @@ -1,7 +1,7 @@ { "name": "Georide integration", - "content_in_root": true, - "rener_readme": true, + "content_in_root": false, + "render_readme": true, "domains": ["devices_tracker", "sensor"], "country": ["FR"] } \ No newline at end of file