wip: name prefix

name-prefix
Sleevezipper 3 years ago
parent 5c95dc833f
commit 8425970c8d

@ -32,6 +32,9 @@
<Compile Update="Views\BackgroundServiceSettings.axaml.cs"> <Compile Update="Views\BackgroundServiceSettings.axaml.cs">
<DependentUpon>BackgroundServiceSettings.axaml</DependentUpon> <DependentUpon>BackgroundServiceSettings.axaml</DependentUpon>
</Compile> </Compile>
<Compile Update="Views\GeneralSettings.axaml.cs">
<DependentUpon>GeneralSettings.axaml</DependentUpon>
</Compile>
<Compile Update="Views\CommandSettings.axaml.cs"> <Compile Update="Views\CommandSettings.axaml.cs">
<DependentUpon>CommandSettings.axaml</DependentUpon> <DependentUpon>CommandSettings.axaml</DependentUpon>
</Compile> </Compile>

@ -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;
}
}
}

@ -0,0 +1,13 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450"
x:Class="UserInterface.Views.GeneralSettingsView">
<StackPanel Margin="30" HorizontalAlignment="Left">
<ContentControl FontSize="18" FontWeight="Bold">Settings</ContentControl>
<ContentControl Margin="0 20 0 10">IP address or hostname</ContentControl>
<TextBox Text="{Binding NamePrefix}" HorizontalAlignment="Left" Width="100" Watermark="laptop-"/>
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Configure">Save</Button>
</StackPanel>
</UserControl>

@ -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<IServiceContractInterfaces> client;
public GeneralSettingsView()
{
this.InitializeComponent();
// register IPC clients
ServiceProvider serviceProvider = new ServiceCollection()
.AddNamedPipeIpcClient<IServiceContractInterfaces>("general", pipeName: "pipeinternal")
.BuildServiceProvider();
// resolve IPC client factory
IIpcClientFactory<IServiceContractInterfaces> clientFactory = serviceProvider
.GetRequiredService<IIpcClientFactory<IServiceContractInterfaces>>();
// create client
this.client = clientFactory.CreateClient("general");
DataContext = new GeneralSettingsViewModel();
GetSettings();
}
public void Configure(object sender, RoutedEventArgs args)
{
var model = (GeneralSettingsViewModel)this.DataContext;
ICollection<ValidationResult> 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);
}
}
}

@ -17,6 +17,7 @@
<Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*, *" Margin="10"> <Grid ColumnDefinitions="Auto, *, Auto" RowDefinitions="*, *" Margin="10">
<views:BrokerSettings Grid.Column="0" Grid.Row="0" Margin="10" Grid.RowSpan="2" Background="#2D2D30"/> <views:BrokerSettings Grid.Column="0" Grid.Row="0" Margin="10" Grid.RowSpan="2" Background="#2D2D30"/>
<views:GeneralSettingsView Grid.Column="0" Grid.Row="1" Margin="10" Grid.RowSpan="2" Background="#2D2D30"/>
<views:SensorSettings Grid.Column="1" Grid.Row="0" Margin="10" Background="#2D2D30"/> <views:SensorSettings Grid.Column="1" Grid.Row="0" Margin="10" Background="#2D2D30"/>
<views:CommandSettings Grid.Column="1" Grid.Row="1" Margin="10" Background="#2D2D30"/> <views:CommandSettings Grid.Column="1" Grid.Row="1" Margin="10" Background="#2D2D30"/>
<views:BackgroundServiceSettings Grid.Column="2" Grid.Row="0" Margin="10" Background="#2D2D30"/> <views:BackgroundServiceSettings Grid.Column="2" Grid.Row="0" Margin="10" Background="#2D2D30"/>

