From dd6e4f8cb70a3d120c2fd3a3a7b873ad8fa47cba Mon Sep 17 00:00:00 2001 From: Linus Dietz <45101649+Dielee@users.noreply.github.com> Date: Thu, 22 Jun 2023 14:50:24 +0200 Subject: [PATCH] Add car location device_tracker --- src/const.py | 5 +++++ src/mqtt.py | 28 +++++++++++++++++++++++++++- src/volvo.py | 6 ++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/const.py b/src/const.py index 59fcaf3..cdfc4af 100644 --- a/src/const.py +++ b/src/const.py @@ -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} +] \ No newline at end of file diff --git a/src/mqtt.py b/src/mqtt.py index 9de116b..1d7f0c6 100644 --- a/src/mqtt.py +++ b/src/mqtt.py @@ -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 = { diff --git a/src/volvo.py b/src/volvo.py index 98f5521..c80e556 100644 --- a/src/volvo.py +++ b/src/volvo.py @@ -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 ""