Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix: Home Assistant 2023.8 MQTT #20

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public static class CommandExtensions
public static string GetCommandName(this CommandType commandType)
{
var (_, name) = commandType.GetLocalizedDescriptionAndKey();
var commandName = name.ToLower();

return $"{SharedHelperFunctions.GetSafeConfiguredDeviceName()}_{commandName}";
return name.ToLower();
}

//TODO: remove after tests

/// <summary>
/// Returns the name of the commandtype, based on the provided devicename
/// </summary>
/// <param name="commandType"></param>
/// <param name="deviceName"></param>
/// <returns></returns>
public static string GetCommandName(this CommandType commandType, string deviceName)
{
var (_, name) = commandType.GetLocalizedDescriptionAndKey();
var commandName = name.ToLower();
return $"{SharedHelperFunctions.GetSafeValue(deviceName)}_{commandName}";
}
/* public static string GetCommandName(this CommandType commandType, string deviceName)
{
var (_, name) = commandType.GetLocalizedDescriptionAndKey();
var commandName = name.ToLower();

return $"{SharedHelperFunctions.GetSafeValue(deviceName)}_{commandName}";
}*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public static class SensorExtensions
public static string GetSensorName(this SensorType sensorType)
{
var (_, name) = sensorType.GetLocalizedDescriptionAndKey();
var sensorName = name.ToLower();

return $"{SharedHelperFunctions.GetSafeConfiguredDeviceName()}_{sensorName}";
return name.ToLower();
}

//TODO: remove after tests

/// <summary>
/// Returns the name of the sensortype, based on the provided devicename
/// </summary>
/// <param name="sensorType"></param>
/// <param name="deviceName"></param>
/// <returns></returns>
public static string GetSensorName(this SensorType sensorType, string deviceName)
/* public static string GetSensorName(this SensorType sensorType, string deviceName)
{
var (_, name) = sensorType.GetLocalizedDescriptionAndKey();
var sensorName = name.ToLower();

return $"{SharedHelperFunctions.GetSafeValue(deviceName)}_{sensorName}";
}
}*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public sealed override void UpdateSensorValues()
{
// default device name
var defaultDeviceId = $"{parentSensorSafeName}_default_device";
var defaultDeviceSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Default Device", defaultDeviceId, string.Empty, "mdi:speaker", string.Empty, Name);
var defaultDeviceSensor = new DataTypeStringSensor(_updateInterval, "Default Device", defaultDeviceId, string.Empty, "mdi:speaker", string.Empty, Name);
defaultDeviceSensor.SetState(audioDevice.DeviceFriendlyName);

if (!Sensors.ContainsKey(defaultDeviceId)) Sensors.Add(defaultDeviceId, defaultDeviceSensor);
else Sensors[defaultDeviceId] = defaultDeviceSensor;

// default device state
var defaultDeviceStateId = $"{parentSensorSafeName}_default_device_state";
var defaultDeviceStateSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Default Device State", defaultDeviceStateId, string.Empty, "mdi:speaker", string.Empty, Name);
var defaultDeviceStateSensor = new DataTypeStringSensor(_updateInterval, "Default Device State", defaultDeviceStateId, string.Empty, "mdi:speaker", string.Empty, Name);
defaultDeviceStateSensor.SetState(GetReadableState(audioDevice.State));

if (!Sensors.ContainsKey(defaultDeviceStateId)) Sensors.Add(defaultDeviceStateId, defaultDeviceStateSensor);
Expand All @@ -63,7 +63,7 @@ public sealed override void UpdateSensorValues()
// default device volume
var masterVolume = Convert.ToInt32(Math.Round(audioDevice.AudioEndpointVolume?.MasterVolumeLevelScalar * 100 ?? 0, 0));
var defaultDeviceVolumeId = $"{parentSensorSafeName}_default_device_volume";
var defaultDeviceVolumeSensor = new DataTypeIntSensor(_updateInterval, $"{Name} Default Device Volume", defaultDeviceVolumeId, string.Empty, "mdi:speaker", string.Empty, Name);
var defaultDeviceVolumeSensor = new DataTypeIntSensor(_updateInterval, "Default Device Volume", defaultDeviceVolumeId, string.Empty, "mdi:speaker", string.Empty, Name);
defaultDeviceVolumeSensor.SetState(masterVolume);

if (!Sensors.ContainsKey(defaultDeviceVolumeId)) Sensors.Add(defaultDeviceVolumeId, defaultDeviceVolumeSensor);
Expand All @@ -72,7 +72,7 @@ public sealed override void UpdateSensorValues()
// default device muted
var defaultDeviceIsMuted = audioDevice.AudioEndpointVolume?.Mute ?? false;
var defaultDeviceIsMutedId = $"{parentSensorSafeName}_default_device_muted";
var defaultDeviceIsMutedSensor = new DataTypeBoolSensor(_updateInterval, $"{Name} Default Device Muted", defaultDeviceIsMutedId, string.Empty, "mdi:speaker", Name);
var defaultDeviceIsMutedSensor = new DataTypeBoolSensor(_updateInterval, "Default Device Muted", defaultDeviceIsMutedId, string.Empty, "mdi:speaker", Name);
defaultDeviceIsMutedSensor.SetState(defaultDeviceIsMuted);

if (!Sensors.ContainsKey(defaultDeviceIsMutedId)) Sensors.Add(defaultDeviceIsMutedId, defaultDeviceIsMutedSensor);
Expand All @@ -83,7 +83,7 @@ public sealed override void UpdateSensorValues()

// peak value sensor
var peakVolumeId = $"{parentSensorSafeName}_peak_volume";
var peakVolumeSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Peak Volume", peakVolumeId, string.Empty, "mdi:volume-high", string.Empty, Name);
var peakVolumeSensor = new DataTypeStringSensor(_updateInterval, "Peak Volume", peakVolumeId, string.Empty, "mdi:volume-high", string.Empty, Name);
peakVolumeSensor.SetState(peakVolume.ToString(CultureInfo.CurrentCulture));

if (!Sensors.ContainsKey(peakVolumeId)) Sensors.Add(peakVolumeId, peakVolumeSensor);
Expand All @@ -92,7 +92,7 @@ public sealed override void UpdateSensorValues()
// sessions sensor
var sessions = JsonConvert.SerializeObject(new AudioSessionInfoCollection(sessionInfos), Formatting.Indented);
var sessionsId = $"{parentSensorSafeName}_audio_sessions";
var sessionsSensor = new DataTypeIntSensor(_updateInterval, $"{Name} Audio Sessions", sessionsId, string.Empty, "mdi:music-box-multiple-outline", string.Empty, Name, true);
var sessionsSensor = new DataTypeIntSensor(_updateInterval, "Audio Sessions", sessionsId, string.Empty, "mdi:music-box-multiple-outline", string.Empty, Name, true);

sessionsSensor.SetState(sessionInfos.Count);
sessionsSensor.SetAttributes(sessions);
Expand All @@ -106,15 +106,15 @@ public sealed override void UpdateSensorValues()
{
// default input device name
var defaultInputDeviceId = $"{parentSensorSafeName}_default_input_device";
var defaultInputDeviceSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Default Input Device", defaultInputDeviceId, string.Empty, "mdi:microphone", string.Empty, Name);
var defaultInputDeviceSensor = new DataTypeStringSensor(_updateInterval, "Default Input Device", defaultInputDeviceId, string.Empty, "mdi:microphone", string.Empty, Name);
defaultInputDeviceSensor.SetState(inputDevice.DeviceFriendlyName);

if (!Sensors.ContainsKey(defaultInputDeviceId)) Sensors.Add(defaultInputDeviceId, defaultInputDeviceSensor);
else Sensors[defaultInputDeviceId] = defaultInputDeviceSensor;

// default input device state
var defaultInputDeviceStateId = $"{parentSensorSafeName}_default_input_device_state";
var defaultInputDeviceStateSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Default Input Device State", defaultInputDeviceStateId, string.Empty, "mdi:microphone", string.Empty, Name);
var defaultInputDeviceStateSensor = new DataTypeStringSensor(_updateInterval, "Default Input Device State", defaultInputDeviceStateId, string.Empty, "mdi:microphone", string.Empty, Name);
defaultInputDeviceStateSensor.SetState(GetReadableState(inputDevice.State));

if (!Sensors.ContainsKey(defaultInputDeviceStateId)) Sensors.Add(defaultInputDeviceStateId, defaultInputDeviceStateSensor);
Expand All @@ -123,7 +123,7 @@ public sealed override void UpdateSensorValues()
// default input device muted
var defaultInputDeviceIsMuted = inputDevice.AudioEndpointVolume?.Mute ?? false;
var defaultInputDeviceIsMutedId = $"{parentSensorSafeName}_default_input_device_muted";
var defaultInputDeviceIsMutedSensor = new DataTypeBoolSensor(_updateInterval, $"{Name} Default Input Device Muted", defaultInputDeviceIsMutedId, string.Empty, "mdi:microphone", Name);
var defaultInputDeviceIsMutedSensor = new DataTypeBoolSensor(_updateInterval, "Default Input Device Muted", defaultInputDeviceIsMutedId, string.Empty, "mdi:microphone", Name);
defaultInputDeviceIsMutedSensor.SetState(defaultInputDeviceIsMuted);

if (!Sensors.ContainsKey(defaultInputDeviceIsMutedId)) Sensors.Add(defaultInputDeviceIsMutedId, defaultInputDeviceIsMutedSensor);
Expand All @@ -132,7 +132,7 @@ public sealed override void UpdateSensorValues()
// default input device volume
var inputVolume = (int)GetDefaultInputDevicePeakVolume(inputDevice);
var defaultInputDeviceVolumeId = $"{parentSensorSafeName}_default_input_device_volume";
var defaultInputDeviceVolumeSensor = new DataTypeIntSensor(_updateInterval, $"{Name} Default Input Device Volume", defaultInputDeviceVolumeId, string.Empty, "mdi:microphone", string.Empty, Name);
var defaultInputDeviceVolumeSensor = new DataTypeIntSensor(_updateInterval, "Default Input Device Volume", defaultInputDeviceVolumeId, string.Empty, "mdi:microphone", string.Empty, Name);
defaultInputDeviceVolumeSensor.SetState(inputVolume);

if (!Sensors.ContainsKey(defaultInputDeviceVolumeId)) Sensors.Add(defaultInputDeviceVolumeId, defaultInputDeviceVolumeSensor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,55 @@ public BatterySensors(int? updateInterval = null, string name = DefaultName, str
UpdateSensorValues();
}

private void AddUpdateSensor(string sensorId, AbstractSingleValueSensor sensor)
{
if (!Sensors.ContainsKey(sensorId))
Sensors.Add(sensorId, sensor);
else
Sensors[sensorId] = sensor;
}

public sealed override void UpdateSensorValues()
{
// lowercase and safe name of the multivalue sensor
var parentSensorSafeName = SharedHelperFunctions.GetSafeValue(Name);

// fetch the latest battery state
var powerStatus = SystemInformation.PowerStatus;

// prepare the data
var lifetimeMinutes = powerStatus.BatteryFullLifetime;
if (lifetimeMinutes != -1) lifetimeMinutes = Convert.ToInt32(Math.Round(TimeSpan.FromSeconds(lifetimeMinutes).TotalMinutes));

var remainingMinutes = powerStatus.BatteryLifeRemaining;
if (remainingMinutes != -1) remainingMinutes = Convert.ToInt32(Math.Round(TimeSpan.FromSeconds(remainingMinutes).TotalMinutes));

// charge status sensor
var chargeStatus = powerStatus.BatteryChargeStatus.ToString();

var chargeStatusId = $"{parentSensorSafeName}_charge_status";
var chargeStatusSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Charge Status", chargeStatusId, string.Empty, "mdi:battery-charging", string.Empty, Name);
var chargeStatusSensor = new DataTypeStringSensor(_updateInterval, "Charge Status", chargeStatusId, string.Empty, "mdi:battery-charging", string.Empty, Name);
chargeStatusSensor.SetState(chargeStatus);
AddUpdateSensor(chargeStatusId, chargeStatusSensor);

if (!Sensors.ContainsKey(chargeStatusId)) Sensors.Add(chargeStatusId, chargeStatusSensor);
else Sensors[chargeStatusId] = chargeStatusSensor;

// full charge lifetime sensor
var fullChargeLifetimeMinutes = lifetimeMinutes;
var fullChargeLifetimeMinutes = powerStatus.BatteryFullLifetime;
if (fullChargeLifetimeMinutes != -1)
fullChargeLifetimeMinutes = Convert.ToInt32(Math.Round(TimeSpan.FromSeconds(fullChargeLifetimeMinutes).TotalMinutes));

var fullChargeLifetimeId = $"{parentSensorSafeName}_full_charge_lifetime";
var fullChargeLifetimeSensor = new DataTypeIntSensor(_updateInterval, $"{Name} Full Charge Lifetime", fullChargeLifetimeId, string.Empty, "mdi:battery-high", string.Empty, Name);
var fullChargeLifetimeSensor = new DataTypeIntSensor(_updateInterval, "Full Charge Lifetime", fullChargeLifetimeId, string.Empty, "mdi:battery-high", string.Empty, Name);
fullChargeLifetimeSensor.SetState(fullChargeLifetimeMinutes);
AddUpdateSensor(fullChargeLifetimeId, fullChargeLifetimeSensor);

if (!Sensors.ContainsKey(fullChargeLifetimeId)) Sensors.Add(fullChargeLifetimeId, fullChargeLifetimeSensor);
else Sensors[fullChargeLifetimeId] = fullChargeLifetimeSensor;

// charge remaining percentage sensor
var chargeRemainingPercentage = Convert.ToInt32(powerStatus.BatteryLifePercent * 100);

var chargeRemainingPercentageId = $"{parentSensorSafeName}_charge_remaining_percentage";
var chargeRemainingPercentageSensor = new DataTypeIntSensor(_updateInterval, $"{Name} Charge Remaining Percentage", chargeRemainingPercentageId, string.Empty, "mdi:battery-high", "%", Name);
var chargeRemainingPercentageSensor = new DataTypeIntSensor(_updateInterval, "Charge Remaining Percentage", chargeRemainingPercentageId, string.Empty, "mdi:battery-high", "%", Name);
chargeRemainingPercentageSensor.SetState(chargeRemainingPercentage);
AddUpdateSensor(chargeRemainingPercentageId, chargeRemainingPercentageSensor);

if (!Sensors.ContainsKey(chargeRemainingPercentageId)) Sensors.Add(chargeRemainingPercentageId, chargeRemainingPercentageSensor);
else Sensors[chargeRemainingPercentageId] = chargeRemainingPercentageSensor;

// charge remaining minutes sensor
var chargeRemainingMinutes = remainingMinutes;
var chargeRemainingMinutes = powerStatus.BatteryLifeRemaining;
if (chargeRemainingMinutes != -1)
chargeRemainingMinutes = Convert.ToInt32(Math.Round(TimeSpan.FromSeconds(chargeRemainingMinutes).TotalMinutes));

var chargeRemainingMinutesId = $"{parentSensorSafeName}_charge_remaining";
var chargeRemainingMinutesSensor = new DataTypeIntSensor(_updateInterval, $"{Name} Charge Remaining", chargeRemainingMinutesId, string.Empty, "mdi:battery-high", string.Empty, Name);
var chargeRemainingMinutesSensor = new DataTypeIntSensor(_updateInterval, "Charge Remaining", chargeRemainingMinutesId, string.Empty, "mdi:battery-high", string.Empty, Name);
chargeRemainingMinutesSensor.SetState(chargeRemainingMinutes);
AddUpdateSensor(chargeRemainingMinutesId, chargeRemainingMinutesSensor);

if (!Sensors.ContainsKey(chargeRemainingMinutesId)) Sensors.Add(chargeRemainingMinutesId, chargeRemainingMinutesSensor);
else Sensors[chargeRemainingMinutesId] = chargeRemainingMinutesSensor;

// powerline status sensor
var powerlineStatus = powerStatus.PowerLineStatus.ToString();

var powerlineStatusId = $"{parentSensorSafeName}_powerline_status";
var powerlineStatusSensor = new DataTypeStringSensor(_updateInterval, $"{Name} Powerline Status", powerlineStatusId, string.Empty, "mdi:power-plug", string.Empty, Name);
var powerlineStatusSensor = new DataTypeStringSensor(_updateInterval, "Powerline Status", powerlineStatusId, string.Empty, "mdi:power-plug", string.Empty, Name);
powerlineStatusSensor.SetState(powerlineStatus);

if (!Sensors.ContainsKey(powerlineStatusId)) Sensors.Add(powerlineStatusId, powerlineStatusSensor);
else Sensors[powerlineStatusId] = powerlineStatusSensor;
AddUpdateSensor(powerlineStatusId, powerlineStatusSensor);
}

public override DiscoveryConfigModel GetAutoDiscoveryConfig() => null;
Expand Down
Loading