Add master volume sensor.

pull/123/head
TerminalMan 3 years ago
parent da64302a6c
commit 79f9431a45

@ -20,6 +20,10 @@ The current clock speed sensor returns the base system clock as configured in th
This sensor returns the current volume of playing audio. **It does not return the master volume.** If you have no sound playing the value will be 0. This sensor returns the current volume of playing audio. **It does not return the master volume.** If you have no sound playing the value will be 0.
### MasterVolumeSensor
This sensor returns the master volume for the currently selected default audio device.
### DummySensor ### DummySensor
This sensor produces a random output every second, and is intended to test latency and connectivity. This sensor produces a random output every second, and is intended to test latency and connectivity.

@ -185,6 +185,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication
AvailableSensors.CurrentVolumeSensor => new CurrentVolumeSensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.CurrentVolumeSensor => new CurrentVolumeSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.GPUTemperatureSensor => new GpuTemperatureSensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.GPUTemperatureSensor => new GpuTemperatureSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.GPULoadSensor => new GpuLoadSensor(_publisher, (int)model.UpdateInterval, model.Name), AvailableSensors.GPULoadSensor => new GpuLoadSensor(_publisher, (int)model.UpdateInterval, model.Name),
AvailableSensors.MasterVolumeSensor => new MasterVolumeSensor(_publisher, (int)model.UpdateInterval, model.Name),
_ => null _ => null
}; };
} }

@ -100,6 +100,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models
LastBootSensor, LastBootSensor,
SessionStateSensor, SessionStateSensor,
CurrentVolumeSensor, CurrentVolumeSensor,
MasterVolumeSensor,
GPUTemperatureSensor, GPUTemperatureSensor,
GPULoadSensor GPULoadSensor
} }

@ -138,6 +138,9 @@ namespace hass_workstation_service.Data
case "CurrentVolumeSensor": case "CurrentVolumeSensor":
sensor = new CurrentVolumeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); sensor = new CurrentVolumeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break; break;
case "MasterVolumeSensor":
sensor = new MasterVolumeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break;
case "GpuTemperatureSensor": case "GpuTemperatureSensor":
sensor = new GpuTemperatureSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); sensor = new GpuTemperatureSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break; break;

@ -0,0 +1,41 @@
using CoreAudio;
using hass_workstation_service.Communication;
using System;
using System.Globalization;
namespace hass_workstation_service.Domain.Sensors
{
public class MasterVolumeSensor : AbstractSensor
{
private MMDeviceEnumerator deviceEnumerator;
public MasterVolumeSensor(MqttPublisher publisher, int? updateInterval = null, string name = "MasterVolume", Guid id = default(Guid)) : base(publisher, name ?? "CurrentVolume", updateInterval ?? 10, id)
{
this.deviceEnumerator = new MMDeviceEnumerator();
}
public override SensorDiscoveryConfigModel GetAutoDiscoveryConfig()
{
return this._autoDiscoveryConfigModel ?? SetAutoDiscoveryConfigModel(new SensorDiscoveryConfigModel()
{
Name = this.Name,
Unique_id = this.Id.ToString(),
Device = this.Publisher.DeviceConfigModel,
State_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/{this.ObjectId}/state",
Icon = "mdi:volume-medium",
Unit_of_measurement = "%",
Availability_topic = $"homeassistant/{this.Domain}/{Publisher.DeviceConfigModel.Name}/availability"
});
}
public override string GetState()
{
var defaultAudioDevice = deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
// check if the volume is muted
if (defaultAudioDevice.AudioEndpointVolume.Mute) return "0";
return Math.Round(defaultAudioDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100, 0) // round volume and convert to percent
.ToString(CultureInfo.InvariantCulture); // convert to string
}
}
}
Loading…
Cancel
Save