@ -1,4 +1,5 @@
using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.InterProcesCommunication.Models;
using hass_workstation_service.Data;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -8,6 +9,8 @@ namespace hass_workstation_service.Communication.NamedPipe
public interface IServiceContractInterfaces public interface IServiceContractInterfaces
{ {
Task<MqttSettings> GetMqttBrokerSettings(); Task<MqttSettings> GetMqttBrokerSettings();
Task<GeneralSettings> GetGeneralSettings();
void WriteGeneralSettings(GeneralSettings settings);
public string Ping(string str); public string Ping(string str);
void WriteMqttBrokerSettingsAsync(MqttSettings settings); void WriteMqttBrokerSettingsAsync(MqttSettings settings);
MqqtClientStatus GetMqqtClientStatus(); MqqtClientStatus GetMqqtClientStatus();

@ -213,5 +213,10 @@ namespace hass_workstation_service.Communication.InterProcesCommunication
_ => null _ => null
}; };
} }
public Task<GeneralSettings> GetGeneralSettings() => _configurationService.ReadGeneralSettings();
public void WriteGeneralSettings(GeneralSettings settings) => _configurationService.WriteGeneralSettingsAsync(settings);
} }
} }

@ -31,6 +31,7 @@ namespace hass_workstation_service.Communication
public DateTime LastAvailabilityAnnounce { get; private set; } public DateTime LastAvailabilityAnnounce { get; private set; }
public DeviceConfigModel DeviceConfigModel { get; private set; } public DeviceConfigModel DeviceConfigModel { get; private set; }
public ICollection<AbstractCommand> Subscribers { get; private set; } public ICollection<AbstractCommand> Subscribers { get; private set; }
public string NamePrefix { get; private set; }
public bool IsConnected public bool IsConnected
{ {
get get
@ -55,9 +56,11 @@ namespace hass_workstation_service.Communication
this._logger = logger; this._logger = logger;
this.DeviceConfigModel = deviceConfigModel; this.DeviceConfigModel = deviceConfigModel;
this._configurationService = configurationService; this._configurationService = configurationService;
this.NamePrefix = configurationService.GeneralSettings?.NamePrefix;
var options = _configurationService.GetMqttClientOptionsAsync().Result; var options = _configurationService.GetMqttClientOptionsAsync().Result;
_configurationService.MqqtConfigChangedHandler = this.ReplaceMqttClient; _configurationService.MqqtConfigChangedHandler = this.ReplaceMqttClient;
_configurationService.NamePrefixChangedHandler = this.UpdateNamePrefix;
var factory = new MqttFactory(); var factory = new MqttFactory();
this._mqttClient = factory.CreateManagedMqttClient(); this._mqttClient = factory.CreateManagedMqttClient();
@ -96,9 +99,9 @@ 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(AbstractDiscoverable discoverable, string domain, bool clearConfig = false) public async Task AnnounceAutoDiscoveryConfig(AbstractDiscoverable discoverable, bool clearConfig = false)
{ {
if (this._mqttClient.IsConnected) if (this._mqttClient.IsConnected)
{ {
@ -111,11 +114,24 @@ namespace hass_workstation_service.Communication
}; };
var message = new MqttApplicationMessageBuilder() 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)) .WithPayload(clearConfig ? "" : JsonSerializer.Serialize(discoverable.GetAutoDiscoveryConfig(), discoverable.GetAutoDiscoveryConfig().GetType(), options))
.WithRetainFlag() .WithRetainFlag()
.Build(); .Build();
await this.Publish(message); 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; LastConfigAnnounce = DateTime.UtcNow;
} }
} }
@ -195,6 +211,11 @@ namespace hass_workstation_service.Communication
Subscribers.Add(command); Subscribers.Add(command);
} }
public void UpdateNamePrefix(string prefix)
{
this.NamePrefix = prefix;
}
private void HandleMessageReceived(MqttApplicationMessage applicationMessage) private void HandleMessageReceived(MqttApplicationMessage applicationMessage)
{ {
foreach (AbstractCommand command in this.Subscribers) foreach (AbstractCommand command in this.Subscribers)

@ -26,36 +26,49 @@ namespace hass_workstation_service.Data
{ {
public ICollection<AbstractSensor> ConfiguredSensors { get; private set; } public ICollection<AbstractSensor> ConfiguredSensors { get; private set; }
public ICollection<AbstractCommand> ConfiguredCommands { get; private set; } public ICollection<AbstractCommand> ConfiguredCommands { get; private set; }
public GeneralSettings GeneralSettings { get; private set; }
public Action<IManagedMqttClientOptions> MqqtConfigChangedHandler { get; set; } public Action<IManagedMqttClientOptions> MqqtConfigChangedHandler { get; set; }
public Action<string> NamePrefixChangedHandler { get; set; }
private readonly DeviceConfigModel _deviceConfigModel; private readonly DeviceConfigModel _deviceConfigModel;
private bool BrokerSettingsFileLocked { get; set; } private bool BrokerSettingsFileLocked { get; set; }
private bool SensorsSettingsFileLocked { get; set; } private bool SensorsSettingsFileLocked { get; set; }
private bool CommandSettingsFileLocked { get; set; } private bool CommandSettingsFileLocked { get; set; }
private bool GeneralSettingsFileLocked { get; set; }
private bool _sensorsLoading { get; set; } private bool _sensorsLoading { get; set; }
private readonly string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Hass Workstation Service"); 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) public ConfigurationService(DeviceConfigModel deviceConfigModel)
{ {
this._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<AbstractSensor>(); ConfiguredSensors = new List<AbstractSensor>();
ConfiguredCommands = new List<AbstractCommand>(); ConfiguredCommands = new List<AbstractCommand>();
this.ReadGeneralSettings();
} }
public async void ReadSensorSettings(MqttPublisher publisher) public async void ReadSensorSettings(MqttPublisher publisher)
@ -67,7 +80,7 @@ namespace hass_workstation_service.Data
} }
this.SensorsSettingsFileLocked = true; this.SensorsSettingsFileLocked = true;
List<ConfiguredSensor> sensors = new List<ConfiguredSensor>(); List<ConfiguredSensor> sensors = new List<ConfiguredSensor>();
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}"); Log.Logger.Information($"reading configured sensors from: {stream.Name}");
if (stream.Length > 0) if (stream.Length > 0)
@ -155,7 +168,7 @@ namespace hass_workstation_service.Data
} }
this.CommandSettingsFileLocked = true; this.CommandSettingsFileLocked = true;
List<ConfiguredCommand> commands = new List<ConfiguredCommand>(); List<ConfiguredCommand> commands = new List<ConfiguredCommand>();
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}"); Log.Logger.Information($"reading configured commands from: {stream.Name}");
if (stream.Length > 0) if (stream.Length > 0)
@ -215,6 +228,70 @@ namespace hass_workstation_service.Data
} }
} }
public async Task<GeneralSettings> 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<GeneralSettings>(stream);
}
stream.Close();
this.GeneralSettings = settings;
this.GeneralSettingsFileLocked = false;
return settings;
}
}
/// <summary>
/// Writes provided settings to the config file and reconfigures all sensors and commands if the nameprefix changed
/// </summary>
/// <param name="settings"></param>
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<IManagedMqttClientOptions> GetMqttClientOptionsAsync() public async Task<IManagedMqttClientOptions> GetMqttClientOptionsAsync()
{ {
ConfiguredMqttBroker configuredBroker = await ReadMqttSettingsAsync(); ConfiguredMqttBroker configuredBroker = await ReadMqttSettingsAsync();
@ -257,7 +334,7 @@ namespace hass_workstation_service.Data
} }
this.BrokerSettingsFileLocked = true; this.BrokerSettingsFileLocked = true;
ConfiguredMqttBroker configuredBroker = null; 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}"); Log.Logger.Information($"reading configured mqttbroker from: {stream.Name}");
if (stream.Length > 0) if (stream.Length > 0)
@ -279,7 +356,7 @@ namespace hass_workstation_service.Data
} }
this.SensorsSettingsFileLocked = true; this.SensorsSettingsFileLocked = true;
List<ConfiguredSensor> configuredSensorsToSave = new List<ConfiguredSensor>(); List<ConfiguredSensor> configuredSensorsToSave = new List<ConfiguredSensor>();
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); stream.SetLength(0);
Log.Logger.Information($"writing configured sensors to: {stream.Name}"); Log.Logger.Information($"writing configured sensors to: {stream.Name}");
@ -316,7 +393,7 @@ namespace hass_workstation_service.Data
} }
this.CommandSettingsFileLocked = true; this.CommandSettingsFileLocked = true;
List<ConfiguredCommand> configuredCommandsToSave = new List<ConfiguredCommand>(); List<ConfiguredCommand> configuredCommandsToSave = new List<ConfiguredCommand>();
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); stream.SetLength(0);
Log.Logger.Information($"writing configured commands to: {stream.Name}"); Log.Logger.Information($"writing configured commands to: {stream.Name}");
@ -374,6 +451,11 @@ namespace hass_workstation_service.Data
WriteCommandSettingsAsync(); WriteCommandSettingsAsync();
} }
/// <summary>
///
/// </summary>
/// <param name="id">The Id of the sensor to replace</param>
/// <param name="sensor">The new sensor</param>
public async void UpdateConfiguredSensor(Guid id, AbstractSensor sensor) public async void UpdateConfiguredSensor(Guid id, AbstractSensor sensor)
{ {
await DeleteSensor(id); await DeleteSensor(id);
@ -438,7 +520,7 @@ namespace hass_workstation_service.Data
await Task.Delay(500); await Task.Delay(500);
} }
this.BrokerSettingsFileLocked = true; 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); stream.SetLength(0);
Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}"); Log.Logger.Information($"writing configured mqttbroker to: {stream.Name}");

