Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 110 additions & 1 deletion homeassistant/components/traccar/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,110 @@
"""The traccar component."""
"""Support for Traccar."""
import logging

import voluptuous as vol
from aiohttp import web

import homeassistant.helpers.config_validation as cv
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, HTTP_OK, CONF_WEBHOOK_ID
from homeassistant.helpers import config_entry_flow
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
from .const import (
ATTR_ACCURACY,
ATTR_ALTITUDE,
ATTR_BATTERY,
ATTR_BEARING,
ATTR_ID,
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_SPEED,
ATTR_TIMESTAMP,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

TRACKER_UPDATE = "{}_tracker_update".format(DOMAIN)


DEFAULT_ACCURACY = 200
DEFAULT_BATTERY = -1


def _id(value: str) -> str:
"""Coerce id by removing '-'."""
return value.replace("-", "")


WEBHOOK_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ID): vol.All(cv.string, _id),
vol.Required(ATTR_LATITUDE): cv.latitude,
vol.Required(ATTR_LONGITUDE): cv.longitude,
vol.Optional(ATTR_ACCURACY, default=DEFAULT_ACCURACY): vol.Coerce(float),
vol.Optional(ATTR_ALTITUDE): vol.Coerce(float),
vol.Optional(ATTR_BATTERY, default=DEFAULT_BATTERY): vol.Coerce(float),
vol.Optional(ATTR_BEARING): vol.Coerce(float),
vol.Optional(ATTR_SPEED): vol.Coerce(float),
vol.Optional(ATTR_TIMESTAMP): vol.Coerce(int),
}
)


async def async_setup(hass, hass_config):
"""Set up the Traccar component."""
hass.data[DOMAIN] = {"devices": set(), "unsub_device_tracker": {}}
return True


async def handle_webhook(hass, webhook_id, request):
"""Handle incoming webhook with Traccar request."""
try:
data = WEBHOOK_SCHEMA(dict(request.query))
except vol.MultipleInvalid as error:
return web.Response(text=error.error_message, status=HTTP_UNPROCESSABLE_ENTITY)

attrs = {
ATTR_ALTITUDE: data.get(ATTR_ALTITUDE),
ATTR_BEARING: data.get(ATTR_BEARING),
ATTR_SPEED: data.get(ATTR_SPEED),
}

device = data[ATTR_ID]

async_dispatcher_send(
hass,
TRACKER_UPDATE,
device,
data[ATTR_LATITUDE],
data[ATTR_LONGITUDE],
data[ATTR_BATTERY],
data[ATTR_ACCURACY],
attrs,
)

return web.Response(text="Setting location for {}".format(device), status=HTTP_OK)


async def async_setup_entry(hass, entry):
"""Configure based on config entry."""
hass.components.webhook.async_register(
DOMAIN, "Traccar", entry.data[CONF_WEBHOOK_ID], handle_webhook
)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, DEVICE_TRACKER)
)
return True


async def async_unload_entry(hass, entry):
"""Unload a config entry."""
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
hass.data[DOMAIN]["unsub_device_tracker"].pop(entry.entry_id)()
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
return True


