From 96805d439d606b78764d7e0d2338116d4ac0feb6 Mon Sep 17 00:00:00 2001 From: Linus Dietz <45101649+Dielee@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:06:13 +0200 Subject: [PATCH] Add interior and exterior image entities #62 --- src/CHANGELOG.md | 7 +++++++ src/config.yaml | 2 +- src/const.py | 2 +- src/mqtt.py | 42 ++++++++++++++++++++++++++++++++++++++++-- src/volvo.py | 1 + 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index f5a79dc..9779178 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -1,3 +1,10 @@ +## v1.7.6 +### 🚀 Features: + +- Added support for interior and exterior images via the new [MQTT Image](https://www.home-assistant.io/integrations/image.mqtt/) entity. #62 + +NOTE: This feature is only available using homeassistant version 2023.7.1 or greater. + ## v1.7.5 ### 🚀 Features: diff --git a/src/config.yaml b/src/config.yaml index 0e7bc0d..c5e745e 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -1,6 +1,6 @@ name: "Volvo2Mqtt" description: "Volvo AAOS MQTT bridge" -version: "1.7.5" +version: "1.7.6" slug: "volvo2mqtt" init: false url: "https://github.com/Dielee/volvo2mqtt" diff --git a/src/const.py b/src/const.py index ebe9e5f..0f7a233 100644 --- a/src/const.py +++ b/src/const.py @@ -1,6 +1,6 @@ from config import settings -VERSION = "v1.7.5" +VERSION = "v1.7.6" OAUTH_URL = "https://volvoid.eu.volvocars.com/as/token.oauth2" VEHICLES_URL = "https://api.volvocars.com/connected-vehicle/v1/vehicles" diff --git a/src/mqtt.py b/src/mqtt.py index 090afe8..bef609c 100644 --- a/src/mqtt.py +++ b/src/mqtt.py @@ -42,6 +42,43 @@ def connect(): mqtt_client = client +def send_car_images(vin, data, device): + if util.keys_exists(data, "images"): + for entity in [{"name": "Exterior Image", "id": "exterior_image"}, + {"name": "Interior Image", "id": "interior_image"}]: + url_topic = f"homeassistant/image/{vin}_{entity['id']}/image_url" + config = { + "name": entity["name"], + "object_id": f"volvo_{vin}_{entity['id']}", + "schema": "state", + "icon": "mdi:image-area", + "url_topic": url_topic, + "device": device, + "unique_id": f"volvoAAOS2mqtt_{vin}_{entity['id']}", + "availability_topic": availability_topic + } + + mqtt_client.publish( + f"homeassistant/image/volvoAAOS2mqtt/{vin}_{entity['id']}/config", + json.dumps(config), + retain=True + ) + + if entity["id"] == "exterior_image": + mqtt_client.publish( + url_topic, + data["images"]["exteriorDefaultUrl"], + retain=True + ) + + if entity["id"] == "interior_image": + mqtt_client.publish( + url_topic, + data["images"]["interiorDefaultUrl"], + retain=True + ) + + def on_connect(client, userdata, flags, rc): send_heartbeat() if len(subscribed_topics) > 0: @@ -232,12 +269,13 @@ def create_ha_devices(): if entity.get("domain") == "device_tracker": config["json_attributes_topic"] = f"homeassistant/{entity['domain']}/{vin}_{entity['id']}/attributes" - - if entity.get("domain") in ["switch", "lock", "button"]: + elif entity.get("domain") in ["switch", "lock", "button"]: command_topic = f"homeassistant/{entity['domain']}/{vin}_{entity['id']}/command" config["command_topic"] = command_topic subscribed_topics.append(command_topic) mqtt_client.subscribe(command_topic) + elif entity.get("domain") == "image": + config["url_topic"] = f"homeassistant/{entity['domain']}/{vin}_{entity['id']}/image_url" mqtt_client.publish( f"homeassistant/{entity['domain']}/volvoAAOS2mqtt/{vin}_{entity['id']}/config", diff --git a/src/volvo.py b/src/volvo.py index bdc29cb..0207b91 100644 --- a/src/volvo.py +++ b/src/volvo.py @@ -125,6 +125,7 @@ def get_vehicle_details(vin): "model": data['descriptions']['model'], "name": f"{data['descriptions']['model']} ({data['modelYear']}) - {vin}", } + mqtt.send_car_images(vin, data, device) elif response.status_code == 500 and not settings.volvoData["vin"]: # Workaround for some cars that are not returning vehicle details device = {