seperate name and object id to fix issue 23

23-sensor-with-space-in-its-name-doesnt-
sleevezipper 4 years ago
parent 742bdd3d69
commit 78ed4d40d4

@ -75,7 +75,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication
public List<ConfiguredSensorModel> GetConfiguredSensors() public List<ConfiguredSensorModel> 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<ConfiguredCommandModel> GetConfiguredCommands() public List<ConfiguredCommandModel> GetConfiguredCommands()

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.InterProcesCommunication.Models;
using hass_workstation_service.Communication.Util; using hass_workstation_service.Communication.Util;
using hass_workstation_service.Data; using hass_workstation_service.Data;
using hass_workstation_service.Domain;
using hass_workstation_service.Domain.Commands; using hass_workstation_service.Domain.Commands;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MQTTnet; using MQTTnet;
@ -97,7 +98,7 @@ namespace hass_workstation_service.Communication
} }
// TODO: This should take a sensor/command instead of a config. // 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 // 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) if (this._mqttClient.IsConnected)
{ {
@ -110,8 +111,8 @@ namespace hass_workstation_service.Communication
}; };
var message = new MqttApplicationMessageBuilder() var message = new MqttApplicationMessageBuilder()
.WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{config.Name}/config") .WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{discoverable.ObjectId}/config")
.WithPayload(clearConfig ? "" : JsonSerializer.Serialize(config, config.GetType(), options)) .WithPayload(clearConfig ? "" : JsonSerializer.Serialize(discoverable.GetAutoDiscoveryConfig(), discoverable.GetAutoDiscoveryConfig().GetType(), options))
.WithRetainFlag() .WithRetainFlag()
.Build(); .Build();
await this.Publish(message); await this.Publish(message);
@ -178,7 +179,7 @@ namespace hass_workstation_service.Communication
{ {
if (this.IsConnected) if (this.IsConnected)
{ {
await this._mqttClient.SubscribeAsync(command.GetAutoDiscoveryConfig().Command_topic); await this._mqttClient.SubscribeAsync(((CommandDiscoveryConfigModel) command.GetAutoDiscoveryConfig()).Command_topic);
} }
else else
{ {
@ -187,7 +188,7 @@ namespace hass_workstation_service.Communication
await Task.Delay(5500); 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) 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") if (Encoding.UTF8.GetString(applicationMessage?.Payload) == "ON")
{ {

@ -15,6 +15,11 @@ namespace hass_workstation_service.Communication
/// </summary> /// </summary>
/// <value></value> /// <value></value>
public string Name { get; set; } public string Name { get; set; }
/// <summary>
/// The MQTT topic subscribed to receive sensor values.
/// </summary>
/// <value></value>
public string State_topic { get; set; }
} }
public class SensorDiscoveryConfigModel : DiscoveryConfigModel public class SensorDiscoveryConfigModel : DiscoveryConfigModel
{ {
@ -69,12 +74,6 @@ namespace hass_workstation_service.Communication
/// </summary> /// </summary>
/// <value></value> /// <value></value>
public int? Qos { get; set; } public int? Qos { get; set; }
/// <summary>
/// The MQTT topic subscribed to receive sensor values.
/// </summary>
/// <value></value>
public string State_topic { get; set; }
/// <summary> /// <summary>
/// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception. /// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.
/// </summary> /// </summary>
@ -147,11 +146,6 @@ namespace hass_workstation_service.Communication
/// <value></value> /// <value></value>
public int? Qos { get; set; } public int? Qos { get; set; }
/// <summary> /// <summary>
/// The MQTT topic subscribed to receive sensor values.
/// </summary>
/// <value></value>
public string State_topic { get; set; }
/// <summary>
/// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception. /// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.
/// </summary> /// </summary>
/// <value></value> /// <value></value>

@ -1,7 +1,9 @@
using System; using hass_workstation_service.Communication;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace hass_workstation_service.Domain namespace hass_workstation_service.Domain
@ -9,5 +11,17 @@ namespace hass_workstation_service.Domain
public abstract class AbstractDiscoverable public abstract class AbstractDiscoverable
{ {
public abstract string Domain { get; } 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();
} }
} }

@ -8,8 +8,6 @@ namespace hass_workstation_service.Domain.Commands
public abstract class AbstractCommand : AbstractDiscoverable public abstract class AbstractCommand : AbstractDiscoverable
{ {
public Guid Id { get; protected set; }
public string Name { get; protected set; }
/// <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>
@ -40,7 +38,6 @@ namespace hass_workstation_service.Domain.Commands
return config; return config;
} }
public abstract CommandDiscoveryConfigModel GetAutoDiscoveryConfig();
public abstract string GetState(); public abstract string GetState();
public async Task PublishStateAsync() public async Task PublishStateAsync()
@ -68,11 +65,11 @@ namespace hass_workstation_service.Domain.Commands
} }
public async void PublishAutoDiscoveryConfigAsync() public async void PublishAutoDiscoveryConfigAsync()
{ {
await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain); await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain);
} }
public async Task UnPublishAutoDiscoveryConfigAsync() 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 TurnOn();
public abstract void TurnOff(); public abstract void TurnOff();

@ -9,11 +9,6 @@ namespace hass_workstation_service.Domain.Sensors
public abstract class AbstractSensor : AbstractDiscoverable 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_-]", "_");
} }
/// <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>
@ -44,7 +39,6 @@ namespace hass_workstation_service.Domain.Sensors
return config; return config;
} }
public abstract SensorDiscoveryConfigModel GetAutoDiscoveryConfig();
public abstract string GetState(); public abstract string GetState();
public async Task PublishStateAsync() public async Task PublishStateAsync()
@ -72,11 +66,11 @@ namespace hass_workstation_service.Domain.Sensors
} }
public async void PublishAutoDiscoveryConfigAsync() public async void PublishAutoDiscoveryConfigAsync()
{ {
await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain); await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain);
} }
public async Task UnPublishAutoDiscoveryConfigAsync() public async Task UnPublishAutoDiscoveryConfigAsync()
{ {
await this.Publisher.AnnounceAutoDiscoveryConfig(this.GetAutoDiscoveryConfig(), this.Domain, true); await this.Publisher.AnnounceAutoDiscoveryConfig(this, this.Domain, true);
} }
} }

Loading…
Cancel
Save