Skip to content

Commit

Permalink
[sonos] Add new channels for battery (Sonos Move) (openhab#9998)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
Signed-off-by: Luca Calcaterra <[email protected]>
  • Loading branch information
lolodomo authored and lucacalcaterra committed Feb 23, 2021
1 parent 7074532 commit 68bf722
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bundles/org.openhab.binding.sonos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ The devices support the following channels:
| alarmproperties | String | R | Properties of the alarm currently running | all |
| alarmrunning | Switch | R | Set to ON if the alarm was triggered | all |
| bass | Number | RW | Set or get the bass level adjustment (value in range -10 / 10) | all |
| batterycharging | Switch | R | Indicator set to ON when the battery is charging | Move |
| batterylevel | Number | R | Current battery level | Move |
| clearqueue | Switch | W | Suppress all songs from the current queue | all |
| control | Player | RW | Control the Zone Player, e.g. PLAY/PAUSE/NEXT/PREVIOUS | all |
| coordinator | String | R | UDN of the coordinator for the current group | all |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class SonosBindingConstants {
public static final String ALARMPROPERTIES = "alarmproperties";
public static final String ALARMRUNNING = "alarmrunning";
public static final String BASS = "bass";
public static final String BATTERYCHARGING = "batterycharging";
public static final String BATTERYLEVEL = "batterylevel";
public static final String CLEARQUEUE = "clearqueue";
public static final String CONTROL = "control";
public static final String COORDINATOR = "coordinator";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ public void onValueReceived(@Nullable String variable, @Nullable String value, @
stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), RADIO), options);
}
break;
case "MoreInfo":
updateChannel(BATTERYCHARGING);
updateChannel(BATTERYLEVEL);
break;
default:
break;
}
Expand Down Expand Up @@ -957,6 +961,18 @@ protected void updateChannel(String channelId) {
newState = new StringType(value);
}
break;
case BATTERYCHARGING:
value = extractInfoFromMoreInfo("BattChg");
if (value != null) {
newState = OnOffType.from("CHARGING".equalsIgnoreCase(value));
}
break;
case BATTERYLEVEL:
value = extractInfoFromMoreInfo("RawBattPct");
if (value != null) {
newState = new DecimalType(value);
}
break;
default:
newState = null;
break;
Expand Down Expand Up @@ -3225,4 +3241,18 @@ private long sleepStrTimeToSeconds(String sleepTime) {
int seconds = Integer.parseInt(units[2]);
return 3600 * hours + 60 * minutes + seconds;
}

private @Nullable String extractInfoFromMoreInfo(String searchedInfo) {
String value = stateMap.get("MoreInfo");
if (value != null) {
String[] fields = value.split(",");
for (int i = 0; i < fields.length; i++) {
String[] pair = fields[i].trim().split(":");
if (pair.length == 2 && searchedInfo.equalsIgnoreCase(pair[0].trim())) {
return pair[1].trim();
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<channel id="sleeptimer" typeId="sleeptimer"/>
<channel id="currenttransporturi" typeId="currenttransporturi"/>
<channel id="currenttrackuri" typeId="currenttrackuri"/>
<!-- Extended SONOS channels -->
<channel id="batterycharging" typeId="batterycharging"/>
<channel id="batterylevel" typeId="system.battery-level"/>
</channels>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,12 @@
<description>Play the line-in of the the Zone Player corresponding to the given UIN</description>
</channel-type>

<!-- Extended channels (for SONOS Move only) -->
<channel-type id="batterycharging" advanced="true">
<item-type>Switch</item-type>
<label>Battery Charging</label>
<description>Indicator set to ON when the battery is charging</description>
<state readOnly="true"/>
</channel-type>

</thing:thing-descriptions>

0 comments on commit 68bf722

Please sign in to comment.