From d2f07186fb426886845a9619aa6ea46998387b8b Mon Sep 17 00:00:00 2001 From: Boris Brock Date: Tue, 8 Oct 2024 08:55:31 +0200 Subject: [PATCH] Squashed commit of the following: commit 34b09d6213dc7998f3edd34c9e708225eac42807 Author: Boris Brock Date: Tue Oct 8 08:02:14 2024 +0200 Deleting obsolete doc, updating ReadMe, fixing MQTT bug commit 36e55a65304a5eb2e5a58a49fd070592c1e016b3 Author: Boris Brock Date: Tue Oct 8 07:49:46 2024 +0200 Update MQTTManager.cpp commit f1e5dcef213a4226374ed027e672c58f81dff7d1 Author: Boris Brock Date: Mon Oct 7 21:59:43 2024 +0200 Adding remaning messages commit 9bf0e535cb4417c037d1e78cfcd22d2e1c57803f Author: Boris Brock Date: Sun Oct 6 21:04:30 2024 +0200 Adding first discovery topics --- README.md | 14 +++--- doc/mqtt.yaml | 67 ----------------------------- src/Components/MQTT/MQTTManager.cpp | 15 +++++++ 3 files changed, 20 insertions(+), 76 deletions(-) delete mode 100644 doc/mqtt.yaml diff --git a/README.md b/README.md index cb953f2..2982589 100644 --- a/README.md +++ b/README.md @@ -110,15 +110,11 @@ Start evcc with your new configuration and the HeidelBridge should be there. # Setting up Home Assistant -HeidelBridge offers a simple MQTT API (see below). Via this API, HeidelBridge can easily be added to Home Assistant. -To do so, add the following to your `configuration.yaml`: - -``` -mqtt: !include mqtt.yaml -``` - -Also create a new file called `mqtt.yaml` and paste the example code from [here](doc/mqtt.yaml) into that file. -Now Home Assistant should have all the entities provided by the HeidelBridge MQTT API. +HeidelBridge offers a simple MQTT API (see below). It also supports Home Assistant's MQTT auto discovery feature. +This way HeidelBridge can easily be added to Home Assistant: + - Make sure the MQTT integration in Home Assistant is enabled. + - Power on your HeidelBridge. + - The HeidelBridge should immediately show up as an MQTT device. GUI Example: diff --git a/doc/mqtt.yaml b/doc/mqtt.yaml deleted file mode 100644 index 118591e..0000000 --- a/doc/mqtt.yaml +++ /dev/null @@ -1,67 +0,0 @@ -- binary_sensor: - name: "Vehicle connected" - state_topic: "heidelbridge/is_vehicle_connected" - unique_id: "is_vehicle_connected" - payload_on: "1" - payload_off: "0" - device_class: "plug" - device: - name: heidelBridge - identifiers: heidelbridge - -- binary_sensor: - name: "Vehicle charging" - state_topic: "heidelbridge/is_vehicle_charging" - unique_id: "is_vehicle_charging" - payload_on: "1" - payload_off: "0" - device_class: "battery_charging" - device: - name: HeidelBridge - identifiers: heidelbridge - -- sensor: - - name: "Charging power" - state_topic: "heidelbridge/charging_power" - unique_id: "charging_power" - unit_of_measurement: "W" - device_class: "power" - device: - name: HeidelBridge - identifiers: heidelbridge - - - name: "Charging current" - state_topic: "heidelbridge/charging_current/phase1" - unique_id: "charging_current" - unit_of_measurement: "A" - device_class: "current" - device: - name: HeidelBridge - identifiers: heidelbridge - - - name: "Charging current limit" - state_topic: "heidelbridge/charging_current_limit" - unique_id: "charging_current_limit" - unit_of_measurement: "A" - device_class: "current" - device: - name: HeidelBridge - identifiers: heidelbridge - - - name: "Energy meter" - state_topic: "heidelbridge/energy_meter" - unique_id: "energy_meter" - unit_of_measurement: "kWh" - device_class: "energy" - device: - name: HeidelBridge - identifiers: heidelbridge - - - name: "Temperature" - state_topic: "heidelbridge/temperature" - unique_id: "temperature" - unit_of_measurement: "°C" - device_class: "temperature" - device: - name: HeidelBridge - identifiers: heidelbridge \ No newline at end of file diff --git a/src/Components/MQTT/MQTTManager.cpp b/src/Components/MQTT/MQTTManager.cpp index 177fc78..1887bf2 100644 --- a/src/Components/MQTT/MQTTManager.cpp +++ b/src/Components/MQTT/MQTTManager.cpp @@ -118,6 +118,18 @@ namespace MQTTManager } } + void PublishHomeAssistantDiscovery() + { + // Publish Home Assistant MQTT discovery messages + gMqttClient.publish("homeassistant/binary_sensor/HeidelBridge/is_vehicle_connected/config", 1, false, R"({"name":"Vehicle connected","device_class":"plug","state_topic":"heidelbridge/is_vehicle_connected","payload_on":"1","payload_off":"0","unique_id":"is_vehicle_connected","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + gMqttClient.publish("homeassistant/binary_sensor/HeidelBridge/is_vehicle_charging/config", 1, false, R"({"name":"Vehicle charging","device_class":"battery_charging","state_topic":"heidelbridge/is_vehicle_charging","payload_on":"1","payload_off":"0","unique_id":"is_vehicle_charging","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + gMqttClient.publish("homeassistant/sensor/HeidelBridge/charging_power/config", 1, false, R"({"name":"Charging power","device_class":"power","state_topic":"heidelbridge/charging_power","unique_id":"charging_power","unit_of_measurement":"W","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + gMqttClient.publish("homeassistant/sensor/HeidelBridge/charging_current/config", 1, false, R"({"name":"Charging current","device_class":"current","state_topic":"heidelbridge/charging_current/phase1","unique_id":"charging_current","unit_of_measurement":"A","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + gMqttClient.publish("homeassistant/sensor/HeidelBridge/charging_current_limit/config", 1, false, R"({"name":"Charging current limit","device_class":"current","state_topic":"heidelbridge/charging_current_limit","unique_id":"charging_current_limit","unit_of_measurement":"A","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + gMqttClient.publish("homeassistant/sensor/HeidelBridge/energy_meter/config", 1, false, R"({"name":"Energy meter","device_class":"energy","state_topic":"heidelbridge/energy_meter","unique_id":"energy_meter","unit_of_measurement":"kWh","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + gMqttClient.publish("homeassistant/sensor/HeidelBridge/temperature/config", 1, false, R"({"name":"Temperature","device_class":"temperature","state_topic":"heidelbridge/temperature","unique_id":"temperature","unit_of_measurement":"°C","device":{"identifiers":["BB42"],"name":"HeidelBridge"}})"); + } + void OnMqttConnect(bool sessionPresent) { Logger::Info("Connected to MQTT"); @@ -130,6 +142,9 @@ namespace MQTTManager gMqttClient.publish("heidelbridge/version", 0, true, versionString.c_str()); gMqttClient.publish("heidelbridge/build_date", 0, true, __DATE__); gMqttClient.publish("heidelbridge/ip_address", 0, true, WiFi.localIP().toString().c_str()); + + // Publish discovery data + PublishHomeAssistantDiscovery(); } void OnMqttDisconnect(AsyncMqttClientDisconnectReason reason)