diff --git a/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuLoadSensor.cs b/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuLoadSensor.cs index 8e5f174e..db186475 100644 --- a/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuLoadSensor.cs +++ b/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuLoadSensor.cs @@ -25,7 +25,7 @@ public GpuLoadSensor(int? updateInterval = null, string name = DefaultName, stri IsNetworkEnabled = false, IsStorageEnabled = false, }; - + computer.Open(); _gpu = computer.Hardware.FirstOrDefault(h => h.HardwareType == HardwareType.GpuAmd || h.HardwareType == HardwareType.GpuNvidia); @@ -53,14 +53,17 @@ public override DiscoveryConfigModel GetAutoDiscoveryConfig() public override string GetState() { - if (_gpu == null) return "NotSupported"; + if (_gpu == null) + return null; _gpu.Update(); + var sensor = _gpu.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Load); - if (sensor?.Value == null) return "NotSupported"; + if (sensor?.Value == null) + return null; - return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : "Unknown"; + return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : null; } public override string GetAttributes() => string.Empty; diff --git a/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuTemperatureSensor.cs b/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuTemperatureSensor.cs index 1e79b168..e01ff699 100644 --- a/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuTemperatureSensor.cs +++ b/src/HASS.Agent.Staging/HASS.Agent.Shared/HomeAssistant/Sensors/GeneralSensors/SingleValue/GpuTemperatureSensor.cs @@ -54,14 +54,17 @@ public override DiscoveryConfigModel GetAutoDiscoveryConfig() public override string GetState() { - if (_gpu == null) return "NotSupported"; + if (_gpu == null) + return null; _gpu.Update(); + var sensor = _gpu.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Temperature); - if (sensor?.Value == null) return "NotSupported"; + if (sensor?.Value == null) + return null; - return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : "Unknown"; + return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : null; } public override string GetAttributes() => string.Empty; diff --git a/src/HASS.Agent.Staging/HASS.Agent.Shared/Models/HomeAssistant/AbstractSingleValueSensor.cs b/src/HASS.Agent.Staging/HASS.Agent.Shared/Models/HomeAssistant/AbstractSingleValueSensor.cs index 1b3a77e2..3a67aaae 100644 --- a/src/HASS.Agent.Staging/HASS.Agent.Shared/Models/HomeAssistant/AbstractSingleValueSensor.cs +++ b/src/HASS.Agent.Staging/HASS.Agent.Shared/Models/HomeAssistant/AbstractSingleValueSensor.cs @@ -35,8 +35,11 @@ protected SensorDiscoveryConfigModel SetAutoDiscoveryConfigModel(SensorDiscovery public override void ClearAutoDiscoveryConfig() => AutoDiscoveryConfigModel = null; - public abstract string GetState(); - public abstract string GetAttributes(); + // nullable in preparation for possible future "nullable enablement" + public abstract string? GetState(); + + // nullable in preparation for possible future "nullable enablement" + public abstract string? GetAttributes(); public void ResetChecks() { @@ -60,6 +63,9 @@ public async Task PublishStateAsync(bool respectChecks = true) // get the current state/attributes var state = GetState(); + if (state == null) + return; + var attributes = GetAttributes(); // are we asked to check state changes? @@ -82,13 +88,10 @@ public async Task PublishStateAsync(bool respectChecks = true) // send it var published = await Variables.MqttManager.PublishAsync(message); if (!published) - { - // failed, don't store the state - return; - } + return; // failed, don't store the state // optionally prepare and send attributes - if (UseAttributes) + if (UseAttributes && attributes != null) { message = new MqttApplicationMessageBuilder() .WithTopic(autoDiscoConfig.Json_attributes_topic) @@ -98,18 +101,18 @@ public async Task PublishStateAsync(bool respectChecks = true) published = await Variables.MqttManager.PublishAsync(message); if (!published) - { - // failed, don't store the state - return; - } + return; // failed, don't store the state } // only store the values if the checks are respected - // otherwise, we might stay in 'unknown' state untill the value changes - if (!respectChecks) return; + // otherwise, we might stay in 'unknown' state until the value changes + if (!respectChecks) + return; PreviousPublishedState = state; - PreviousPublishedAttributes = attributes; + if (attributes != null) + PreviousPublishedAttributes = attributes; + LastUpdated = DateTime.Now; } catch (Exception ex)