Skip to content

Commit

Permalink
[amazonechocontrol] Add do not disturb channel
Browse files Browse the repository at this point in the history
Signed-off-by: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer committed Dec 20, 2024
1 parent 7a3380a commit 2e344cb
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 5 deletions.
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.amazonechocontrol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ The only possibility to find out the id is by using the discover function in the
| mediaLength | Number:Time | R | echo, echoshow, echospot | Media length |
| notificationVolume | Dimmer | R | echo, echoshow, echospot | Notification volume |
| ascendingAlarm | Switch | R/W | echo, echoshow, echospot | Ascending alarm up to the configured volume |
| doNotDisturb | Switch | R/W | echo, echoshow, echospot | Do Not Disturb mode enabled |
| sendMessage | String | W | account | Sends a message to the Echo devices. |
| save | Switch | W | flashbriefingprofile | Stores the current configuration of flash briefings within the thing |
| active | Switch | R/W | flashbriefingprofile | Active the profile |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class AmazonEchoControlBindingConstants {
public static final String CHANNEL_MEDIA_LENGTH = "mediaLength";
public static final String CHANNEL_MEDIA_PROGRESS_TIME = "mediaProgressTime";
public static final String CHANNEL_ASCENDING_ALARM = "ascendingAlarm";
public static final String CHANNEL_DO_NOT_DISTURB = "doNotDisturb";
public static final String CHANNEL_NOTIFICATION_VOLUME = "notificationVolume";
public static final String CHANNEL_NEXT_REMINDER = "nextReminder";
public static final String CHANNEL_NEXT_ALARM = "nextAlarm";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDeviceNotificationState.DeviceNotificationState;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDoNotDisturb;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDoNotDisturb.DoNotDisturbDeviceStatus;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonEnabledFeeds;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonEqualizer;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonExchangeTokenResponse;
Expand Down Expand Up @@ -1313,6 +1315,14 @@ public void ascendingAlarm(Device device, boolean ascendingAlarm)
makeRequest("PUT", url, command, true, true, null, 0);
}

public void doNotDisturb(Device device, boolean doNotDisturb)
throws IOException, URISyntaxException, InterruptedException {
String url = alexaServer + "/api/dnd/status";
String command = "{\"enabled\":" + (doNotDisturb ? "true" : "false") + ",\"deviceSerialNumber\":\""
+ device.serialNumber + "\",\"deviceType\":\"" + device.deviceType + "\",\"deviceAccountId\":null}";
makeRequest("PUT", url, command, true, true, null, 0);
}

