diff --git a/src/Models/AutoDiscovery.cs b/src/Models/AutoDiscovery.cs index f9fa393b..02725c24 100644 --- a/src/Models/AutoDiscovery.cs +++ b/src/Models/AutoDiscovery.cs @@ -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); \ No newline at end of file diff --git a/src/Models/Device.cs b/src/Models/Device.cs index bb95aa02..a92baa4e 100644 --- a/src/Models/Device.cs +++ b/src/Models/Device.cs @@ -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()