forked from peter/volvo2mqtt
Add car location device_tracker
This commit is contained in:
@@ -13,6 +13,7 @@ CAR_LOCK_URL = "https://api.volvocars.com/connected-vehicle/v2/vehicles/{0}/comm
|
||||
CAR_UNLOCK_URL = "https://api.volvocars.com/connected-vehicle/v2/vehicles/{0}/commands/unlock"
|
||||
RECHARGE_STATE_URL = "https://api.volvocars.com/energy/v1/vehicles/{0}/recharge-status"
|
||||
ODOMETER_STATE_URL = "https://api.volvocars.com/connected-vehicle/v2/vehicles/{0}/odometer"
|
||||
LOCATION_STATE_URL = "https://api.volvocars.com/location/v1/vehicles/{0}/location"
|
||||
|
||||
charging_system_states = {"CHARGING_SYSTEM_CHARGING": "Charging", "CHARGING_SYSTEM_IDLE": "Idle",
|
||||
"CHARGING_SYSTEM_FAULT": "Fault", "CHARGING_SYSTEM_UNSPECIFIED": "Unspecified"}
|
||||
@@ -49,3 +50,7 @@ supported_locks = [
|
||||
supported_buttons = [
|
||||
{"name": "Force Update Data", "id": "update_data", "icon": "update", "url": ""}
|
||||
]
|
||||
|
||||
supported_device_trackers = [
|
||||
{"name": "Location", "id": "location", "icon": "map-marker-radius", "url": LOCATION_STATE_URL}
|
||||
]
|
||||
+27
-1
@@ -7,7 +7,9 @@ from datetime import datetime
|
||||
from babel.dates import format_datetime
|
||||
from config import settings
|
||||
from const import CLIMATE_START_URL, CLIMATE_STOP_URL, CAR_LOCK_URL, \
|
||||
CAR_UNLOCK_URL, supported_sensors, supported_buttons, supported_switches, supported_locks
|
||||
CAR_UNLOCK_URL, supported_sensors, supported_buttons, supported_switches, \
|
||||
supported_locks, supported_device_trackers
|
||||
|
||||
|
||||
|
||||
mqtt_client: mqtt.Client
|
||||
@@ -88,6 +90,13 @@ def update_car_data(force_update=False):
|
||||
global last_data_update
|
||||
last_data_update = format_datetime(datetime.now(), format="medium", locale=settings["babelLocale"])
|
||||
for vin in volvo.vins:
|
||||
for device_tracker in supported_device_trackers:
|
||||
state = volvo.api_call(device_tracker["url"], "GET", vin, device_tracker["id"], force_update)
|
||||
mqtt_client.publish(
|
||||
f"homeassistant/device_tracker/{vin}_{device_tracker['id']}/attributes",
|
||||
json.dumps(state)
|
||||
)
|
||||
|
||||
for lock in supported_locks:
|
||||
state = volvo.api_call(lock["url"], "GET", vin, lock["id"], force_update)
|
||||
mqtt_client.publish(
|
||||
@@ -121,6 +130,23 @@ def create_ha_devices():
|
||||
for vin in volvo.vins:
|
||||
device = volvo.get_vehicle_details(vin)
|
||||
|
||||
for device_tracker in supported_device_trackers:
|
||||
config = {
|
||||
"name": device_tracker['name'],
|
||||
"object_id": device_tracker['id'],
|
||||
"schema": "state",
|
||||
"icon": f"mdi:{device_tracker['icon']}",
|
||||
|
||||
"state_topic": f"homeassistant/device_tracker/{vin}_{device_tracker['id']}/state",
|
||||
"device": device,
|
||||
"unique_id": f"volvoAAOS2mqtt_{vin}_{device_tracker['id']}",
|
||||
"json_attributes_topic": f"homeassistant/device_tracker/{vin}_{device_tracker['id']}/attributes"
|
||||
}
|
||||
mqtt_client.publish(
|
||||
f"homeassistant/device_tracker/volvoAAOS2mqtt/{vin}_{device_tracker['id']}/config",
|
||||
json.dumps(config),
|
||||
)
|
||||
|
||||
for button in supported_buttons:
|
||||
command_topic = f"homeassistant/button/{vin}_{button['id']}/command"
|
||||
config = {
|
||||
|
||||
@@ -311,5 +311,11 @@ def parse_api_data(data, sensor_id=None):
|
||||
return data["data"]["tankLidOpen"]["value"]
|
||||
else:
|
||||
return ""
|
||||
elif sensor_id == "location":
|
||||
coordinates = {}
|
||||
if "geometry" in data["data"]:
|
||||
raw_data = data["data"]["geometry"]["coordinates"]
|
||||
coordinates = {"longitude": raw_data[0], "latitude": raw_data[1], "gps_accuracy": 1}
|
||||
return coordinates
|
||||
else:
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user