]> git.basschouten.com Git - openhab-addons.git/commitdiff
Add UoM support for volume dB channel (#16759)
authorJacob Laursen <jacob-github@vindvejr.dk>
Tue, 14 May 2024 22:01:04 +0000 (00:01 +0200)
committerGitHub <noreply@github.com>
Tue, 14 May 2024 22:01:04 +0000 (00:01 +0200)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.denonmarantz/README.md
bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/DenonMarantzState.java
bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/DenonMarantzConnector.java
bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/xml/adapters/VolumeAdapter.java
bundles/org.openhab.binding.denonmarantz/src/main/resources/OH-INF/thing/thing-types.xml
bundles/org.openhab.binding.denonmarantz/src/main/resources/OH-INF/update/instructions.xml [new file with mode: 0644]

index 8e85ff082853fd11c5a12ae8dbda85af551080f9..1ed3b65c0da8cc3d47b5f63e70f53ccd4b356ec5 100644 (file)
@@ -52,25 +52,25 @@ The DenonMarantz AVR supports the following channels (some channels are model sp
 | _Main zone_             |              |
 |  mainZone#power    | Switch (RW) | Main zone power on/off
 |  mainZone#volume       | Dimmer (RW) | Main zone volume
-|  mainZone#volumeDB     | Number (RW) | Main zone volume in dB (-80 offset)
+|  mainZone#volumeDB     | Number:Dimensionless (RW) | Main zone volume in dB (-80 offset)
 |  mainZone#mute             | Switch (RW) | Main zone mute
 |  mainZone#input            | String (RW) | Main zone input (e.g. TV, TUNER, ..)
 |  _Zone 2_               |              |
 |  zone2#power | Switch (RW) | Zone 2 power on/off
 |  zone2#volume | Dimmer (RW) | Zone 2 volume
-|  zone2#volumeDB | Number (RW) | Zone 2 volume in dB (-80 offset)
+|  zone2#volumeDB | Number:Dimensionless (RW) | Zone 2 volume in dB (-80 offset)
 |  zone2#mute | Switch (RW) | Zone 2 mute
 |  zone2#input | String (RW) | Zone 2 input
 |  _Zone 3_               |              |
 |  zone3#power | Switch (RW) | Zone 3 power on/off
 |  zone3#volume | Dimmer (RW) | Zone 3 volume
-|  zone3#volumeDB | Number (RW) | Zone 3 volume in dB (-80 offset)
+|  zone3#volumeDB | Number:Dimensionless (RW) | Zone 3 volume in dB (-80 offset)
 |  zone3#mute | Switch (RW) | Zone 3 mute
 |  zone3#input | String (RW) | Zone 3 input
 |  _Zone 4_               |              |
 |  zone4#power | Switch (RW) | Zone 4 power on/off
 |  zone4#volume | Dimmer (RW) | Zone 4 volume
-|  zone4#volumeDB | Number (RW) | Zone 4 volume in dB (-80 offset)
+|  zone4#volumeDB | Number:Dimensionless (RW) | Zone 4 volume in dB (-80 offset)
 |  zone4#mute | Switch (RW) | Zone 4 mute
 |  zone4#input | String (RW) | Zone 4 input
 
@@ -89,14 +89,14 @@ Thing denonmarantz:avr:1 "Receiver" @ "Living room" [host="192.168.1.100"]
 `.items` file:
 
 ```java
-Switch marantz_power    "Receiver" <switch>         {channel="denonmarantz:avr:1:general#power"}
-Dimmer marantz_volume   "Volume"   <soundvolume>    {channel="denonmarantz:avr:1:mainZone#volume"}
-Number marantz_volumeDB "Volume [%.1f dB]"          {channel="denonmarantz:avr:1:mainzone#volume"}
-Switch marantz_mute     "Mute"     <mute>           {channel="denonmarantz:avr:1:mainZone#mute"}
-Switch marantz_z2power  "Zone 2"                    {channel="denonmarantz:avr:1:zone2#power"}
-String marantz_input    "Input [%s]"                {channel="denonmarantz:avr:1:mainZone#input" }
-String marantz_surround "Surround: [%s]"            {channel="denonmarantz:avr:1:general#surroundProgram"}
-String marantz_command                              {channel="denonmarantz:avr:1:general#command"}
+Switch               marantz_power    "Receiver" <switch>         {channel="denonmarantz:avr:1:general#power"}
+Dimmer               marantz_volume   "Volume"   <soundvolume>    {channel="denonmarantz:avr:1:mainZone#volume"}
+Number:Dimensionless marantz_volumeDB "Volume [%.1f dB]"          {channel="denonmarantz:avr:1:mainzone#volume", unit="dB"}
+Switch               marantz_mute     "Mute"     <mute>           {channel="denonmarantz:avr:1:mainZone#mute"}
+Switch               marantz_z2power  "Zone 2"                    {channel="denonmarantz:avr:1:zone2#power"}
+String               marantz_input    "Input [%s]"                {channel="denonmarantz:avr:1:mainZone#input" }
+String               marantz_surround "Surround: [%s]"            {channel="denonmarantz:avr:1:general#surroundProgram"}
+String               marantz_command                              {channel="denonmarantz:avr:1:general#command"}
 ```
 
 `.sitemap` file:
index d79e8e263cef31e3d5e32ec2461e55cef238c186..c45bae5de8ab9b5701cf53e005616f3fea84f722 100644 (file)
@@ -16,10 +16,11 @@ import java.math.BigDecimal;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.library.types.OnOffType;
 import org.openhab.core.library.types.PercentType;
+import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.library.types.StringType;
+import org.openhab.core.library.unit.Units;
 import org.openhab.core.types.State;
 
 /**
@@ -164,8 +165,8 @@ public class DenonMarantzState {
             this.mainVolume = newVal;
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME, newVal);
             // update the main volume in dB too
-            State mainVolumeDB = this.mainVolumeDB = DecimalType
-                    .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
+            State mainVolumeDB = this.mainVolumeDB = new QuantityType<>(
+                    volume.subtract(DenonMarantzBindingConstants.DB_OFFSET), Units.DECIBEL);
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB, mainVolumeDB);
         }
     }
@@ -224,8 +225,8 @@ public class DenonMarantzState {
             this.zone2Volume = newVal;
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME, newVal);
             // update the volume in dB too
-            State zone2VolumeDB = this.zone2VolumeDB = DecimalType
-                    .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
+            State zone2VolumeDB = this.zone2VolumeDB = new QuantityType<>(
+                    volume.subtract(DenonMarantzBindingConstants.DB_OFFSET), Units.DECIBEL);
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB, zone2VolumeDB);
         }
     }
@@ -260,8 +261,8 @@ public class DenonMarantzState {
             this.zone3Volume = newVal;
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME, newVal);
             // update the volume in dB too
-            State zone3VolumeDB = this.zone3VolumeDB = DecimalType
-                    .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
+            State zone3VolumeDB = this.zone3VolumeDB = new QuantityType<>(
+                    volume.subtract(DenonMarantzBindingConstants.DB_OFFSET), Units.DECIBEL);
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB, zone3VolumeDB);
         }
     }
@@ -296,8 +297,8 @@ public class DenonMarantzState {
             this.zone4Volume = newVal;
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME, newVal);
             // update the volume in dB too
-            State zone4VolumeDB = this.zone4VolumeDB = DecimalType
-                    .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
+            State zone4VolumeDB = this.zone4VolumeDB = new QuantityType<>(
+                    volume.subtract(DenonMarantzBindingConstants.DB_OFFSET), Units.DECIBEL);
             handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB, zone4VolumeDB);
         }
     }
index c9d99c0d5917a4241bdbfae25eb88909d5de0cc1..15da922ba48d78b0f8686ebf7bd46092a98ffb19 100644 (file)
@@ -26,7 +26,9 @@ import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.library.types.IncreaseDecreaseType;
 import org.openhab.core.library.types.OnOffType;
 import org.openhab.core.library.types.PercentType;
+import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.library.types.StringType;
+import org.openhab.core.library.unit.Units;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.RefreshType;
 
@@ -188,9 +190,16 @@ public abstract class DenonMarantzConnector {
         Command dbCommand = command;
         if (dbCommand instanceof PercentType) {
             throw new UnsupportedCommandTypeException();
-        } else if (dbCommand instanceof DecimalType) {
+        } else if (dbCommand instanceof DecimalType decimalCommand) {
             // convert dB to 'normal' volume by adding the offset of 80
-            dbCommand = new DecimalType(((DecimalType) command).toBigDecimal().add(DB_OFFSET));
+            dbCommand = new DecimalType(decimalCommand.toBigDecimal().add(DB_OFFSET));
+        } else if (dbCommand instanceof QuantityType<?> quantityCommand) {
+            QuantityType<?> decibelCommand = quantityCommand.toUnit(Units.DECIBEL);
+            if (decibelCommand != null) {
+                dbCommand = new DecimalType(new BigDecimal(decibelCommand.doubleValue()).add(DB_OFFSET));
+            } else {
+                throw new UnsupportedCommandTypeException();
+            }
         }
         sendVolumeCommand(dbCommand, zone);
     }
index d79bcc061f26dcbd658b8d8a7e68b43a47b8fd3e..b55e20948cf5b13ff02fd981b2202505938d4d38 100644 (file)
@@ -22,7 +22,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
 /**
- * Maps Denon volume values in db to percentage
+ * Maps Denon volume values in dB to percentage
  *
  * @author Jeroen Idserda - Initial contribution
  */
index 7e9e72f0450919f6b19cac903dd63318e25be6c5..1030e7d36d5f8fb9f3268b52653ae95793e15ac0 100644 (file)
                        </channel-group>
                </channel-groups>
 
+               <properties>
+                       <property name="thingTypeVersion">1</property>
+               </properties>
+
                <representation-property>serialNumber</representation-property>
 
                <config-description>
        </channel-type>
 
        <channel-type id="volumeDB" advanced="true">
-               <item-type>Number</item-type>
+               <item-type unitHint="dB">Number:Dimensionless</item-type>
                <label>Volume (dB)</label>
                <description>Set the volume level (dB). Same as [mainVolume - 80].</description>
                <category>SoundVolume</category>
-               <state min="-80" max="18" step="0.5" pattern="%.1f dB"/>
+               <state min="-80" max="18" step="0.5" pattern="%.1f %unit%"/>
        </channel-type>
 
        <channel-type id="mute">
diff --git a/bundles/org.openhab.binding.denonmarantz/src/main/resources/OH-INF/update/instructions.xml b/bundles/org.openhab.binding.denonmarantz/src/main/resources/OH-INF/update/instructions.xml
new file mode 100644 (file)
index 0000000..c7d0619
--- /dev/null
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
+<update:update-descriptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:update="https://openhab.org/schemas/update-description/v1.0.0"
+       xsi:schemaLocation="https://openhab.org/schemas/update-description/v1.0.0 https://openhab.org/schemas/update-description-1.0.0.xsd">
+
+       <thing-type uid="denonmarantz:avr">
+
+               <instruction-set targetVersion="1">
+                       <update-channel id="volumeDB" groupIds="mainZone,zone2,zone3,zone4">
+                               <type>denonmarantz:volumeDB</type>
+                       </update-channel>
+               </instruction-set>
+
+       </thing-type>
+
+</update:update-descriptions>