From 8425970c8d5a71dee8224b37a84f602e7d6dbfb6 Mon Sep 17 00:00:00 2001 From: Sleevezipper Date: Sun, 5 Sep 2021 01:22:59 +0200 Subject: [PATCH 1/2] wip: name prefix --- UserInterface/UserInterface.csproj | 3 + .../ViewModels/GeneralSettingsViewModel.cs | 22 ++++ UserInterface/Views/GeneralSettings.axaml | 13 +++ UserInterface/Views/GeneralSettings.axaml.cs | 66 +++++++++++ UserInterface/Views/MainWindow.axaml | 1 + .../IServiceContractInterfaces.cs | 3 + .../InterProcessApi.cs | 5 + .../Communication/MQTT/MqttPublisher.cs | 29 ++++- .../Data/ConfigurationService.cs | 106 ++++++++++++++++-- .../Data/GeneralSettings.cs | 13 +++ .../Data/IConfigurationService.cs | 4 + .../Domain/Commands/AbstractCommand.cs | 4 +- .../Domain/Commands/CustomCommand.cs | 4 +- .../Domain/Commands/KeyCommand.cs | 4 +- .../Domain/Sensors/AbstractSensor.cs | 8 +- .../Domain/Sensors/ActiveWindowSensor.cs | 2 +- .../Domain/Sensors/CPULoadSensor.cs | 2 +- .../Domain/Sensors/CurrentClockSpeedSensor.cs | 2 +- .../Domain/Sensors/CurrentVolumeSensor.cs | 2 +- .../Domain/Sensors/DummySensor.cs | 2 +- .../Domain/Sensors/GpuLoadSensor.cs | 2 +- .../Domain/Sensors/GpuTemperatureSensor.cs | 2 +- .../Domain/Sensors/LastActiveSensor.cs | 2 +- .../Domain/Sensors/LastBootSensor.cs | 2 +- .../Domain/Sensors/MemoryUsageSensor.cs | 2 +- .../Domain/Sensors/MicrophoneActiveSensor.cs | 2 +- .../Domain/Sensors/NamedWindowSensor.cs | 2 +- .../Domain/Sensors/SessionStateSensor.cs | 2 +- .../Sensors/UserNotificationStateSensor.cs | 2 +- .../Domain/Sensors/WMIQuerySensor.cs | 2 +- .../Domain/Sensors/WebcamActiveSensor.cs | 2 +- hass-workstation-service/Program.cs | 1 - 32 files changed, 277 insertions(+), 41 deletions(-) create mode 100644 UserInterface/ViewModels/GeneralSettingsViewModel.cs create mode 100644 UserInterface/Views/GeneralSettings.axaml create mode 100644 UserInterface/Views/GeneralSettings.axaml.cs create mode 100644 hass-workstation-service/Data/GeneralSettings.cs diff --git a/UserInterface/UserInterface.csproj b/UserInterface/UserInterface.csproj index 633f585..9813764 100644 --- a/UserInterface/UserInterface.csproj +++ b/UserInterface/UserInterface.csproj @@ -32,6 +32,9 @@ BackgroundServiceSettings.axaml + + GeneralSettings.axaml + CommandSettings.axaml diff --git a/UserInterface/ViewModels/GeneralSettingsViewModel.cs b/UserInterface/ViewModels/GeneralSettingsViewModel.cs new file mode 100644 index 0000000..bda2e48 --- /dev/null +++ b/UserInterface/ViewModels/GeneralSettingsViewModel.cs @@ -0,0 +1,22 @@ +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using hass_workstation_service.Data; +using ReactiveUI; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Text; + +namespace UserInterface.ViewModels +{ + public class GeneralSettingsViewModel : ViewModelBase + { + private string namePrefix; + + public string NamePrefix { get => namePrefix; set => this.RaiseAndSetIfChanged(ref namePrefix, value); } + + public void Update(GeneralSettings settings) + { + this.NamePrefix = settings.NamePrefix; + } + } +} diff --git a/UserInterface/Views/GeneralSettings.axaml b/UserInterface/Views/GeneralSettings.axaml new file mode 100644 index 0000000..baab826 --- /dev/null +++ b/UserInterface/Views/GeneralSettings.axaml @@ -0,0 +1,13 @@ + + + Settings + IP address or hostname + + + + diff --git a/UserInterface/Views/GeneralSettings.axaml.cs b/UserInterface/Views/GeneralSettings.axaml.cs new file mode 100644 index 0000000..39b2618 --- /dev/null +++ b/UserInterface/Views/GeneralSettings.axaml.cs @@ -0,0 +1,66 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using Microsoft.Extensions.DependencyInjection; +using hass_workstation_service.Communication.NamedPipe; +using JKang.IpcServiceFramework.Client; +using System.Threading.Tasks; +using Avalonia.Interactivity; +using System.Reactive.Linq; +using UserInterface.ViewModels; +using System.Security; +using hass_workstation_service.Communication.InterProcesCommunication.Models; +using System.ComponentModel.DataAnnotations; +using System.Collections.Generic; +using hass_workstation_service.Data; + +namespace UserInterface.Views +{ + public class GeneralSettingsView : UserControl + { + private readonly IIpcClient client; + + public GeneralSettingsView() + { + this.InitializeComponent(); + // register IPC clients + ServiceProvider serviceProvider = new ServiceCollection() + .AddNamedPipeIpcClient("general", pipeName: "pipeinternal") + .BuildServiceProvider(); + + // resolve IPC client factory + IIpcClientFactory clientFactory = serviceProvider + .GetRequiredService>(); + + // create client + this.client = clientFactory.CreateClient("general"); + + + DataContext = new GeneralSettingsViewModel(); + GetSettings(); + + } + + public void Configure(object sender, RoutedEventArgs args) + { + var model = (GeneralSettingsViewModel)this.DataContext; + ICollection results; + if (model.IsValid(model, out results)) + { + var result = this.client.InvokeAsync(x => x.WriteGeneralSettings(new GeneralSettings() { NamePrefix = model.NamePrefix })); + } + } + + public async void GetSettings() + { + GeneralSettings settings = await this.client.InvokeAsync(x => x.GetGeneralSettings()); + ((GeneralSettingsViewModel)this.DataContext).Update(settings); + } + + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + } +} diff --git a/UserInterface/Views/MainWindow.axaml b/UserInterface/Views/MainWindow.axaml index 6c8b69b..d559359 100644 --- a/UserInterface/Views/MainWindow.axaml +++ b/UserInterface/Views/MainWindow.axaml @@ -17,6 +17,7 @@ + diff --git a/hass-workstation-service/Communication/InterProcesCommunication/IServiceContractInterfaces.cs b/hass-workstation-service/Communication/InterProcesCommunication/IServiceContractInterfaces.cs index 4252e75..ed3f329 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/IServiceContractInterfaces.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/IServiceContractInterfaces.cs @@ -1,4 +1,5 @@ using hass_workstation_service.Communication.InterProcesCommunication.Models; +using hass_workstation_service.Data; using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -8,6 +9,8 @@ namespace hass_workstation_service.Communication.NamedPipe public interface IServiceContractInterfaces { Task GetMqttBrokerSettings(); + Task GetGeneralSettings(); + void WriteGeneralSettings(GeneralSettings settings); public string Ping(string str); void WriteMqttBrokerSettingsAsync(MqttSettings settings); MqqtClientStatus GetMqqtClientStatus(); diff --git a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs index 6bc188a..e02e73e 100644 --- a/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs +++ b/hass-workstation-service/Communication/InterProcesCommunication/InterProcessApi.cs @@ -213,5 +213,10 @@ namespace hass_workstation_service.Communication.InterProcesCommunication _ => null }; } + + public Task GetGeneralSettings() => _configurationService.ReadGeneralSettings(); + + + public void WriteGeneralSettings(GeneralSettings settings) => _configurationService.WriteGeneralSettingsAsync(settings); } } \ No newline at end of file diff --git a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs index bcdd12f..286b5b9 100644 --- a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs +++ b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs @@ -31,6 +31,7 @@ namespace hass_workstation_service.Communication public DateTime LastAvailabilityAnnounce { get; private set; } public DeviceConfigModel DeviceConfigModel { get; private set; } public ICollection Subscribers { get; private set; } + public string NamePrefix { get; private set; } public bool IsConnected { get @@ -55,9 +56,11 @@ namespace hass_workstation_service.Communication this._logger = logger; this.DeviceConfigModel = deviceConfigModel; this._configurationService = configurationService; + this.NamePrefix = configurationService.GeneralSettings?.NamePrefix; var options = _configurationService.GetMqttClientOptionsAsync().Result; _configurationService.MqqtConfigChangedHandler = this.ReplaceMqttClient; + _configurationService.NamePrefixChangedHandler = this.UpdateNamePrefix; var factory = new MqttFactory(); this._mqttClient = factory.CreateManagedMqttClient(); @@ -96,9 +99,9 @@ namespace hass_workstation_service.Communication 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(AbstractDiscoverable discoverable, string domain, bool clearConfig = false) + + + public async Task AnnounceAutoDiscoveryConfig(AbstractDiscoverable discoverable, bool clearConfig = false) { if (this._mqttClient.IsConnected) { @@ -111,11 +114,24 @@ namespace hass_workstation_service.Communication }; var message = new MqttApplicationMessageBuilder() - .WithTopic($"homeassistant/{domain}/{this.DeviceConfigModel.Name}/{discoverable.ObjectId}/config") + .WithTopic($"homeassistant/{discoverable.Domain}/{this.DeviceConfigModel.Name}/{this.NamePrefix}{discoverable.ObjectId}/config") .WithPayload(clearConfig ? "" : JsonSerializer.Serialize(discoverable.GetAutoDiscoveryConfig(), discoverable.GetAutoDiscoveryConfig().GetType(), options)) .WithRetainFlag() .Build(); await this.Publish(message); + // if clearconfig is true, also remove previous state messages + throw new NotImplementedException(); + // TODO: + // The nameprefix we get here is already the new one so the old messages never get deleted + if (clearConfig) + { + var stateMessage = new MqttApplicationMessageBuilder() + .WithTopic($"homeassistant/{discoverable.Domain}/{this.DeviceConfigModel.Name}/{this.NamePrefix}{discoverable.ObjectId}/") + .WithPayload("") + .WithRetainFlag() + .Build(); + await this.Publish(stateMessage); + } LastConfigAnnounce = DateTime.UtcNow; } } @@ -195,6 +211,11 @@ namespace hass_workstation_service.Communication Subscribers.Add(command); } + public void UpdateNamePrefix(string prefix) + { + this.NamePrefix = prefix; + } + private void HandleMessageReceived(MqttApplicationMessage applicationMessage) { foreach (AbstractCommand command in this.Subscribers) diff --git a/hass-workstation-service/Data/ConfigurationService.cs b/hass-workstation-service/Data/ConfigurationService.cs index 7b63668..30536a5 100644 --- a/hass-workstation-service/Data/ConfigurationService.cs +++ b/hass-workstation-service/Data/ConfigurationService.cs @@ -26,36 +26,49 @@ namespace hass_workstation_service.Data { public ICollection ConfiguredSensors { get; private set; } public ICollection ConfiguredCommands { get; private set; } + public GeneralSettings GeneralSettings { get; private set; } public Action MqqtConfigChangedHandler { get; set; } + public Action NamePrefixChangedHandler { get; set; } private readonly DeviceConfigModel _deviceConfigModel; private bool BrokerSettingsFileLocked { get; set; } private bool SensorsSettingsFileLocked { get; set; } private bool CommandSettingsFileLocked { get; set; } + private bool GeneralSettingsFileLocked { get; set; } private bool _sensorsLoading { get; set; } private readonly string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Hass Workstation Service"); + private const string MQTT_SETTINGS_FILENAME = "mqttbroker.json"; + private const string SENSORS_SETTINGS_FILENAME = "configured-sensors.json"; + private const string COMMANDS_SETTINGS_FILENAME = "configured-commands.json"; + private const string GENERAL_SETTINGS_FILENAME = "general-settings.json"; + public ConfigurationService(DeviceConfigModel deviceConfigModel) { this._deviceConfigModel = deviceConfigModel; - if (!File.Exists(Path.Combine(path, "mqttbroker.json"))) + if (!File.Exists(Path.Combine(path, MQTT_SETTINGS_FILENAME))) { - File.Create(Path.Combine(path, "mqttbroker.json")).Close(); + File.Create(Path.Combine(path, MQTT_SETTINGS_FILENAME)).Close(); } - if (!File.Exists(Path.Combine(path, "configured-sensors.json"))) + if (!File.Exists(Path.Combine(path, SENSORS_SETTINGS_FILENAME))) { - File.Create(Path.Combine(path, "configured-sensors.json")).Close(); + File.Create(Path.Combine(path, SENSORS_SETTINGS_FILENAME)).Close(); } - if (!File.Exists(Path.Combine(path, "configured-commands.json"))) + if (!File.Exists(Path.Combine(path, COMMANDS_SETTINGS_FILENAME))) + { + File.Create(Path.Combine(path, COMMANDS_SETTINGS_FILENAME)).Close(); + } + if (!File.Exists(Path.Combine(path, GENERAL_SETTINGS_FILENAME))) { - File.Create(Path.Combine(path, "configured-commands.json")).Close(); + File.Create(Path.Combine(path, GENERAL_SETTINGS_FILENAME)).Close(); } ConfiguredSensors = new List(); ConfiguredCommands = new List(); + this.ReadGeneralSettings(); } public async void ReadSensorSettings(MqttPublisher publisher) @@ -67,7 +80,7 @@ namespace hass_workstation_service.Data } this.SensorsSettingsFileLocked = true; List sensors = new List(); - using (var stream = new FileStream(Path.Combine(path, "configured-sensors.json"), FileMode.Open)) + using (var stream = new FileStream(Path.Combine(path, SENSORS_SETTINGS_FILENAME), FileMode.Open)) { Log.Logger.Information($"reading configured sensors from: {stream.Name}"); if (stream.Length > 0) @@ -155,7 +168,7 @@ namespace hass_workstation_service.Data } this.CommandSettingsFileLocked = true; List commands = new List(); - using (var stream = new FileStream(Path.Combine(path, "configured-commands.json"), FileMode.Open)) + using (var stream = new FileStream(Path.Combine(path, COMMANDS_SETTINGS_FILENAME), FileMode.Open)) { Log.Logger.Information($"reading configured commands from: {stream.Name}"); if (stream.Length > 0) @@ -215,6 +228,70 @@ namespace hass_workstation_service.Data } } + public async Task ReadGeneralSettings() + { + while (this.GeneralSettingsFileLocked) + { + await Task.Delay(500); + } + this.GeneralSettingsFileLocked = true; + GeneralSettings settings = new GeneralSettings(); + using (var stream = new FileStream(Path.Combine(path, GENERAL_SETTINGS_FILENAME), FileMode.Open)) + { + Log.Logger.Information($"reading general settings from: {stream.Name}"); + if (stream.Length > 0) + { + settings = await JsonSerializer.DeserializeAsync(stream); + } + stream.Close(); + this.GeneralSettings = settings; + this.GeneralSettingsFileLocked = false; + return settings; + } + } + + /// + /// Writes provided settings to the config file and reconfigures all sensors and commands if the nameprefix changed + /// + /// + public async void WriteGeneralSettingsAsync(GeneralSettings settings) + { + while (this.GeneralSettingsFileLocked) + { + await Task.Delay(500); + } + this.GeneralSettingsFileLocked = true; + using (FileStream stream = new FileStream(Path.Combine(path, GENERAL_SETTINGS_FILENAME), FileMode.Open)) + { + stream.SetLength(0); + Log.Logger.Information($"writing general settings to: {stream.Name}"); + + + await JsonSerializer.SerializeAsync(stream, settings); + stream.Close(); + } + this.GeneralSettingsFileLocked = false; + + // if the nameprefix changed, we need to update all sensors and commands to reflect the new name + if (settings.NamePrefix != this.GeneralSettings.NamePrefix) + { + // notify the mqtt publisher of the new prefix + this.NamePrefixChangedHandler.Invoke(settings.NamePrefix); + + foreach (AbstractSensor sensor in this.ConfiguredSensors) + { + await sensor.UnPublishAutoDiscoveryConfigAsync(); + sensor.PublishAutoDiscoveryConfigAsync(); + } + + foreach (AbstractCommand command in this.ConfiguredCommands) + { + await command.UnPublishAutoDiscoveryConfigAsync(); + command.PublishAutoDiscoveryConfigAsync(); + } + } + } + public async Task GetMqttClientOptionsAsync() { ConfiguredMqttBroker configuredBroker = await ReadMqttSettingsAsync(); @@ -257,7 +334,7 @@ namespace hass_workstation_service.Data } this.BrokerSettingsFileLocked = true; ConfiguredMqttBroker configuredBroker = null; - using (FileStream stream = new FileStream(Path.Combine(path, "mqttbroker.json"), FileMode.Open)) + using (FileStream stream = new FileStream(Path.Combine(path, MQTT_SETTINGS_FILENAME), FileMode.Open)) { Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}"); if (stream.Length > 0) @@ -279,7 +356,7 @@ namespace hass_workstation_service.Data } this.SensorsSettingsFileLocked = true; List configuredSensorsToSave = new List(); - using (FileStream stream = new FileStream(Path.Combine(path, "configured-sensors.json"), FileMode.Open)) + using (FileStream stream = new FileStream(Path.Combine(path, SENSORS_SETTINGS_FILENAME), FileMode.Open)) { stream.SetLength(0); Log.Logger.Information($"writing configured sensors to: {stream.Name}"); @@ -316,7 +393,7 @@ namespace hass_workstation_service.Data } this.CommandSettingsFileLocked = true; List configuredCommandsToSave = new List(); - using (FileStream stream = new FileStream(Path.Combine(path, "configured-commands.json"), FileMode.Open)) + using (FileStream stream = new FileStream(Path.Combine(path, COMMANDS_SETTINGS_FILENAME), FileMode.Open)) { stream.SetLength(0); Log.Logger.Information($"writing configured commands to: {stream.Name}"); @@ -374,6 +451,11 @@ namespace hass_workstation_service.Data WriteCommandSettingsAsync(); } + /// + /// + /// + /// The Id of the sensor to replace + /// The new sensor public async void UpdateConfiguredSensor(Guid id, AbstractSensor sensor) { await DeleteSensor(id); @@ -438,7 +520,7 @@ namespace hass_workstation_service.Data await Task.Delay(500); } this.BrokerSettingsFileLocked = true; - using (FileStream stream = new FileStream(Path.Combine(path, "mqttbroker.json"), FileMode.Open)) + using (FileStream stream = new FileStream(Path.Combine(path, MQTT_SETTINGS_FILENAME), FileMode.Open)) { stream.SetLength(0); Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}"); diff --git a/hass-workstation-service/Data/GeneralSettings.cs b/hass-workstation-service/Data/GeneralSettings.cs new file mode 100644 index 0000000..c2cc869 --- /dev/null +++ b/hass-workstation-service/Data/GeneralSettings.cs @@ -0,0 +1,13 @@ +using hass_workstation_service.Domain.Sensors; +using System; + +namespace hass_workstation_service.Data +{ + public class GeneralSettings + { + /// + /// If set, all sensor and command names will be be prefixed with this + /// + public string NamePrefix { get; set; } + } +} \ No newline at end of file diff --git a/hass-workstation-service/Data/IConfigurationService.cs b/hass-workstation-service/Data/IConfigurationService.cs index b2b63e5..1215ee1 100644 --- a/hass-workstation-service/Data/IConfigurationService.cs +++ b/hass-workstation-service/Data/IConfigurationService.cs @@ -14,8 +14,10 @@ namespace hass_workstation_service.Data public interface IConfigurationService { Action MqqtConfigChangedHandler { get; set; } + Action NamePrefixChangedHandler { get; set; } ICollection ConfiguredSensors { get; } ICollection ConfiguredCommands { get; } + GeneralSettings GeneralSettings { get; } void AddConfiguredSensor(AbstractSensor sensor); void AddConfiguredCommand(AbstractCommand command); @@ -23,6 +25,7 @@ namespace hass_workstation_service.Data void AddConfiguredCommands(List commands); void DeleteConfiguredSensor(Guid id); void DeleteConfiguredCommand(Guid id); + Task ReadGeneralSettings(); void UpdateConfiguredSensor(Guid id, AbstractSensor sensor); void UpdateConfiguredCommand(Guid id, AbstractCommand command); Task GetMqttClientOptionsAsync(); @@ -35,5 +38,6 @@ namespace hass_workstation_service.Data void WriteCommandSettingsAsync(); void ReadCommandSettings(MqttPublisher publisher); Task> GetSensorsAfterLoadingAsync(); + void WriteGeneralSettingsAsync(GeneralSettings settings); } } \ No newline at end of file diff --git a/hass-workstation-service/Domain/Commands/AbstractCommand.cs b/hass-workstation-service/Domain/Commands/AbstractCommand.cs index c711408..94df49d 100644 --- a/hass-workstation-service/Domain/Commands/AbstractCommand.cs +++ b/hass-workstation-service/Domain/Commands/AbstractCommand.cs @@ -48,9 +48,9 @@ namespace hass_workstation_service.Domain.Commands LastUpdated = DateTime.UtcNow; } - public async void PublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, Domain); + public async void PublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this); - public async Task UnPublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, Domain, true); + public async Task UnPublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, true); protected CommandDiscoveryConfigModel _autoDiscoveryConfigModel; protected CommandDiscoveryConfigModel SetAutoDiscoveryConfigModel(CommandDiscoveryConfigModel config) diff --git a/hass-workstation-service/Domain/Commands/CustomCommand.cs b/hass-workstation-service/Domain/Commands/CustomCommand.cs index 2cc1a9a..cdcc827 100644 --- a/hass-workstation-service/Domain/Commands/CustomCommand.cs +++ b/hass-workstation-service/Domain/Commands/CustomCommand.cs @@ -56,8 +56,8 @@ namespace hass_workstation_service.Domain.Commands Name = this.Name, Unique_id = this.Id.ToString(), Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", - Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/set", - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Device = this.Publisher.DeviceConfigModel, }; } diff --git a/hass-workstation-service/Domain/Commands/KeyCommand.cs b/hass-workstation-service/Domain/Commands/KeyCommand.cs index 4da4e5a..928f45d 100644 --- a/hass-workstation-service/Domain/Commands/KeyCommand.cs +++ b/hass-workstation-service/Domain/Commands/KeyCommand.cs @@ -32,8 +32,8 @@ namespace hass_workstation_service.Domain.Commands Name = this.Name, Unique_id = this.Id.ToString(), Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", - Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/set", - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Device = this.Publisher.DeviceConfigModel, }; } diff --git a/hass-workstation-service/Domain/Sensors/AbstractSensor.cs b/hass-workstation-service/Domain/Sensors/AbstractSensor.cs index 50c52a8..47e263e 100644 --- a/hass-workstation-service/Domain/Sensors/AbstractSensor.cs +++ b/hass-workstation-service/Domain/Sensors/AbstractSensor.cs @@ -48,9 +48,13 @@ namespace hass_workstation_service.Domain.Sensors LastUpdated = DateTime.UtcNow; } - public async void PublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, Domain); + public async void PublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this); - public async Task UnPublishAutoDiscoveryConfigAsync() => await Publisher.AnnounceAutoDiscoveryConfig(this, Domain, true); + public async Task UnPublishAutoDiscoveryConfigAsync() + { + await Publisher.AnnounceAutoDiscoveryConfig(this, true); + this._autoDiscoveryConfigModel = null; + } protected SensorDiscoveryConfigModel _autoDiscoveryConfigModel; protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscoveryConfigModel config) diff --git a/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs b/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs index 2ab6f2c..7bd6dc0 100644 --- a/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs +++ b/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs @@ -16,7 +16,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:window-maximize", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs b/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs index c5fcf7e..3ba8923 100644 --- a/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs @@ -24,7 +24,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:chart-areaspline", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs b/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs index 56754cc..e1388ff 100644 --- a/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs @@ -16,7 +16,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:speedometer", Unit_of_measurement = "MHz", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs b/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs index 6a46bde..a202670 100644 --- a/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs @@ -25,7 +25,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:volume-medium", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/DummySensor.cs b/hass-workstation-service/Domain/Sensors/DummySensor.cs index 66eaaad..6b79485 100644 --- a/hass-workstation-service/Domain/Sensors/DummySensor.cs +++ b/hass-workstation-service/Domain/Sensors/DummySensor.cs @@ -20,7 +20,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs b/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs index 10ee77f..afca2cd 100644 --- a/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs +++ b/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs @@ -37,7 +37,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs b/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs index 6828f18..f1cc1d9 100644 --- a/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs +++ b/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs @@ -37,7 +37,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Device_class = "temperature", Unit_of_measurement = "°C", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs b/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs index a6e4ccc..769d409 100644 --- a/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs @@ -16,7 +16,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:clock-time-three-outline", Device_class = "timestamp" }); diff --git a/hass-workstation-service/Domain/Sensors/LastBootSensor.cs b/hass-workstation-service/Domain/Sensors/LastBootSensor.cs index 8b5a29c..56feb03 100644 --- a/hass-workstation-service/Domain/Sensors/LastBootSensor.cs +++ b/hass-workstation-service/Domain/Sensors/LastBootSensor.cs @@ -20,7 +20,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:clock-time-three-outline", Device_class = "timestamp" }); diff --git a/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs b/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs index 9b666ed..fcd2f72 100644 --- a/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs +++ b/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs @@ -44,7 +44,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:memory", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs b/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs index 1b46811..97b98d5 100644 --- a/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs @@ -29,7 +29,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs b/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs index 095b5bd..2a947ec 100644 --- a/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs +++ b/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs @@ -24,7 +24,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs index 650de5f..2265a5f 100644 --- a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs +++ b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs @@ -44,7 +44,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:lock", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs b/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs index 793df7e..a4dde79 100644 --- a/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs +++ b/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs @@ -16,7 +16,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.Name}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Icon = "mdi:laptop", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs index d2601b6..b542e67 100644 --- a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs +++ b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs @@ -27,7 +27,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.Name}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs b/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs index 598d3f9..5b94466 100644 --- a/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs @@ -32,7 +32,7 @@ namespace hass_workstation_service.Domain.Sensors Name = this.Name, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.Name}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Program.cs b/hass-workstation-service/Program.cs index ed90371..72b9cfa 100644 --- a/hass-workstation-service/Program.cs +++ b/hass-workstation-service/Program.cs @@ -70,7 +70,6 @@ namespace hass_workstation_service } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) - .ConfigureLogging((hostContext, loggingBuilder) => loggingBuilder.AddSerilog(dispose: true)) .ConfigureServices((hostContext, services) => From 508ed3ae5a72468153eb6b398f1a348cf8a193e3 Mon Sep 17 00:00:00 2001 From: Sleevezipper Date: Sat, 16 Oct 2021 22:25:16 +0200 Subject: [PATCH 2/2] implement name prefix --- UserInterface/Views/GeneralSettings.axaml | 18 ++++++++- .../Communication/MQTT/MqttPublisher.cs | 7 +--- .../MQTT/SensorDiscoveryConfigModel.cs | 38 +++++++++++++++++++ .../Domain/Commands/CustomCommand.cs | 3 +- .../Domain/Commands/KeyCommand.cs | 3 +- .../Domain/Sensors/AbstractSensor.cs | 10 ++--- .../Domain/Sensors/ActiveWindowSensor.cs | 3 +- .../Domain/Sensors/CPULoadSensor.cs | 3 +- .../Domain/Sensors/CurrentClockSpeedSensor.cs | 3 +- .../Domain/Sensors/CurrentVolumeSensor.cs | 3 +- .../Domain/Sensors/DummySensor.cs | 3 +- .../Domain/Sensors/GpuLoadSensor.cs | 3 +- .../Domain/Sensors/GpuTemperatureSensor.cs | 3 +- .../Domain/Sensors/LastActiveSensor.cs | 3 +- .../Domain/Sensors/LastBootSensor.cs | 3 +- .../Domain/Sensors/MemoryUsageSensor.cs | 3 +- .../Domain/Sensors/MicrophoneActiveSensor.cs | 3 +- .../Domain/Sensors/NamedWindowSensor.cs | 5 ++- .../Domain/Sensors/SessionStateSensor.cs | 3 +- .../Sensors/UserNotificationStateSensor.cs | 3 +- .../Domain/Sensors/WMIQuerySensor.cs | 3 +- .../Domain/Sensors/WebcamActiveSensor.cs | 3 +- 22 files changed, 98 insertions(+), 31 deletions(-) diff --git a/UserInterface/Views/GeneralSettings.axaml b/UserInterface/Views/GeneralSettings.axaml index baab826..4ca197d 100644 --- a/UserInterface/Views/GeneralSettings.axaml +++ b/UserInterface/Views/GeneralSettings.axaml @@ -6,8 +6,22 @@ x:Class="UserInterface.Views.GeneralSettingsView"> Settings - IP address or hostname - + + Name prefix + (What's this?) + + + +[Experimental] +This allows you to set a name which will be used to prefix all sensor- and command names. For example: +If a sensor is called "ActiveWindow" and the name prefix is set to "laptop", the sensor will be named "laptop-ActiveWindow" and its entityId will be "laptop_activewindow". + + + + + + + diff --git a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs index 286b5b9..7449d67 100644 --- a/hass-workstation-service/Communication/MQTT/MqttPublisher.cs +++ b/hass-workstation-service/Communication/MQTT/MqttPublisher.cs @@ -114,19 +114,16 @@ namespace hass_workstation_service.Communication }; var message = new MqttApplicationMessageBuilder() - .WithTopic($"homeassistant/{discoverable.Domain}/{this.DeviceConfigModel.Name}/{this.NamePrefix}{discoverable.ObjectId}/config") + .WithTopic($"homeassistant/{discoverable.Domain}/{this.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(discoverable.GetAutoDiscoveryConfig().NamePrefix, discoverable.ObjectId)}/config") .WithPayload(clearConfig ? "" : JsonSerializer.Serialize(discoverable.GetAutoDiscoveryConfig(), discoverable.GetAutoDiscoveryConfig().GetType(), options)) .WithRetainFlag() .Build(); await this.Publish(message); // if clearconfig is true, also remove previous state messages - throw new NotImplementedException(); - // TODO: - // The nameprefix we get here is already the new one so the old messages never get deleted if (clearConfig) { var stateMessage = new MqttApplicationMessageBuilder() - .WithTopic($"homeassistant/{discoverable.Domain}/{this.DeviceConfigModel.Name}/{this.NamePrefix}{discoverable.ObjectId}/") + .WithTopic($"homeassistant/{discoverable.Domain}/{this.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(discoverable.GetAutoDiscoveryConfig().NamePrefix, discoverable.ObjectId)}/state") .WithPayload("") .WithRetainFlag() .Build(); diff --git a/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs b/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs index 3d942e1..7626fda 100644 --- a/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs +++ b/hass-workstation-service/Communication/MQTT/SensorDiscoveryConfigModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text.Json.Serialization; namespace hass_workstation_service.Communication { @@ -14,12 +15,49 @@ namespace hass_workstation_service.Communication /// (Optional) The name of the MQTT sensor. Defaults to MQTT Sensor in hass. /// /// + [JsonIgnore] public string Name { get; set; } + /// + /// (Optional) The first part of the name. + /// + /// + [JsonIgnore] + public string NamePrefix { get; set; } + + [JsonPropertyName("name")] + public string CompiledName { get => GetName(); } + /// /// The MQTT topic subscribed to receive sensor values. /// /// public string State_topic { get; set; } + + /// + /// Gets the name including the prefix + /// + /// + public string GetName() + { + if (string.IsNullOrWhiteSpace(NamePrefix)) + { + return Name; + } + return $"{NamePrefix}-{Name}"; + } + + /// + /// Gets the name including the prefix if the class has not been instantiated yet. + /// + /// + public static string GetNameWithPrefix(string namePrefix, string name) + { + if (string.IsNullOrWhiteSpace(namePrefix)) + { + return name; + } + return $"{namePrefix}-{name}"; + } } public class SensorDiscoveryConfigModel : DiscoveryConfigModel { diff --git a/hass-workstation-service/Domain/Commands/CustomCommand.cs b/hass-workstation-service/Domain/Commands/CustomCommand.cs index cdcc827..f4dced8 100644 --- a/hass-workstation-service/Domain/Commands/CustomCommand.cs +++ b/hass-workstation-service/Domain/Commands/CustomCommand.cs @@ -54,10 +54,11 @@ namespace hass_workstation_service.Domain.Commands return new CommandDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set", - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Device = this.Publisher.DeviceConfigModel, }; } diff --git a/hass-workstation-service/Domain/Commands/KeyCommand.cs b/hass-workstation-service/Domain/Commands/KeyCommand.cs index 928f45d..c0b560f 100644 --- a/hass-workstation-service/Domain/Commands/KeyCommand.cs +++ b/hass-workstation-service/Domain/Commands/KeyCommand.cs @@ -30,10 +30,11 @@ namespace hass_workstation_service.Domain.Commands return new CommandDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set", - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Device = this.Publisher.DeviceConfigModel, }; } diff --git a/hass-workstation-service/Domain/Sensors/AbstractSensor.cs b/hass-workstation-service/Domain/Sensors/AbstractSensor.cs index 47e263e..73904bf 100644 --- a/hass-workstation-service/Domain/Sensors/AbstractSensor.cs +++ b/hass-workstation-service/Domain/Sensors/AbstractSensor.cs @@ -38,11 +38,11 @@ namespace hass_workstation_service.Domain.Sensors return; var message = new MqttApplicationMessageBuilder() - .WithTopic(GetAutoDiscoveryConfig().State_topic) - .WithPayload(state) - .WithExactlyOnceQoS() - .WithRetainFlag() - .Build(); + .WithTopic(GetAutoDiscoveryConfig().State_topic) + .WithPayload(state) + .WithExactlyOnceQoS() + .WithRetainFlag() + .Build(); await Publisher.Publish(message); PreviousPublishedState = state; LastUpdated = DateTime.UtcNow; diff --git a/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs b/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs index 7bd6dc0..26ea9c2 100644 --- a/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs +++ b/hass-workstation-service/Domain/Sensors/ActiveWindowSensor.cs @@ -14,9 +14,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:window-maximize", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs b/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs index 3ba8923..eb3752c 100644 --- a/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CPULoadSensor.cs @@ -22,9 +22,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:chart-areaspline", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs b/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs index e1388ff..7af44c4 100644 --- a/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CurrentClockSpeedSensor.cs @@ -14,9 +14,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:speedometer", Unit_of_measurement = "MHz", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs b/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs index a202670..2d01976 100644 --- a/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs +++ b/hass-workstation-service/Domain/Sensors/CurrentVolumeSensor.cs @@ -23,9 +23,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:volume-medium", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/DummySensor.cs b/hass-workstation-service/Domain/Sensors/DummySensor.cs index 6b79485..77ffc9d 100644 --- a/hass-workstation-service/Domain/Sensors/DummySensor.cs +++ b/hass-workstation-service/Domain/Sensors/DummySensor.cs @@ -18,9 +18,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs b/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs index afca2cd..91e5a66 100644 --- a/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs +++ b/hass-workstation-service/Domain/Sensors/GpuLoadSensor.cs @@ -35,9 +35,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs b/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs index f1cc1d9..f16edda 100644 --- a/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs +++ b/hass-workstation-service/Domain/Sensors/GpuTemperatureSensor.cs @@ -35,9 +35,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Device_class = "temperature", Unit_of_measurement = "°C", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs b/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs index 769d409..9aa38bc 100644 --- a/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/LastActiveSensor.cs @@ -14,9 +14,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:clock-time-three-outline", Device_class = "timestamp" }); diff --git a/hass-workstation-service/Domain/Sensors/LastBootSensor.cs b/hass-workstation-service/Domain/Sensors/LastBootSensor.cs index 56feb03..fda171c 100644 --- a/hass-workstation-service/Domain/Sensors/LastBootSensor.cs +++ b/hass-workstation-service/Domain/Sensors/LastBootSensor.cs @@ -18,9 +18,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:clock-time-three-outline", Device_class = "timestamp" }); diff --git a/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs b/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs index fcd2f72..7b75cee 100644 --- a/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs +++ b/hass-workstation-service/Domain/Sensors/MemoryUsageSensor.cs @@ -42,9 +42,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:memory", Unit_of_measurement = "%", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" diff --git a/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs b/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs index 97b98d5..7c94e54 100644 --- a/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/MicrophoneActiveSensor.cs @@ -27,9 +27,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs b/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs index 2a947ec..7194a9e 100644 --- a/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs +++ b/hass-workstation-service/Domain/Sensors/NamedWindowSensor.cs @@ -22,11 +22,12 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" - }); + });; } public override string GetState() diff --git a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs index 2265a5f..59c6ee2 100644 --- a/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs +++ b/hass-workstation-service/Domain/Sensors/SessionStateSensor.cs @@ -42,9 +42,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:lock", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs b/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs index a4dde79..942c0f5 100644 --- a/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs +++ b/hass-workstation-service/Domain/Sensors/UserNotificationStateSensor.cs @@ -14,9 +14,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Icon = "mdi:laptop", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); diff --git a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs index b542e67..a0b6984 100644 --- a/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs +++ b/hass-workstation-service/Domain/Sensors/WMIQuerySensor.cs @@ -25,9 +25,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" }); } diff --git a/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs b/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs index 5b94466..0e2d440 100644 --- a/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs +++ b/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs @@ -30,9 +30,10 @@ namespace hass_workstation_service.Domain.Sensors return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel() { Name = this.Name, + NamePrefix = Publisher.NamePrefix, Unique_id = this.Id.ToString(), Device = this.Publisher.DeviceConfigModel, - State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state", + State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{DiscoveryConfigModel.GetNameWithPrefix(Publisher.NamePrefix, this.ObjectId)}/state", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" }); }