Skip to content

Commit

Permalink
Fix HASS discovery, set source_type to bluetooth fixes #657 (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
DTTerastar authored Sep 1, 2024
1 parent e044f81 commit a5d75ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
60 changes: 29 additions & 31 deletions src/Models/AutoDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,45 @@
using Newtonsoft.Json;
using TextExtensions;

namespace ESPresense.Models
namespace ESPresense.Models;

public class AutoDiscovery(Device dev, string name, string component, string sourceType = null)
{
public class AutoDiscovery
{
private readonly Device _dev;
private readonly string _component;
private bool _sent;

public AutoDiscovery(Device dev, string component)
{
_dev = dev;
_component = component;
}
[JsonProperty("name")]
private string Name => name;

[JsonProperty("name")]
string Name => _dev.Name ?? _dev.Id;
[JsonProperty("unique_id")]
private string UniqueId => $"espresense-companion-{dev.Id}";

[JsonProperty("unique_id")]
string UniqueId => $"espresense-companion-{_dev.Id}";
[JsonProperty("state_topic")]
private string StateTopic => $"espresense/companion/{dev.Id}";

[JsonProperty("state_topic")]
string StateTopic => $"espresense/companion/{_dev.Id}";
[JsonProperty("json_attributes_topic")]
private string JsonAttributesTopic => $"espresense/companion/{dev.Id}/attributes";

[JsonProperty("json_attributes_topic")]
string JsonAttributesTopic => $"espresense/companion/{_dev.Id}/attributes";
[JsonProperty("status_topic")]
private string EntityStatusTopic => "espresense/companion/status";

[JsonProperty("status_topic")]
string EntityStatusTopic => "espresense/companion/status";
[JsonProperty("device")]
private DeviceRecord Device => new(dev.Name ?? dev.Id, "ESPresense", "Companion", "0.0.0", new[] { $"espresense-{dev.Id}" });

[JsonProperty("device")]
private DeviceRecord Device => new DeviceRecord(_dev.Name, "ESPresense", "Companion", "0.0.0", new[] { $"espresense-{_dev.Id}" });
[JsonProperty("origin")]
private OriginRecord Origin => new("ESPresense Companion");

bool _sent;
[JsonProperty("source_type")]
private string SourceType => sourceType;

public async Task Send(MqttCoordinator mc)
{
if (_sent) return;
_sent = true;
public async Task Send(MqttCoordinator mc)
{
if (_sent) return;
_sent = true;

await mc.EnqueueAsync($"homeassistant/{_component}/{_dev.Id.ToSnakeCase()}/config", JsonConvert.SerializeObject(this, SerializerSettings.NullIgnore), retain: true);
}
await mc.EnqueueAsync($"homeassistant/{component}/{dev.Id.ToSnakeCase()}/config", JsonConvert.SerializeObject(this, SerializerSettings.NullIgnore), true);
}

public record DeviceRecord(string? name, string manufacturer, string model, string sw_version, string[] identifiers);
}

public record DeviceRecord(string? name, string manufacturer, string model, string sw_version, string[] identifiers);

public record OriginRecord(string? name);
2 changes: 1 addition & 1 deletion src/Models/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Device(string id, TimeSpan timeout)
{
Id = id;
Timeout = timeout;
HassAutoDiscovery.Add(new AutoDiscovery(this, "device_tracker"));
HassAutoDiscovery.Add(new AutoDiscovery(this, "", "device_tracker", "bluetooth"));
}

public override string ToString()
Expand Down

0 comments on commit a5d75ba

Please sign in to comment.