diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 95c4a6b..7dc486f 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -75,7 +75,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication public List GetConfiguredSensors() { - return this._configurationService.ConfiguredSensors.Select(s => new ConfiguredSensorModel() { Name = s.Name, Type = s.GetType().Name, Value = s.PreviousPublishedState, Id = s.Id, UpdateInterval = s.UpdateInterval, UnitOfMeasurement = s.GetAutoDiscoveryConfig().Unit_of_measurement }).ToList(); + return this._configurationService.ConfiguredSensors.Select(s => new ConfiguredSensorModel() { Name = s.Name, Type = s.GetType().Name, Value = s.PreviousPublishedState, Id = s.Id, UpdateInterval = s.UpdateInterval, UnitOfMeasurement = ((SensorDiscoveryConfigModel)s.GetAutoDiscoveryConfig()).Unit_of_measurement }).ToList(); } public List GetConfiguredCommands() diff --git a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs index 3a9e333..bcdd12f 100644 --- a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs +++ b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.Util; using hass_workstation_service.Data; +using hass_workstation_service.Domain; using hass_workstation_service.Domain.Commands; using Microsoft.Extensions.Logging; using MQTTnet; @@ -97,7 +98,7 @@ namespace hass_workstation_service.Communication } // 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(AbstractDiscoverable discoverable, string domain, bool clearConfig = false) { if (this._mqttClient.IsConnected) { @@ -110,8 +111,8 @@ namespace hass_workstation_service.Communication }; var message = new MqttApplicationMessageBuilder() - .WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{config.Name}/config") - .WithPayload(clearConfig ? "" : JsonSerializer.Serialize(config, config.GetType(), options)) + .WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{discoverable.ObjectId}/config") + .WithPayload(clearConfig ? "" : JsonSerializer.Serialize(discoverable.GetAutoDiscoveryConfig(), discoverable.GetAutoDiscoveryConfig().GetType(), options)) .WithRetainFlag() .Build(); await this.Publish(message); @@ -178,7 +179,7 @@ namespace hass_workstation_service.Communication { if (this.IsConnected) { - await this._mqttClient.SubscribeAsync(command.GetAutoDiscoveryConfig().Command_topic); + await this._mqttClient.SubscribeAsync(((CommandDiscoveryConfigModel) command.GetAutoDiscoveryConfig()).Command_topic); } else { @@ -187,7 +188,7 @@ namespace hass_workstation_service.Communication await Task.Delay(5500); } - await this._mqttClient.SubscribeAsync(command.GetAutoDiscoveryConfig().Command_topic); + await this._mqttClient.SubscribeAsync(((CommandDiscoveryConfigModel) command.GetAutoDiscoveryConfig()).Command_topic); } @@ -198,7 +199,7 @@ namespace hass_workstation_service.Communication { foreach (AbstractCommand command in this.Subscribers) { - if (command.GetAutoDiscoveryConfig().Command_topic == applicationMessage.Topic) + if (((CommandDiscoveryConfigModel)command.GetAutoDiscoveryConfig()).Command_topic == applicationMessage.Topic) { if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON") { diff --git a/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs b/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs index 1c5218d..3d942e1 100644 --- a/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs +++ b/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs @@ -15,6 +15,11 @@ namespace hass_workstation_service.Communication /// /// public string Name { get; set; } + /// + /// The MQTT topic subscribed to receive sensor values. + /// + /// + public string State_topic { get; set; } } public class SensorDiscoveryConfigModel : DiscoveryConfigModel { @@ -69,12 +74,6 @@ namespace hass_workstation_service.Communication /// /// public int? Qos { get; set; } - - /// - /// The MQTT topic subscribed to receive sensor values. - /// - /// - public string State_topic { get; set; } /// /// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception. /// @@ -147,11 +146,6 @@ namespace hass_workstation_service.Communication /// public int? Qos { get; set; } /// - /// The MQTT topic subscribed to receive sensor values. - /// - /// - public string State_topic { get; set; } - /// /// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception. /// /// diff --git a/hass-workstation-service/Domain/AbstractDiscoverable.cs b/hass-workstation-service/Domain/AbstractDiscoverable.cs index 7c388f7..aba479e 100644 --- a/hass-workstation-service/Domain/AbstractDiscoverable.cs +++ b/hass-workstation-service/Domain/AbstractDiscoverable.cs @@ -1,7 +1,9 @@ -using System; +using hass_workstation_service.Communication; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace hass_workstation_service.Domain @@ -9,5 +11,17 @@ namespace hass_workstation_service.Domain public abstract class AbstractDiscoverable { public abstract string Domain { get; } + public string Name { get; protected set; } + + public string ObjectId + { + get + { + return Regex.Replace(this.Name, "[^a-zA-Z0-9_-]", "_"); + } + } + public Guid Id { get; protected set; } + + public abstract DiscoveryConfigModel GetAutoDiscoveryConfig(); } } diff --git a/hass-workstation-service/Domain/Commands/AbstractCommand.cs b/hass-workstation-service/Domain/Commands/AbstractCommand.cs index b1b30e6..849077e 100644 --- a/hass-workstation-service/Domain/Commands/AbstractCommand.cs +++ b/hass-workstation-service/Domain/Commands/AbstractCommand.cs @@ -8,8 +8,6 @@ namespace hass_workstation_service.Domain.Commands public abstract class AbstractCommand : AbstractDiscoverable { - public Guid Id { get; protected set; } - public string Name { get; protected set; } /// /// The update interval in seconds. It checks state only if the interval has passed. /// @@ -40,7 +38,6 @@ namespace hass_workstation_service.Domain.Commands return config; } - public abstract CommandDiscoveryConfigModel GetAutoDiscoveryConfig(); public abstract string GetState(); public async Task PublishStateAsync() @@ -68,11 +65,11 @@ namespace hass_workstation_service.Domain.Commands } public async void PublishAutoDiscoveryConfigAsync() { - await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain); + await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain); } public async Task UnPublishAutoDiscoveryConfigAsync() { - await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain, true); + await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain, true); } public abstract void TurnOn(); public abstract void TurnOff(); diff --git a/hass-workstation-service/Domain/Sensors/AbstractSensor.cs b/hass-workstation-service/Domain/Sensors/AbstractSensor.cs index d78eb6c..22d9a37 100644 --- a/hass-workstation-service/Domain/Sensors/AbstractSensor.cs +++ b/hass-workstation-service/Domain/Sensors/AbstractSensor.cs @@ -9,11 +9,6 @@ namespace hass_workstation_service.Domain.Sensors public abstract class AbstractSensor : AbstractDiscoverable { - public Guid Id { get; protected set; } - public string Name { get; protected set; } - public string ObjectId { get { - return Regex.Replace(this.Name, "[^a-zA-Z0-9_-]", "_"); - } } /// /// The update interval in seconds. It checks state only if the interval has passed. /// @@ -44,7 +39,6 @@ namespace hass_workstation_service.Domain.Sensors return config; } - public abstract SensorDiscoveryConfigModel GetAutoDiscoveryConfig(); public abstract string GetState(); public async Task PublishStateAsync() @@ -72,11 +66,11 @@ namespace hass_workstation_service.Domain.Sensors } public async void PublishAutoDiscoveryConfigAsync() { - await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain); + await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain); } public async Task UnPublishAutoDiscoveryConfigAsync() { - await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain, true); + await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain, true); } }