Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HASS discovery, set source_type to bluetooth fixes #657 #699

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Check warning on line 8 in src/Models/AutoDiscovery.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
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
Loading