@@ -27,26 +27,27 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistant do
2727 the given vehicle summary.
2828
2929 Each payload is published retained (QoS 1) to
30- `<discovery_prefix>/<component>/<node>_<car_id>/<object_id>/config` where
31- `node` is `#{ @ node } `. Returns `:ok` on success.
30+ `<discovery_prefix>/<component>/<node>/<object_id>/config` where `node` is
31+ `#{ @ node } _<car_id>` (with the `MQTT_NAMESPACE` inserted after `#{ @ node } `
32+ when set). Returns `:ok` on success.
3233 """
3334 @ spec publish ( term ( ) , publish_opts ( ) , term ( ) ) :: :ok | { :error , term ( ) }
3435 def publish ( % Summary { } = summary , opts , publisher ) do
3536 car_id = Keyword . fetch! ( opts , :car_id )
3637 namespace = Keyword . get ( opts , :namespace )
3738 base_url = Keyword . get ( opts , :base_url )
3839 prefix = Keyword . get ( opts , :discovery_prefix , @ discovery_prefix )
39- node = " #{ @ node } _ #{ car_id } "
40+ node = node ( car_id , namespace )
4041
41- device = device ( summary , car_id , base_url )
42+ device = device ( summary , car_id , base_url , namespace )
4243
4344 Enum . reduce_while ( entities ( ) , :ok , fn { component , object_id , config } , _acc ->
4445 topic = discovery_topic ( prefix , component , node , object_id )
4546
4647 payload =
4748 config
4849 |> resolve_topics ( car_id , namespace )
49- |> Map . put ( :unique_id , "teslamate_ #{ car_id } _#{ object_id } " )
50+ |> Map . put ( :unique_id , "#{ node } _#{ object_id } " )
5051 |> Map . put ( :object_id , "tesla_#{ object_id } " )
5152 |> Map . put ( :device , device )
5253 |> Jason . encode! ( )
@@ -65,8 +66,9 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistant do
6566 """
6667 @ spec clear ( pos_integer ( ) , publish_opts ( ) , term ( ) ) :: :ok | { :error , term ( ) }
6768 def clear ( car_id , opts , publisher ) do
69+ namespace = Keyword . get ( opts , :namespace )
6870 prefix = Keyword . get ( opts , :discovery_prefix , @ discovery_prefix )
69- node = " #{ @ node } _ #{ car_id } "
71+ node = node ( car_id , namespace )
7072
7173 Enum . reduce_while ( entities ( ) , :ok , fn { component , object_id , _config } , _acc ->
7274 topic = discovery_topic ( prefix , component , node , object_id )
@@ -109,14 +111,29 @@ defmodule TeslaMate.Mqtt.PubSub.HomeAssistant do
109111 |> Enum . join ( "/" )
110112 end
111113
112- defp device ( % Summary { } = summary , car_id , base_url ) do
114+ defp node ( car_id , namespace ) do
115+ [ @ node , namespace , car_id ]
116+ |> Enum . reject ( & is_nil / 1 )
117+ |> Enum . join ( "_" )
118+ end
119+
120+ defp device ( % Summary { } = summary , car_id , base_url , namespace ) do
113121 name = summary . display_name || car_name ( summary ) || "Tesla ##{ car_id } "
114122 model = summary . model || "Tesla"
115123
116- % { identifiers: [ "teslamate_car_#{ car_id } " ] , manufacturer: "Tesla" , name: name , model: model }
124+ % {
125+ identifiers: [ device_identifier ( car_id , namespace ) ] ,
126+ manufacturer: "Tesla" ,
127+ name: name ,
128+ model: model
129+ }
117130 |> maybe_put ( :configuration_url , base_url )
118131 end
119132
133+ defp device_identifier ( car_id , namespace ) do
134+ [ @ node , namespace , "car" , car_id ] |> Enum . reject ( & is_nil / 1 ) |> Enum . join ( "_" )
135+ end
136+
120137 defp car_name ( % Summary { car: % Car { name: name } } ) when is_binary ( name ) and name != "" , do: name
121138 defp car_name ( _ ) , do: nil
122139
0 commit comments