wip reworking mqtt topic

33-command-click-for-more-information-bu
Sleevezipper 4 years ago
parent 0d17427772
commit 742bdd3d69

@ -95,7 +95,8 @@ namespace hass_workstation_service.Communication
this._logger.LogInformation($"Message dropped because mqtt not connected: {message}"); this._logger.LogInformation($"Message dropped because mqtt not connected: {message}");
} }
} }
// TODO: This should take a sensor/command instead of a config.
// Then we can ask the sensor about the topic based on ObjectId instead of referencing Name directly
public async Task AnnounceAutoDiscoveryConfig(DiscoveryConfigModel config, string domain, bool clearConfig = false) public async Task AnnounceAutoDiscoveryConfig(DiscoveryConfigModel config, string domain, bool clearConfig = false)
{ {
if (this._mqttClient.IsConnected) if (this._mqttClient.IsConnected)

@ -1,4 +1,5 @@
using System; using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using hass_workstation_service.Communication; using hass_workstation_service.Communication;
using MQTTnet; using MQTTnet;
@ -10,6 +11,9 @@ namespace hass_workstation_service.Domain.Sensors
{ {
public Guid Id { get; protected set; } public Guid Id { get; protected set; }
public string Name { get; protected set; } public string Name { get; protected set; }
public string ObjectId { get {
return Regex.Replace(this.Name, "[^a-zA-Z0-9_-]", "_");
} }
/// <summary> /// <summary>
/// The update interval in seconds. It checks state only if the interval has passed. /// The update interval in seconds. It checks state only if the interval has passed.
/// </summary> /// </summary>

@ -20,7 +20,7 @@ namespace hass_workstation_service.Domain.Sensors
Name = this.Name, Name = this.Name,
Unique_id = this.Id.ToString(), Unique_id = this.Id.ToString(),
Device = this.Publisher.DeviceConfigModel, Device = this.Publisher.DeviceConfigModel,
State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.Name}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });
} }

Loading…
Cancel
Save