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

[squeezebox] Add UoM support for time channels #15997

Merged
merged 1 commit into from
Dec 5, 2023
Merged
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
64 changes: 32 additions & 32 deletions bundles/org.openhab.binding.squeezebox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,38 @@ The Squeezebox server supports the following channel:

All devices support some of the following channels:

| Channel Type ID | Item Type | Description |
| ---------------------- | --------- | -------------------------------------------------------------------------------------------- |
| power | Switch | Power on/off your device |
| mute | Switch | Mute/unmute your device |
| volume | Dimmer | Volume of your device |
| stop | Switch | Stop the current title |
| control | Player | Control the Zone Player, e.g. play/pause/next/previous/ffward/rewind |
| stream | String | Play the given HTTP or file stream (file:// or http://) |
| source | String | Shows the source of the currently playing playlist entry. (i.e. `http://radio.org/radio.mp3` |
| sync | String | Add another player to your device for synchronized playback (other player mac address) |
| playListIndex | Number | Playlist Index |
| currentPlayingTime | Number | Current Playing Time |
| duration | Number | Duration of currently playing track (in seconds) |
| currentPlaylistShuffle | Number | Current playlist shuffle mode (0 No Shuffle, 1 Shuffle Songs, 2 Shuffle Albums) |
| currentPlaylistRepeat | Number | Current playlist repeat Mode (0 No Repeat, 1 Repeat Song, 2 Repeat Playlist) |
| title | String | Title of the current song |
| remotetitle | String | Remote Title (Radio) of the current song |
| album | String | Album name of the current song |
| artist | String | Artist name of the current song |
| year | String | Release year of the current song |
| genre | String | Genre name of the current song |
| albumArtist | String | Main artist of the entire album |
| trackArtist | String | Main artist of the track |
| band | String | Band/orchestra that performed the work |
| composer | String | Original composer of the work |
| conductor | String | Person who conducted the performance |
| coverartdata | Image | Image data of cover art of the current song |
| ircode | String | Received IR code |
| numberPlaylistTracks | Number | Number of playlist tracks |
| playFavorite | String | ID of Favorite to play (channel's state options contains available favorites) |
| rate | Switch | "Like" or "unlike" the currently playing song (if supported by the streaming service) |
| sleep | Number | Power off the player in the specified number of minutes. Sending 0 cancels the timer |
| Channel Type ID | Item Type | Description |
| ---------------------- | ----------- | -------------------------------------------------------------------------------------------- |
| power | Switch | Power on/off your device |
| mute | Switch | Mute/unmute your device |
| volume | Dimmer | Volume of your device |
| stop | Switch | Stop the current title |
| control | Player | Control the Zone Player, e.g. play/pause/next/previous/ffward/rewind |
| stream | String | Play the given HTTP or file stream (file:// or http://) |
| source | String | Shows the source of the currently playing playlist entry. (i.e. `http://radio.org/radio.mp3` |
| sync | String | Add another player to your device for synchronized playback (other player mac address) |
| playListIndex | Number | Playlist Index |
| currentPlayingTime | Number:Time | Current Playing Time |
| duration | Number:Time | Duration of currently playing track (in seconds) |
| currentPlaylistShuffle | Number | Current playlist shuffle mode (0 No Shuffle, 1 Shuffle Songs, 2 Shuffle Albums) |
| currentPlaylistRepeat | Number | Current playlist repeat Mode (0 No Repeat, 1 Repeat Song, 2 Repeat Playlist) |
| title | String | Title of the current song |
| remotetitle | String | Remote Title (Radio) of the current song |
| album | String | Album name of the current song |
| artist | String | Artist name of the current song |
| year | String | Release year of the current song |
| genre | String | Genre name of the current song |
| albumArtist | String | Main artist of the entire album |
| trackArtist | String | Main artist of the track |
| band | String | Band/orchestra that performed the work |
| composer | String | Original composer of the work |
| conductor | String | Person who conducted the performance |
| coverartdata | Image | Image data of cover art of the current song |
| ircode | String | Received IR code |
| numberPlaylistTracks | Number | Number of playlist tracks |
| playFavorite | String | ID of Favorite to play (channel's state options contains available favorites) |
| rate | Switch | "Like" or "unlike" the currently playing song (if supported by the streaming service) |
| sleep | Number | Power off the player in the specified number of minutes. Sending 0 cancels the timer |

## Example .Items File

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.PlayPauseType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.RewindFastforwardType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
Expand Down Expand Up @@ -297,7 +299,14 @@ public void handleCommand(ChannelUID channelUID, Command command) {
squeezeBoxServerHandler.playPlaylistItem(mac, ((DecimalType) command).intValue());
break;
case CHANNEL_CURRENT_PLAYING_TIME:
squeezeBoxServerHandler.setPlayingTime(mac, ((DecimalType) command).intValue());
if (command instanceof DecimalType decimalCommand) {
squeezeBoxServerHandler.setPlayingTime(mac, decimalCommand.intValue());
} else if (command instanceof QuantityType<?> quantityCommand) {
QuantityType<?> quantitySeconds = quantityCommand.toUnit(Units.SECOND);
if (quantitySeconds != null) {
squeezeBoxServerHandler.setPlayingTime(mac, quantitySeconds.intValue());
}
}
break;
case CHANNEL_CURRENT_PLAYLIST_SHUFFLE:
squeezeBoxServerHandler.setShuffleMode(mac, ((DecimalType) command).intValue());
Expand Down Expand Up @@ -390,7 +399,7 @@ public void currentPlaylistIndexEvent(String mac, int index) {

@Override
public void currentPlayingTimeEvent(String mac, int time) {
updateChannel(mac, CHANNEL_CURRENT_PLAYING_TIME, new DecimalType(time));
updateChannel(mac, CHANNEL_CURRENT_PLAYING_TIME, new QuantityType<>(time, Units.SECOND));
if (isMe(mac)) {
currentTime = time;
}
Expand All @@ -402,7 +411,7 @@ public void durationEvent(String mac, int duration) {
logger.debug("Channel 'duration' does not exist. Delete and readd player thing to pick up channel.");
return;
}
updateChannel(mac, CHANNEL_DURATION, new DecimalType(duration));
updateChannel(mac, CHANNEL_DURATION, new QuantityType<>(duration, Units.SECOND));
}

@Override
Expand Down Expand Up @@ -646,7 +655,15 @@ private boolean cachedStateAsBoolean(String key, @NonNull State activeState) {

private int cachedStateAsInt(String key) {
State state = stateMap.get(key);
return state instanceof DecimalType decimalValue ? decimalValue.intValue() : 0;
if (state instanceof DecimalType decimalValue) {
return decimalValue.intValue();
} else if (state instanceof QuantityType<?> quantityValue) {
QuantityType<?> quantitySeconds = quantityValue.toUnit(Units.SECOND);
if (quantitySeconds != null) {
return quantitySeconds.intValue();
}
}
return 0;
}

/**
Expand All @@ -655,7 +672,7 @@ private int cachedStateAsInt(String key) {
private void timeCounter() {
timeCounterJob = scheduler.scheduleWithFixedDelay(() -> {
if (playing) {
updateChannel(mac, CHANNEL_CURRENT_PLAYING_TIME, new DecimalType(currentTime++));
updateChannel(mac, CHANNEL_CURRENT_PLAYING_TIME, new QuantityType<>(currentTime++, Units.SECOND));
}
}, 0, 1, TimeUnit.SECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<property name="name"></property>
<property name="uid"></property>
<property name="ip"></property>
<property name="thingTypeVersion">1</property>
</properties>

<representation-property>mac</representation-property>
Expand Down Expand Up @@ -216,14 +217,18 @@
<description>Playlist index</description>
</channel-type>
<channel-type id="currentPlayingTime">
<item-type>Number</item-type>
<item-type>Number:Time</item-type>
<label>Current Playing Time</label>
<description>Current Playing Time</description>
<category>Time</category>
<state pattern="%.0f %unit%"/>
</channel-type>
<channel-type id="duration">
<item-type>Number</item-type>
<item-type>Number:Time</item-type>
<label>Track Duration</label>
<description>Duration of Current Track (in seconds)</description>
<category>Time</category>
<state pattern="%.0f %unit%" readOnly="true"/>
</channel-type>
<channel-type id="currentPlaylistShuffle">
<item-type>Number</item-type>
Expand Down
Original file line number Diff line number Diff line change
@@ -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="squeezebox:squeezeboxplayer">
<instruction-set targetVersion="1">
<update-channel id="currentPlayingTime">
<type>squeezebox:currentPlayingTime</type>
</update-channel>
<update-channel id="duration">
<type>squeezebox:duration</type>
</update-channel>
</instruction-set>
</thing-type>

</update:update-descriptions>