-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Add Traccar device tracker #18200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Traccar device tracker #18200
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """ | ||
| Support for Traccar device tracking. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/device_tracker.traccar/ | ||
| """ | ||
| from datetime import timedelta | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.device_tracker import PLATFORM_SCHEMA | ||
| from homeassistant.const import ( | ||
| CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL, | ||
| CONF_PASSWORD, CONF_USERNAME) | ||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
| from homeassistant.helpers.event import async_track_time_interval | ||
| from homeassistant.util import slugify | ||
|
|
||
|
|
||
| REQUIREMENTS = ['pytraccar==0.1.2'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| ATTR_ADDRESS = 'address' | ||
| ATTR_CATEGORY = 'category' | ||
| ATTR_GEOFENCE = 'geofence' | ||
| ATTR_TRACKER = 'tracker' | ||
|
|
||
| DEFAULT_SCAN_INTERVAL = timedelta(seconds=30) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_HOST): cv.string, | ||
| vol.Optional(CONF_PORT, default=8082): cv.port, | ||
| vol.Optional(CONF_SSL, default=False): cv.boolean, | ||
| vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean, | ||
| }) | ||
|
|
||
|
|
||
| async def async_setup_scanner(hass, config, async_see, discovery_info=None): | ||
| """Validate the configuration and return a Traccar scanner.""" | ||
| from pytraccar.api import API | ||
|
|
||
| session = async_get_clientsession(hass, config[CONF_VERIFY_SSL]) | ||
|
|
||
| api = API(hass.loop, session, config[CONF_USERNAME], config[CONF_PASSWORD], | ||
| config[CONF_HOST], config[CONF_PORT], config[CONF_SSL]) | ||
| scanner = TraccarScanner(api, hass, async_see) | ||
| return await scanner.async_init() | ||
|
|
||
|
|
||
| class TraccarScanner: | ||
| """Define an object to retrieve Traccar data.""" | ||
|
|
||
| def __init__(self, api, hass, async_see): | ||
| """Initialize.""" | ||
| self._async_see = async_see | ||
| self._api = api | ||
| self._hass = hass | ||
|
|
||
| async def async_init(self): | ||
| """Further initialize connection to Traccar.""" | ||
| await self._api.test_connection() | ||
| if self._api.authenticated: | ||
| await self._async_update() | ||
| async_track_time_interval(self._hass, | ||
| self._async_update, | ||
| DEFAULT_SCAN_INTERVAL) | ||
|
|
||
| return self._api.authenticated | ||
|
|
||
| async def _async_update(self, now=None): | ||
| """Update info from Traccar.""" | ||
| _LOGGER.debug('Updating device data.') | ||
| await self._api.get_device_info() | ||
| for devicename in self._api.device_info: | ||
| device = self._api.device_info[devicename] | ||
| device_attributes = { | ||
| ATTR_ADDRESS: device['address'], | ||
| ATTR_GEOFENCE: device['geofence'], | ||
| ATTR_CATEGORY: device['category'], | ||
| ATTR_TRACKER: 'traccar' | ||
| } | ||
| await self._async_see( | ||
| dev_id=slugify(device['device_id']), | ||
| gps=(device['latitude'], device['longitude']), | ||
| attributes=device_attributes) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest that we are using a secured channel for the communication by default, if possible.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible, but I think that would confuse the potential enduser?
Home Assistant itself and most of the components/platforms do have this as false by defaul?
git grep "CONF_SSL, default=True" homeassistant/components/sensor/synologydsm.py: vol.Optional(CONF_SSL, default=True): cv.boolean,git grep "CONF_SSL, default=False" homeassistant/components/binary_sensor/hikvision.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/device_tracker/netgear.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/device_tracker/tomato.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/device_tracker/traccar.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/media_player/epson.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/octoprint.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/sensor/nzbget.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/sensor/pyload.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/sensor/qnap.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/sensor/radarr.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/sensor/sma.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/sensor/sonarr.py: vol.Optional(CONF_SSL, default=False): cv.boolean, homeassistant/components/splunk.py: vol.Optional(CONF_SSL, default=False): cv.boolean,git grep "DEFAULT_SSL =" homeassistant/components/binary_sensor/concord232.py:DEFAULT_SSL = False homeassistant/components/binary_sensor/nx584.py:DEFAULT_SSL = False homeassistant/components/climate/venstar.py:DEFAULT_SSL = False homeassistant/components/device_tracker/ddwrt.py:DEFAULT_SSL = False homeassistant/components/device_tracker/luci.py:DEFAULT_SSL = False homeassistant/components/media_player/emby.py:DEFAULT_SSL = False homeassistant/components/media_player/firetv.py:DEFAULT_SSL = False homeassistant/components/media_player/itunes.py:DEFAULT_SSL = False homeassistant/components/rainmachine/__init__.py:DEFAULT_SSL = True homeassistant/components/sabnzbd.py:DEFAULT_SSL = False homeassistant/components/sensor/influxdb.py:DEFAULT_SSL = False homeassistant/components/sensor/mfi.py:DEFAULT_SSL = True homeassistant/components/sensor/pi_hole.py:DEFAULT_SSL = False homeassistant/components/sensor/plex.py:DEFAULT_SSL = False homeassistant/components/sensor/tautulli.py:DEFAULT_SSL = False homeassistant/components/splunk.py:DEFAULT_SSL = False homeassistant/components/switch/mfi.py:DEFAULT_SSL = True homeassistant/components/zabbix.py:DEFAULT_SSL = False homeassistant/components/zoneminder/__init__.py:DEFAULT_SSL = FalseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not disagree with the suggestion, but to my knowledge, there is not a standard that a component or a platform should have that enabled by default, it may be something to discuss in https://github.com/home-assistant/architecture?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes a difference where the target is located. Devices on the LAN usually do not have valid certificates anyway.
Connections to servers on the internet should default to SSL with verification.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if their paid service is running SSL, but the demo versions are not.
For the local version they do not have built-in support for SSL, you would have to run it behind a proxy to get that for a local instance.
I have used a local instance (hassio add-on) while building and testing this.