Skip to content

Commit 59fa6b8

Browse files
committed
Add Home Assistant charging binary sensor
1 parent feb9b39 commit 59fa6b8

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

lib/teslamate/mqtt/pubsub/home_assistant.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,17 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistant do
468468
icon: "mdi:ev-station",
469469
value_template: humanize_value_template(:camel_case)
470470
}},
471+
{"binary_sensor", "charging_state",
472+
%{
473+
component_id: "charging",
474+
state_topic_key: :charging_state,
475+
name: "Charging",
476+
device_class: "battery_charging",
477+
payload_on: "true",
478+
payload_off: "false",
479+
value_template: "{{ 'true' if value == 'Charging' else 'false' }}",
480+
icon: "mdi:battery-charging"
481+
}},
471482
{"sensor", "climate_keeper_mode",
472483
%{
473484
state_topic_key: :climate_keeper_mode,

test/teslamate/mqtt/pubsub/home_assistant_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,32 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistantTest do
234234
"{{ value | replace('_', ' ') | title }}"
235235
end
236236

237+
test "derives charging status from the charging state", %{test: name} do
238+
publisher_name = start_publisher(name)
239+
240+
:ok = HomeAssistant.publish(@summary, [car_id: 0], {MqttPublisherMock, publisher_name})
241+
242+
{_topic, decoded} = receive_device_config()
243+
components = decoded["components"]
244+
245+
charging_state = components["charging_state"]
246+
assert charging_state["platform"] == "sensor"
247+
assert charging_state["state_topic"] == "teslamate/cars/0/charging_state"
248+
249+
charging = components["charging"]
250+
assert charging["platform"] == "binary_sensor"
251+
assert charging["name"] == "Charging"
252+
assert charging["device_class"] == "battery_charging"
253+
assert charging["payload_on"] == "true"
254+
assert charging["payload_off"] == "false"
255+
assert charging["value_template"] == "{{ 'true' if value == 'Charging' else 'false' }}"
256+
assert charging["icon"] == "mdi:battery-charging"
257+
assert charging["state_topic"] == "teslamate/cars/0/charging_state"
258+
assert charging["unique_id"] == "teslamate_0_charging"
259+
assert charging["object_id"] == "tesla_charging_state"
260+
refute Map.has_key?(charging, "component_id")
261+
end
262+
237263
test "derives the parking brake from documented shift states", %{test: name} do
238264
publisher_name = start_publisher(name)
239265

0 commit comments

Comments
 (0)