@ -0,0 +1,13 @@
using hass_workstation_service.Domain.Sensors;
using System;
namespace hass_workstation_service.Data
{
public class GeneralSettings
{
/// <summary>
/// If set, all sensor and command names will be be prefixed with this
/// </summary>
public string NamePrefix { get; set; }
}
}

@ -14,8 +14,10 @@ namespace hass_workstation_service.Data
public interface IConfigurationService public interface IConfigurationService
{ {
Action<IManagedMqttClientOptions> MqqtConfigChangedHandler { get; set; } Action<IManagedMqttClientOptions> MqqtConfigChangedHandler { get; set; }
Action<string> NamePrefixChangedHandler { get; set; }
ICollection<AbstractSensor> ConfiguredSensors { get; } ICollection<AbstractSensor> ConfiguredSensors { get; }
ICollection<AbstractCommand> ConfiguredCommands { get; } ICollection<AbstractCommand> ConfiguredCommands { get; }
GeneralSettings GeneralSettings { get; }
void AddConfiguredSensor(AbstractSensor sensor); void AddConfiguredSensor(AbstractSensor sensor);
void AddConfiguredCommand(AbstractCommand command); void AddConfiguredCommand(AbstractCommand command);
@ -23,6 +25,7 @@ namespace hass_workstation_service.Data
void AddConfiguredCommands(List<AbstractCommand> commands); void AddConfiguredCommands(List<AbstractCommand> commands);
void DeleteConfiguredSensor(Guid id); void DeleteConfiguredSensor(Guid id);
void DeleteConfiguredCommand(Guid id); void DeleteConfiguredCommand(Guid id);
Task<GeneralSettings> ReadGeneralSettings();
void UpdateConfiguredSensor(Guid id, AbstractSensor sensor); void UpdateConfiguredSensor(Guid id, AbstractSensor sensor);
void UpdateConfiguredCommand(Guid id, AbstractCommand command); void UpdateConfiguredCommand(Guid id, AbstractCommand command);
Task<IManagedMqttClientOptions> GetMqttClientOptionsAsync(); Task<IManagedMqttClientOptions> GetMqttClientOptionsAsync();
@ -35,5 +38,6 @@ namespace hass_workstation_service.Data
void WriteCommandSettingsAsync(); void WriteCommandSettingsAsync();
void ReadCommandSettings(MqttPublisher publisher); void ReadCommandSettings(MqttPublisher publisher);
Task<ICollection<AbstractSensor>> GetSensorsAfterLoadingAsync(); Task<ICollection<AbstractSensor>> GetSensorsAfterLoadingAsync();
void WriteGeneralSettingsAsync(GeneralSettings settings);
} }
} }

