From d7eef51adf1a33e62ee5a5aadd23920dd42cd5cc Mon Sep 17 00:00:00 2001 From: Ryan Wagoner Date: Sun, 5 May 2024 15:33:24 -0400 Subject: [PATCH] 1.1.17 - Add MQTT volume scaling for Home Assistant media player --- OmniLinkBridge/Global.cs | 1 + OmniLinkBridge/MQTT/HomeAssistant/Number.cs | 5 ++++- OmniLinkBridge/MQTT/MappingExtensions.cs | 18 ++++++++++++++++-- OmniLinkBridge/MQTT/MessageProcessor.cs | 12 ++++++++++-- OmniLinkBridge/OmniLinkBridge.ini | 7 +++++-- OmniLinkBridge/Properties/AssemblyInfo.cs | 4 ++-- OmniLinkBridge/Settings.cs | 1 + OmniLinkBridgeTest/MQTTTest.cs | 6 ++++++ README.md | 1 + 9 files changed, 46 insertions(+), 9 deletions(-) diff --git a/OmniLinkBridge/Global.cs b/OmniLinkBridge/Global.cs index 8aad965..0c4dd5f 100644 --- a/OmniLinkBridge/Global.cs +++ b/OmniLinkBridge/Global.cs @@ -62,6 +62,7 @@ namespace OmniLinkBridge public static ConcurrentDictionary mqtt_discovery_override_unit; public static Type mqtt_discovery_button_type; public static bool mqtt_audio_local_mute; + public static bool mqtt_audio_volume_media_player; // Notifications public static bool notify_area; diff --git a/OmniLinkBridge/MQTT/HomeAssistant/Number.cs b/OmniLinkBridge/MQTT/HomeAssistant/Number.cs index 79fb256..a964106 100644 --- a/OmniLinkBridge/MQTT/HomeAssistant/Number.cs +++ b/OmniLinkBridge/MQTT/HomeAssistant/Number.cs @@ -12,9 +12,12 @@ namespace OmniLinkBridge.MQTT.HomeAssistant public string command_topic { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] - public int? min { get; set; } + public int? min { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public int? max { get; set; } + + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public double? step { get; set; } } } diff --git a/OmniLinkBridge/MQTT/MappingExtensions.cs b/OmniLinkBridge/MQTT/MappingExtensions.cs index a9edd11..f092bf2 100644 --- a/OmniLinkBridge/MQTT/MappingExtensions.cs +++ b/OmniLinkBridge/MQTT/MappingExtensions.cs @@ -726,13 +726,27 @@ namespace OmniLinkBridge.MQTT icon = "mdi:volume-low", state_topic = audioZone.ToTopic(Topic.volume_state), command_topic = audioZone.ToTopic(Topic.volume_command), + min = 0, + max = 100, + step = 1, }; + + if(Global.mqtt_audio_volume_media_player) + { + ret.min = 0; + ret.max = 1; + ret.step = 0.01; + } + return ret; } - public static int ToVolumeState(this clsAudioZone audioZone) + public static double ToVolumeState(this clsAudioZone audioZone) { - return audioZone.Volume; + if (Global.mqtt_audio_volume_media_player) + return audioZone.Volume * 0.01; + else + return audioZone.Volume; } } } diff --git a/OmniLinkBridge/MQTT/MessageProcessor.cs b/OmniLinkBridge/MQTT/MessageProcessor.cs index 28c1ae8..7695090 100644 --- a/OmniLinkBridge/MQTT/MessageProcessor.cs +++ b/OmniLinkBridge/MQTT/MessageProcessor.cs @@ -397,9 +397,17 @@ namespace OmniLinkBridge.MQTT OmniLink.SendCommand(enuUnitCommand.AudioSource, (byte)source, (ushort)audioZone.Number); } - else if (command == Topic.volume_command && int.TryParse(payload, out int volume)) + else if (command == Topic.volume_command && double.TryParse(payload, out double volume)) { - log.Debug("SetAudioVolume: {id} to {value}", audioZone.Number, payload); + if (Global.mqtt_audio_volume_media_player) + volume *= 100; + + if (volume > 100) + volume = 100; + else if (volume < 0) + volume = 0; + + log.Debug("SetAudioVolume: {id} to {value}", audioZone.Number, volume); OmniLink.SendCommand(enuUnitCommand.AudioVolume, (byte)volume, (ushort)audioZone.Number); } diff --git a/OmniLinkBridge/OmniLinkBridge.ini b/OmniLinkBridge/OmniLinkBridge.ini index f19b8e2..63d893a 100644 --- a/OmniLinkBridge/OmniLinkBridge.ini +++ b/OmniLinkBridge/OmniLinkBridge.ini @@ -70,10 +70,13 @@ mqtt_discovery_area_code_required = #mqtt_discovery_override_unit = id=1;type=switch #mqtt_discovery_override_unit = id=395;type=number # Publish buttons as this Home Assistant device type -# must be button or switch (previous behavior) -#mqtt_discovery_button_type = button +# must be button (recommended) or switch (default, previous behavior) +mqtt_discovery_button_type = switch # Handle mute locally by setting volume to 0 and restoring to previous value mqtt_audio_local_mute = no +# Change audio volume scaling for Home Assistant media player +# yes 0.00-1.00, no 0-100 (default, previous behavior) +mqtt_audio_volume_media_player = no # Notifications (yes/no) # Always sent for area alarms and critical system events diff --git a/OmniLinkBridge/Properties/AssemblyInfo.cs b/OmniLinkBridge/Properties/AssemblyInfo.cs index 0209f91..7348a72 100644 --- a/OmniLinkBridge/Properties/AssemblyInfo.cs +++ b/OmniLinkBridge/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.16.0")] -[assembly: AssemblyFileVersion("1.1.16.0")] +[assembly: AssemblyVersion("1.1.17.0")] +[assembly: AssemblyFileVersion("1.1.17.0")] diff --git a/OmniLinkBridge/Settings.cs b/OmniLinkBridge/Settings.cs index 76584cb..87197b7 100644 --- a/OmniLinkBridge/Settings.cs +++ b/OmniLinkBridge/Settings.cs @@ -93,6 +93,7 @@ namespace OmniLinkBridge Global.mqtt_discovery_override_unit = settings.LoadOverrideUnit("mqtt_discovery_override_unit"); Global.mqtt_discovery_button_type = settings.ValidateType("mqtt_discovery_button_type", typeof(Switch), typeof(Button)); Global.mqtt_audio_local_mute = settings.ValidateBool("mqtt_audio_local_mute"); + Global.mqtt_audio_volume_media_player = settings.ValidateBool("mqtt_audio_volume_media_player"); } // Notifications diff --git a/OmniLinkBridgeTest/MQTTTest.cs b/OmniLinkBridgeTest/MQTTTest.cs index caadbd4..d92e4c2 100644 --- a/OmniLinkBridgeTest/MQTTTest.cs +++ b/OmniLinkBridgeTest/MQTTTest.cs @@ -433,6 +433,12 @@ namespace OmniLinkBridgeTest check(1, "75", enuUnitCommand.AudioVolume, 75); check(2, "0", enuUnitCommand.AudioVolume, 0); + + Global.mqtt_audio_volume_media_player = true; + + check(2, "1", enuUnitCommand.AudioVolume, 100); + check(2, "0.75", enuUnitCommand.AudioVolume, 75); + check(2, "0", enuUnitCommand.AudioVolume, 0); } } } diff --git a/README.md b/README.md index caa1a93..e070268 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,7 @@ note Refer to omnilink/sourceXX/name SUB omnilink/audioXX/volume_state PUB omnilink/audioXX/volume_command int Level from 0 to 100 percent +double Level from 0.00 to 1.00 (mqtt_audio_volume_media_player = yes) ``` ## Web API