Skip to content

Commit 1ff9185

Browse files
committed
feat(mqtt): add opt-in Home Assistant MQTT discovery
Add an MQTT_HOME_ASSISTANT_DISCOVERY env var that, when enabled, makes VehicleSubscriber publish HA discovery config payloads (one per entity) to homeassistant/<component>/teslamate_<car_id>/<object_id>/config on startup, mirroring the manual configuration documented in website/docs/integrations/home_assistant.md.
1 parent df17f41 commit 1ff9185

9 files changed

Lines changed: 934 additions & 60 deletions

File tree

config/runtime.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ if System.get_env("DISABLE_MQTT") != "true" or config_env() == :test do
181181
tls: System.get_env("MQTT_TLS") == "true",
182182
accept_invalid_certs: System.get_env("MQTT_TLS_ACCEPT_INVALID_CERTS") == "true",
183183
namespace: System.get_env("MQTT_NAMESPACE") |> Util.validate_namespace!(),
184-
ipv6: System.get_env("MQTT_IPV6") == "true"
184+
ipv6: System.get_env("MQTT_IPV6") == "true",
185+
discovery: System.get_env("MQTT_HOME_ASSISTANT_DISCOVERY") == "true",
186+
discovery_base_url: System.get_env("MQTT_HOME_ASSISTANT_DISCOVERY_URL"),
187+
discovery_prefix: System.get_env("MQTT_HOME_ASSISTANT_DISCOVERY_PREFIX")
185188
end
186189

187190
if config_env() != :test do

lib/teslamate/mqtt.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ defmodule TeslaMate.Mqtt do
1616
children = [
1717
{Tortoise311.Connection, connection_config(opts) ++ [client_id: client_id]},
1818
{Publisher, client_id: client_id},
19-
{PubSub, namespace: opts[:namespace]}
19+
{PubSub,
20+
[
21+
namespace: opts[:namespace],
22+
discovery: opts[:discovery],
23+
discovery_base_url: opts[:discovery_base_url],
24+
discovery_prefix: opts[:discovery_prefix]
25+
]
26+
|> Enum.reject(fn {_key, value} -> is_nil(value) end)}
2027
]
2128

2229
Supervisor.init(children, strategy: :one_for_one)

lib/teslamate/mqtt/pubsub.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ defmodule TeslaMate.Mqtt.PubSub do
1212

1313
@impl true
1414
def init(opts) do
15+
subscriber_opts =
16+
opts
17+
|> Keyword.take([:namespace, :discovery, :discovery_base_url, :discovery_prefix])
18+
1519
children =
1620
Vehicles.list()
17-
|> Enum.map(&{VehicleSubscriber, Keyword.merge(opts, car_id: &1.car.id)})
21+
|> Enum.map(&{VehicleSubscriber, Keyword.merge(subscriber_opts, car_id: &1.car.id)})
1822

1923
Supervisor.init(children, strategy: :one_for_one)
2024
end

0 commit comments

Comments
 (0)