@ -48,9 +48,9 @@ namespace hass_workstation_service.Domain.Commands
LastUpdated = DateTime.UtcNow; 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 _autoDiscoveryConfigModel;
protected CommandDiscoveryConfigModel SetAutoDiscoveryConfigModel(CommandDiscoveryConfigModel config) protected CommandDiscoveryConfigModel SetAutoDiscoveryConfigModel(CommandDiscoveryConfigModel config)

@ -56,8 +56,8 @@ namespace hass_workstation_service.Domain.Commands
Name = this.Name, Name = this.Name,
Unique_id = this.Id.ToString(), Unique_id = this.Id.ToString(),
Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability",
Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/set", Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set",
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 = this.Publisher.DeviceConfigModel, Device = this.Publisher.DeviceConfigModel,
}; };
} }

@ -32,8 +32,8 @@ namespace hass_workstation_service.Domain.Commands
Name = this.Name, Name = this.Name,
Unique_id = this.Id.ToString(), Unique_id = this.Id.ToString(),
Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability", Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability",
Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/set", Command_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/set",
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 = this.Publisher.DeviceConfigModel, Device = this.Publisher.DeviceConfigModel,
}; };
} }

@ -48,9 +48,13 @@ namespace hass_workstation_service.Domain.Sensors
LastUpdated = DateTime.UtcNow; 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 _autoDiscoveryConfigModel;
protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscoveryConfigModel config) protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscoveryConfigModel config)

