working device discovery

pull/9/head
sleevezipper 4 years ago
parent 7af05b88b9
commit 65fb3c7d20

@ -81,7 +81,7 @@ namespace hass_desktop_service.Communication
PropertyNameCaseInsensitive = true PropertyNameCaseInsensitive = true
}; };
var message = new MqttApplicationMessageBuilder() var message = new MqttApplicationMessageBuilder()
.WithTopic($"homeassistant/sensor/{config.Device.Identifiers}/config") .WithTopic($"homeassistant/sensor/{config.Name}/config")
.WithPayload(clearPreviousConfig ? "" : JsonSerializer.Serialize(config, options)) .WithPayload(clearPreviousConfig ? "" : JsonSerializer.Serialize(config, options))
.WithRetainFlag() .WithRetainFlag()
.Build(); .Build();

@ -9,6 +9,7 @@ namespace hass_desktop_service.Domain.Sensors
{ {
public Guid Id { get; protected set; } public Guid Id { get; protected set; }
public string Name { get; protected set; } public string Name { get; protected set; }
public string PreviousPublishedState { get; protected set; }
public MqttPublisher Publisher { get; protected set; } public MqttPublisher Publisher { get; protected set; }
protected AutoDiscoveryConfigModel _autoDiscoveryConfigModel; protected AutoDiscoveryConfigModel _autoDiscoveryConfigModel;
protected AutoDiscoveryConfigModel SetAutoDiscoveryConfigModel(AutoDiscoveryConfigModel config) protected AutoDiscoveryConfigModel SetAutoDiscoveryConfigModel(AutoDiscoveryConfigModel config)
@ -22,13 +23,20 @@ namespace hass_desktop_service.Domain.Sensors
public async Task PublishStateAsync() public async Task PublishStateAsync()
{ {
string state = this.GetState();
if (this.PreviousPublishedState == state)
{
// don't publish the state if it hasn't changed
return;
}
var message = new MqttApplicationMessageBuilder() var message = new MqttApplicationMessageBuilder()
.WithTopic(this.GetAutoDiscoveryConfig().State_topic) .WithTopic(this.GetAutoDiscoveryConfig().State_topic)
.WithPayload(this.GetState()) .WithPayload(state)
.WithExactlyOnceQoS() .WithExactlyOnceQoS()
.WithRetainFlag() .WithRetainFlag()
.Build(); .Build();
await Publisher.Publish(message); await Publisher.Publish(message);
this.PreviousPublishedState = state;
} }
public async Task PublishAutoDiscoveryConfigAsync() public async Task PublishAutoDiscoveryConfigAsync()
{ {

@ -85,6 +85,15 @@ namespace hass_desktop_service.Domain.Sensors
/// without those distractions. /// without those distractions.
/// Quiet time also occurs for each user after an operating system upgrade or clean installation. /// Quiet time also occurs for each user after an operating system upgrade or clean installation.
/// </summary> /// </summary>
QuietTime = 6 QuietTime = 6,
/// <summary>
/// Introduced in Windows 7. The current user is in "quiet time", which is the first hour after
/// a new user logs into his or her account for the first time. During this time, most notifications
/// should not be sent or shown. This lets a user become accustomed to a new computer system
/// without those distractions.
/// Quiet time also occurs for each user after an operating system upgrade or clean installation.
/// </summary>
RunningWindowsStoreApp = 7
}; };
} }

@ -37,10 +37,11 @@ namespace hass_desktop_service
.Build(); .Build();
var deviceConfig = new DeviceConfigModel var deviceConfig = new DeviceConfigModel
{ {
Name = "hass-workstation-service3", Name = Environment.MachineName,
//TODO: make this more dynamic Identifiers = "hass-workstation-service",
Identifiers = "hass-workstation-service_unique4", Manufacturer = Environment.UserName,
Sw_version = "0.0.4" Model = Environment.OSVersion.ToString(),
Sw_version = "0.0.5"
}; };
services.AddSingleton(configuration); services.AddSingleton(configuration);
services.AddSingleton(deviceConfig); services.AddSingleton(deviceConfig);

@ -50,7 +50,10 @@ namespace hass_desktop_service
// announce autodiscovery every 30 seconds // announce autodiscovery every 30 seconds
if (_mqttPublisher.LastConfigAnnounce < DateTime.UtcNow.AddSeconds(-30)) if (_mqttPublisher.LastConfigAnnounce < DateTime.UtcNow.AddSeconds(-30))
{ {
// TODO: make every sensor publish its auto discovery config foreach (AbstractSensor sensor in _configuredSensorsService.ConfiguredSensors)
{
await sensor.PublishAutoDiscoveryConfigAsync();
}
} }
await Task.Delay(1000, stoppingToken); await Task.Delay(1000, stoppingToken);
} }

Loading…
Cancel
Save