# pylint: disable=invalid-name
async_remove_entry = config_entry_flow.webhook_async_remove_entry
10 changes: 10 additions & 0 deletions homeassistant/components/traccar/config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Config flow for Traccar."""
from homeassistant.helpers import config_entry_flow
from .const import DOMAIN


config_entry_flow.register_webhook_flow(
DOMAIN,
"Traccar Webhook",
{"docs_url": "https://www.home-assistant.io/components/traccar/"},
)
12 changes: 11 additions & 1 deletion homeassistant/components/traccar/const.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
"""Constants for Traccar integration."""

DOMAIN = "traccar"

CONF_MAX_ACCURACY = "max_accuracy"
CONF_SKIP_ACCURACY_ON = "skip_accuracy_filter_on"

ATTR_ACCURACY = "accuracy"
ATTR_ADDRESS = "address"
ATTR_ALTITUDE = "altitude"
ATTR_BATTERY = "batt"
ATTR_BEARING = "bearing"
ATTR_CATEGORY = "category"
ATTR_GEOFENCE = "geofence"
ATTR_ID = "id"
ATTR_LATITUDE = "lat"
ATTR_LONGITUDE = "lon"
ATTR_MOTION = "motion"
ATTR_SPEED = "speed"
ATTR_STATUS = "status"
ATTR_TIMESTAMP = "timestamp"
ATTR_TRACKER = "tracker"
ATTR_TRACCAR_ID = "traccar_id"
ATTR_STATUS = "status"

EVENT_DEVICE_MOVING = "device_moving"
EVENT_COMMAND_RESULT = "command_result"
Expand Down
177 changes: 175 additions & 2 deletions homeassistant/components/traccar/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import voluptuous as vol

from homeassistant.components.device_tracker import PLATFORM_SCHEMA
from homeassistant.core import callback
from homeassistant.const import (
CONF_HOST,
CONF_PORT,
Expand All @@ -16,19 +16,33 @@
CONF_MONITORED_CONDITIONS,
CONF_EVENT,
)
from homeassistant.components.device_tracker import PLATFORM_SCHEMA, SOURCE_TYPE_GPS
from homeassistant.components.device_tracker.config_entry import TrackerEntity
from homeassistant.helpers import device_registry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import HomeAssistantType
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

from . import DOMAIN, TRACKER_UPDATE
from .const import (
ATTR_ACCURACY,
ATTR_ADDRESS,
ATTR_ALTITUDE,
ATTR_BATTERY,
ATTR_BEARING,
ATTR_CATEGORY,
ATTR_GEOFENCE,
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_MOTION,
ATTR_SPEED,
ATTR_STATUS,
ATTR_TRACKER,
ATTR_TRACCAR_ID,
ATTR_STATUS,
EVENT_DEVICE_MOVING,
EVENT_COMMAND_RESULT,
EVENT_DEVICE_FUEL_DROP,
Expand Down Expand Up @@ -101,6 +115,45 @@
)


async def async_setup_entry(hass: HomeAssistantType, entry, async_add_entities):
"""Configure a dispatcher connection based on a config entry."""

@callback
def _receive_data(device, latitude, longitude, battery, accuracy, attrs):
"""Receive set location."""
if device in hass.data[DOMAIN]["devices"]:
return

hass.data[DOMAIN]["devices"].add(device)

async_add_entities(
[TraccarEntity(device, latitude, longitude, battery, accuracy, attrs)]
)

hass.data[DOMAIN]["unsub_device_tracker"][
entry.entry_id
] = async_dispatcher_connect(hass, TRACKER_UPDATE, _receive_data)

# Restore previously loaded devices
dev_reg = await device_registry.async_get_registry(hass)
dev_ids = {
identifier[1]
for device in dev_reg.devices.values()
for identifier in device.identifiers
if identifier[0] == DOMAIN
}
if not dev_ids:
return

entities = []
for dev_id in dev_ids:
hass.data[DOMAIN]["devices"].add(dev_id)
entity = TraccarEntity(dev_id, None, None, None, None, None)
entities.append(entity)

async_add_entities(entities)


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
Expand Down Expand Up @@ -273,3 +326,123 @@ async def import_events(self):
"attributes": event["attributes"],
},
)


class TraccarEntity(TrackerEntity, RestoreEntity):
"""Represent a tracked device."""

def __init__(self, device, latitude, longitude, battery, accuracy, attributes):
"""Set up Geofency entity."""
self._accuracy = accuracy
self._attributes = attributes
self._name = device
self._battery = battery
self._latitude = latitude
self._longitude = longitude
self._unsub_dispatcher = None
self._unique_id = device

@property
def battery_level(self):
"""Return battery value of the device."""
return self._battery

@property
def device_state_attributes(self):
"""Return device specific attributes."""
return self._attributes

@property
def latitude(self):
"""Return latitude value of the device."""
return self._latitude

@property
def longitude(self):
"""Return longitude value of the device."""
return self._longitude

@property
def location_accuracy(self):
"""Return the gps accuracy of the device."""
return self._accuracy

@property
def name(self):
"""Return the name of the device."""
return self._name

@property
def should_poll(self):
"""No polling needed."""
return False

@property
def unique_id(self):
"""Return the unique ID."""
return self._unique_id

@property
def device_info(self):
"""Return the device info."""
return {"name": self._name, "identifiers": {(DOMAIN, self._unique_id)}}

@property
def source_type(self):
"""Return the source type, eg gps or router, of the device."""
return SOURCE_TYPE_GPS

async def async_added_to_hass(self):
"""Register state update callback."""
await super().async_added_to_hass()
self._unsub_dispatcher = async_dispatcher_connect(
self.hass, TRACKER_UPDATE, self._async_receive_data
)

# don't restore if we got created with data
if self._latitude is not None or self._longitude is not None:
return

state = await self.async_get_last_state()
if state is None:
self._latitude = None
self._longitude = None
self._accuracy = None
self._attributes = {
ATTR_ALTITUDE: None,
ATTR_BEARING: None,
ATTR_SPEED: None,
}
self._battery = None
return

attr = state.attributes
self._latitude = attr.get(ATTR_LATITUDE)
self._longitude = attr.get(ATTR_LONGITUDE)
self._accuracy = attr.get(ATTR_ACCURACY)
self._attributes = {
ATTR_ALTITUDE: attr.get(ATTR_ALTITUDE),
ATTR_BEARING: attr.get(ATTR_BEARING),
ATTR_SPEED: attr.get(ATTR_SPEED),
}
self._battery = attr.get(ATTR_BATTERY)

async def async_will_remove_from_hass(self):
"""Clean up after entity before removal."""
await super().async_will_remove_from_hass()
self._unsub_dispatcher()

@callback
def _async_receive_data(
self, device, latitude, longitude, battery, accuracy, attributes
):
"""Mark the device as seen."""
if device != self.name:
return

self._latitude = latitude
self._longitude = longitude
self._battery = battery
self._accuracy = accuracy
self._attributes.update(attributes)
self.async_write_ha_state()
7 changes: 5 additions & 2 deletions homeassistant/components/traccar/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"domain": "traccar",
"name": "Traccar",
"config_flow": true,
"documentation": "https://www.home-assistant.io/components/traccar",
"requirements": [
"pytraccar==0.9.0",
"stringcase==1.2.0"
],
"dependencies": [],
"dependencies": [
"webhook"
],
"codeowners": [
"@ludeeus"
]
}
}
1 change: 1 addition & 0 deletions homeassistant/generated/config_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"tellduslive",
"toon",
"tplink",
"traccar",
"tradfri",
"twentemilieu",
"twilio",
Expand Down
1 change: 1 addition & 0 deletions tests/components/traccar/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the Traccar component."""
Loading