@ -16,7 +16,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:window-maximize", Icon = "mdi:window-maximize",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });

@ -24,7 +24,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:chart-areaspline", Icon = "mdi:chart-areaspline",
Unit_of_measurement = "%", Unit_of_measurement = "%",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"

@ -16,7 +16,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:speedometer", Icon = "mdi:speedometer",
Unit_of_measurement = "MHz", Unit_of_measurement = "MHz",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"

@ -25,7 +25,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:volume-medium", Icon = "mdi:volume-medium",
Unit_of_measurement = "%", Unit_of_measurement = "%",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"

@ -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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });
} }

@ -37,7 +37,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Unit_of_measurement = "%", Unit_of_measurement = "%",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });

@ -37,7 +37,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Device_class = "temperature", Device_class = "temperature",
Unit_of_measurement = "°C", Unit_of_measurement = "°C",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"

@ -16,7 +16,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:clock-time-three-outline", Icon = "mdi:clock-time-three-outline",
Device_class = "timestamp" Device_class = "timestamp"
}); });

@ -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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:clock-time-three-outline", Icon = "mdi:clock-time-three-outline",
Device_class = "timestamp" Device_class = "timestamp"
}); });

@ -44,7 +44,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:memory", Icon = "mdi:memory",
Unit_of_measurement = "%", Unit_of_measurement = "%",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"

@ -29,7 +29,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability"
}); });
} }

@ -24,7 +24,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability"
}); });
} }

@ -44,7 +44,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.ObjectId}/state", State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:lock", Icon = "mdi:lock",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });

@ -16,7 +16,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}/{Publisher.NamePrefix}{this.ObjectId}/state",
Icon = "mdi:laptop", Icon = "mdi:laptop",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });

@ -27,7 +27,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}/{Publisher.NamePrefix}{this.ObjectId}/state",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
}); });
} }

@ -32,7 +32,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}/{Publisher.NamePrefix}{this.ObjectId}/state",
Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability" Availability_topic = $"homeassistant/sensor/{Publisher.DeviceConfigModel.Name}/availability"
}); });
} }

@ -70,7 +70,6 @@ namespace hass_workstation_service
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) Host.CreateDefaultBuilder(args)
.ConfigureLogging((hostContext, loggingBuilder) => .ConfigureLogging((hostContext, loggingBuilder) =>
loggingBuilder.AddSerilog(dispose: true)) loggingBuilder.AddSerilog(dispose: true))
.ConfigureServices((hostContext, services) => .ConfigureServices((hostContext, services) =>

Loading…
Cancel
Save