public List<DeviceNotificationState> getDeviceNotificationStates() {
try {
String json = makeRequestAndReturnString(alexaServer + "/api/device-notification-state");
Expand All @@ -1336,6 +1346,18 @@ public List<AscendingAlarmModel> getAscendingAlarm() {
return List.of();
}

public List<DoNotDisturbDeviceStatus> getDoNotDisturb() {
String json;
try {
json = makeRequestAndReturnString(alexaServer + "/api/dnd/device-status-list");
JsonDoNotDisturb result = parseJson(json, JsonDoNotDisturb.class);
return Objects.requireNonNullElse(result.doNotDisturbDeviceStatusList, List.of());
} catch (IOException | URISyntaxException | InterruptedException e) {
logger.info("Error getting do not disturb status list", e);
}
return List.of();
}

public void bluetooth(Device device, @Nullable String address)
throws IOException, URISyntaxException, InterruptedException {
if (address == null || address.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushNotificationChange;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDeviceNotificationState.DeviceNotificationState;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDoNotDisturb.DoNotDisturbDeviceStatus;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonFeed;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonMusicProvider;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonNotificationResponse;
Expand Down Expand Up @@ -509,6 +510,7 @@ private void refreshData() {

List<DeviceNotificationState> deviceNotificationStates = List.of();
List<AscendingAlarmModel> ascendingAlarmModels = List.of();
List<DoNotDisturbDeviceStatus> doNotDisturbDeviceStatuses = List.of();
JsonBluetoothStates states = null;
List<JsonMusicProvider> musicProviders = null;
if (currentConnection.getIsLoggedIn()) {
Expand All @@ -518,6 +520,9 @@ private void refreshData() {
// update ascending alarm
ascendingAlarmModels = currentConnection.getAscendingAlarm();

// update do not disturb
doNotDisturbDeviceStatuses = currentConnection.getDoNotDisturb();

// update bluetooth states
states = currentConnection.getBluetoothConnectionStates();

Expand Down Expand Up @@ -557,6 +562,7 @@ private void refreshData() {
}
DeviceNotificationState deviceNotificationState = null;
AscendingAlarmModel ascendingAlarmModel = null;
DoNotDisturbDeviceStatus doNotDisturbDeviceStatus = null;
if (device != null) {
final String serialNumber = device.serialNumber;
if (serialNumber != null) {
Expand All @@ -566,10 +572,13 @@ private void refreshData() {
deviceNotificationState = deviceNotificationStates.stream()
.filter(current -> serialNumber.equals(current.deviceSerialNumber)).findFirst()
.orElse(null);
doNotDisturbDeviceStatus = doNotDisturbDeviceStatuses.stream()
.filter(current -> serialNumber.equals(current.deviceSerialNumber)).findFirst()
.orElse(null);
}
}
child.updateState(this, device, state, deviceNotificationState, ascendingAlarmModel, playlists,
notificationSounds, musicProviders);
child.updateState(this, device, state, deviceNotificationState, ascendingAlarmModel,
doNotDisturbDeviceStatus, playlists, notificationSounds, musicProviders);
}

// refresh notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonCommandPayloadPushVolumeChange;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDeviceNotificationState.DeviceNotificationState;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDevices.Device;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonDoNotDisturb.DoNotDisturbDeviceStatus;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonEqualizer;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonMediaState;
import org.openhab.binding.amazonechocontrol.internal.jsons.JsonMediaState.QueueEntry;
Expand Down Expand Up @@ -124,6 +125,7 @@ public class EchoHandler extends BaseThingHandler implements IEchoThingHandler {
private boolean updateStartCommand = true;
private @Nullable Integer notificationVolumeLevel;
private @Nullable Boolean ascendingAlarm;
private @Nullable Boolean doNotDisturb;
private @Nullable JsonPlaylists playLists;
private List<JsonNotificationSound> alarmSounds = List.of();
private List<JsonMusicProvider> musicProviders = List.of();
Expand Down Expand Up @@ -315,6 +317,14 @@ public void handleCommand(ChannelUID channelUID, Command command) {
account.forceCheckData();
}
}
// Do Not Disturb command
if (channelId.equals(CHANNEL_DO_NOT_DISTURB) && command instanceof OnOffType) {
boolean newDnd = OnOffType.ON.equals(command);
connection.doNotDisturb(device, newDnd);
this.doNotDisturb = true;
waitForUpdate = -1;
account.forceCheckData();
}
// Media progress commands
Long mediaPosition = null;
if (channelId.equals(CHANNEL_MEDIA_PROGRESS)) {
Expand Down Expand Up @@ -652,7 +662,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}

updateState(account, device, state, null, null, null, null, null);
updateState(account, device, state, null, null, null, null, null, null);
};
if (command instanceof RefreshType) {
waitForUpdate = 0;
Expand Down Expand Up @@ -791,7 +801,8 @@ private void updateNotificationTimerState() {

public void updateState(AccountHandler accountHandler, @Nullable Device device,
@Nullable BluetoothState bluetoothState, @Nullable DeviceNotificationState deviceNotificationState,
@Nullable AscendingAlarmModel ascendingAlarmModel, @Nullable JsonPlaylists playlists,
@Nullable AscendingAlarmModel ascendingAlarmModel,
@Nullable DoNotDisturbDeviceStatus doNotDisturbDeviceStatus, @Nullable JsonPlaylists playlists,
@Nullable List<JsonNotificationSound> alarmSounds, @Nullable List<JsonMusicProvider> musicProviders) {
try {
this.logger.debug("Handle updateState {}", this.getThing().getUID());
Expand All @@ -802,6 +813,9 @@ public void updateState(AccountHandler accountHandler, @Nullable Device device,
if (ascendingAlarmModel != null) {
ascendingAlarm = ascendingAlarmModel.ascendingAlarmEnabled;
}
if (doNotDisturbDeviceStatus != null) {
doNotDisturb = doNotDisturbDeviceStatus.enabled;
}
if (playlists != null) {
this.playLists = playlists;
}
Expand Down Expand Up @@ -1119,6 +1133,9 @@ public void updateState(AccountHandler accountHandler, @Nullable Device device,
updateState(CHANNEL_ASCENDING_ALARM,
ascendingAlarm != null ? OnOffType.from(ascendingAlarm) : UnDefType.UNDEF);

updateState(CHANNEL_DO_NOT_DISTURB,
doNotDisturb != null ? (doNotDisturb ? OnOffType.ON : OnOffType.OFF) : UnDefType.UNDEF);

final Integer notificationVolumeLevel = this.notificationVolumeLevel;
if (notificationVolumeLevel != null) {
updateState(CHANNEL_NOTIFICATION_VOLUME, new PercentType(notificationVolumeLevel));
Expand Down Expand Up @@ -1260,7 +1277,7 @@ public void handlePushCommand(String command, String payload) {
Device device = this.device;
if (account != null && device != null) {
this.disableUpdate = false;
updateState(account, device, null, null, null, null, null, null);
updateState(account, device, null, null, null, null, null, null, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.amazonechocontrol.internal.jsons;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link JsonDoNotDisturb} encapsulate the GSON data of the /api/dnd/device-status-list response
*
* @author Cody Cutrer - Initial contribution
*/
@NonNullByDefault
public class JsonDoNotDisturb {

public @Nullable List<DoNotDisturbDeviceStatus> doNotDisturbDeviceStatusList;

public static class DoNotDisturbDeviceStatus {
public @Nullable Boolean enabled;
public @Nullable String deviceSerialNumber;
public @Nullable String deviceType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ channel-type.amazonechocontrol.colorName.label = Color Name
channel-type.amazonechocontrol.colorName.description = Color Name
channel-type.amazonechocontrol.colorTemperatureName.label = Color Temperature Name
channel-type.amazonechocontrol.colorTemperatureName.description = Color Temperature Name
channel-type.amazonechocontrol.doNotDisturb.label = Do Not Disturb
channel-type.amazonechocontrol.doNotDisturb.description = Do Not Disturb mode enabled
channel-type.amazonechocontrol.equalizerBass.label = Bass
channel-type.amazonechocontrol.equalizerBass.description = Equalizer Bass
channel-type.amazonechocontrol.equalizerMidrange.label = Midrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@
<channel id="lastVoiceCommand" typeId="lastVoiceCommand"/>
<channel id="notificationVolume" typeId="notificationVolume"/>
<channel id="ascendingAlarm" typeId="ascendingAlarm"/>
<channel id="doNotDisturb" typeId="doNotDisturb"/>
</channels>
<properties>
<property name="thingTypeVersion">1</property>
</properties>
<representation-property>serialNumber</representation-property>
<config-description>
<parameter name="serialNumber" type="text" required="true">
Expand Down Expand Up @@ -142,7 +146,11 @@
<channel id="lastVoiceCommand" typeId="lastVoiceCommand"/>
<channel id="notificationVolume" typeId="notificationVolume"/>
<channel id="ascendingAlarm" typeId="ascendingAlarm"/>
<channel id="doNotDisturb" typeId="doNotDisturb"/>
</channels>
<properties>
<property name="thingTypeVersion">1</property>
</properties>
<representation-property>serialNumber</representation-property>
<config-description>
<parameter name="serialNumber" type="text" required="true">
Expand Down Expand Up @@ -197,7 +205,11 @@
<channel id="lastVoiceCommand" typeId="lastVoiceCommand"/>
<channel id="notificationVolume" typeId="notificationVolume"/>
<channel id="ascendingAlarm" typeId="ascendingAlarm"/>
<channel id="doNotDisturb" typeId="doNotDisturb"/>
</channels>
<properties>
<property name="thingTypeVersion">1</property>
</properties>
<representation-property>serialNumber</representation-property>
<config-description>
<parameter name="serialNumber" type="text" required="true">
Expand Down Expand Up @@ -517,6 +529,11 @@
<description>Ascending alarm up to the configured volume</description>
<state readOnly="false"/>
</channel-type>
<channel-type id="doNotDisturb">
<item-type>Switch</item-type>
<label>Do Not Disturb</label>
<description>Do Not Disturb mode enabled</description>
</channel-type>
<channel-type id="nextReminder" advanced="true">
<item-type>DateTime</item-type>
<label>Next Reminder</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,34 @@
</instruction-set>
</thing-type>

<thing-type uid="amazonechocontrol:echo">
<instruction-set targetVersion="1">
<add-channel id="doNotDisturb">
<type>amazoneechoecontrol:doNotDisturb</type>
<label>Do Not Disturb</label>
<description>Do Not Disturb mode enabled</description>
</add-channel>
</instruction-set>
</thing-type>

<thing-type uid="amazonechocontrol:echospot">
<instruction-set targetVersion="1">
<add-channel id="doNotDisturb">
<type>amazoneechoecontrol:doNotDisturb</type>
<label>Do Not Disturb</label>
<description>Do Not Disturb mode enabled</description>
</add-channel>
</instruction-set>
</thing-type>

<thing-type uid="amazonechocontrol:echoshow">
<instruction-set targetVersion="1">
<add-channel id="doNotDisturb">
<type>amazoneechoecontrol:doNotDisturb</type>
<label>Do Not Disturb</label>
<description>Do Not Disturb mode enabled</description>
</add-channel>
</instruction-set>
</thing-type>

</update:update-descriptions>

0 comments on commit 2e344cb

Please sign in to comment.