Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

WIP : Feature/gpu cpu power usage #25

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/HASS.Agent.Staging/HASS.Agent.Shared/Enums/SensorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ public enum SensorType

[LocalizedDescription("SensorType_WmiQuerySensor", typeof(Languages))]
[EnumMember(Value = "WmiQuerySensor")]
WmiQuerySensor
WmiQuerySensor,

[LocalizedDescription("SensorType_CpuTotalPowerSensor", typeof(Languages))]
[EnumMember(Value = "CpuTotalPowerSensor")]
CpuTotalPowerSensor,

[LocalizedDescription("SensorType_GpuTotalPowerSensor", typeof(Languages))]
[EnumMember(Value = "GpuTotalPowerSensor")]
GpuTotalPowerSensor,

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Globalization;
using System.Linq;
using HASS.Agent.Shared.Models.HomeAssistant;
using LibreHardwareMonitor.Hardware;

namespace HASS.Agent.Shared.HomeAssistant.Sensors.GeneralSensors.SingleValue
{
/// <summary>
/// Sensor containing the current CPU power drain
/// </summary>
public class CpuTotalPowerSensor : AbstractSingleValueSensor
{
private const string DefaultName = "cputotalpower";
private readonly IHardware _cpu;

public CpuTotalPowerSensor(int? updateInterval = null, string name = DefaultName, string friendlyName = DefaultName, string id = default) : base(name ?? DefaultName, friendlyName ?? null, updateInterval ?? 30, id)
{
var computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = false,
IsMemoryEnabled = false,
IsMotherboardEnabled = false,
IsControllerEnabled = false,
IsNetworkEnabled = false,
IsStorageEnabled = false,
};

computer.Open();
_cpu = computer.Hardware.FirstOrDefault(h => h.HardwareType == HardwareType.Cpu);

computer.Close();
}

public override DiscoveryConfigModel GetAutoDiscoveryConfig()
{
if (Variables.MqttManager == null) return null;

var deviceConfig = Variables.MqttManager.GetDeviceConfigModel();
if (deviceConfig == null) return null;

return AutoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()
{
Name = Name,
Unique_id = Id,
Device = deviceConfig,
State_topic = $"{Variables.MqttManager.MqttDiscoveryPrefix()}/{Domain}/{deviceConfig.Name}/{ObjectId}/state",
Device_class = "power",
Unit_of_measurement = "W",
Availability_topic = $"{Variables.MqttManager.MqttDiscoveryPrefix()}/{Domain}/{deviceConfig.Name}/availability"
});
}

public override string GetState()
{
if (_cpu == null) return "NotSupported";

// _cpu.Update();
var sensor = _cpu.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Power);

if (sensor?.Value == null) return "NotSupported";

return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : "Unknown";
}

public override string GetAttributes() => string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Globalization;
using System.Linq;
using HASS.Agent.Shared.Models.HomeAssistant;
using LibreHardwareMonitor.Hardware;

namespace HASS.Agent.Shared.HomeAssistant.Sensors.GeneralSensors.SingleValue
{
/// <summary>
/// Sensor containing the current GPU power drain
/// </summary>
public class GpuTotalPowerSensor : AbstractSingleValueSensor
{
private const string DefaultName = "gputotalpower";
private readonly IHardware _gpu;

public GpuTotalPowerSensor(int? updateInterval = null, string name = DefaultName, string friendlyName = DefaultName, string id = default) : base(name ?? DefaultName, friendlyName ?? null, updateInterval ?? 30, id)
{
var computer = new Computer
{
IsCpuEnabled = false,
IsGpuEnabled = true,
IsMemoryEnabled = false,
IsMotherboardEnabled = false,
IsControllerEnabled = false,
IsNetworkEnabled = false,
IsStorageEnabled = false,
};

computer.Open();
_gpu = computer.Hardware.FirstOrDefault(h => h.HardwareType == HardwareType.GpuAmd || h.HardwareType == HardwareType.GpuNvidia);

computer.Close();
}

public override DiscoveryConfigModel GetAutoDiscoveryConfig()
{
if (Variables.MqttManager == null) return null;

var deviceConfig = Variables.MqttManager.GetDeviceConfigModel();
if (deviceConfig == null) return null;

return AutoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()
{
Name = Name,
Unique_id = Id,
Device = deviceConfig,
State_topic = $"{Variables.MqttManager.MqttDiscoveryPrefix()}/{Domain}/{deviceConfig.Name}/{ObjectId}/state",
Device_class = "power",
Unit_of_measurement = "W",
Availability_topic = $"{Variables.MqttManager.MqttDiscoveryPrefix()}/{Domain}/{deviceConfig.Name}/availability"
});
}

public override string GetState()
{
if (_gpu == null) return "NotSupported";

_gpu.Update();
var sensor = _gpu.Sensors.FirstOrDefault(s => s.SensorType == SensorType.Power);

if (sensor?.Value == null) return "NotSupported";

return sensor.Value.HasValue ? sensor.Value.Value.ToString("#.##", CultureInfo.InvariantCulture) : "Unknown";
}

public override string GetAttributes() => string.Empty;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/HASS.Agent.Staging/HASS.Agent/Sensors/SensorsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ internal static void LoadSensorInfo()

// =================================

sensorInfoCard = new SensorInfoCard(SensorType.CpuTotalPowerSensor,
Languages.SensorsManager_CpuTotalPowerSensorDescription,
30, false, true, true);

SensorInfoCards.Add(sensorInfoCard.SensorType, sensorInfoCard);

// =================================

sensorInfoCard = new SensorInfoCard(SensorType.CurrentClockSpeedSensor,
Languages.SensorsManager_CurrentClockSpeedSensorDescription,
300, false, true, true);
Expand Down Expand Up @@ -434,6 +442,14 @@ internal static void LoadSensorInfo()

// =================================

sensorInfoCard = new SensorInfoCard(SensorType.GpuTotalPowerSensor,
Languages.SensorsManager_GpuTotalPowerSensorDescription,
30, false, true, true);

SensorInfoCards.Add(sensorInfoCard.SensorType, sensorInfoCard);

// =================================

sensorInfoCard = new SensorInfoCard(SensorType.GpuTemperatureSensor,
Languages.SensorsManager_GpuTemperatureSensorDescription,
30, false, true, true);
Expand Down
6 changes: 6 additions & 0 deletions src/HASS.Agent.Staging/HASS.Agent/Settings/StoredSensors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ internal static AbstractSingleValueSensor ConvertConfiguredToAbstractSingleValue
case SensorType.CpuLoadSensor:
abstractSensor = new CpuLoadSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
case SensorType.CpuTotalPowerSensor:
abstractSensor = new CpuTotalPowerSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
case SensorType.MemoryUsageSensor:
abstractSensor = new MemoryUsageSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
Expand Down Expand Up @@ -150,6 +153,9 @@ internal static AbstractSingleValueSensor ConvertConfiguredToAbstractSingleValue
case SensorType.GpuTemperatureSensor:
abstractSensor = new GpuTemperatureSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
case SensorType.GpuTotalPowerSensor:
abstractSensor = new GpuTotalPowerSensor(sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
case SensorType.WmiQuerySensor:
abstractSensor = new WmiQuerySensor(sensor.Query, sensor.Scope, sensor.ApplyRounding, sensor.Round, sensor.UpdateInterval, sensor.Name, sensor.FriendlyName, sensor.Id.ToString());
break;
Expand Down