2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mycroft.internal.channels;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.mycroft.internal.MycroftBindingConstants;
20 import org.openhab.binding.mycroft.internal.MycroftHandler;
21 import org.openhab.binding.mycroft.internal.api.MessageType;
22 import org.openhab.binding.mycroft.internal.api.dto.BaseMessage;
23 import org.openhab.binding.mycroft.internal.api.dto.MessageVolumeMute;
24 import org.openhab.binding.mycroft.internal.api.dto.MessageVolumeSet;
25 import org.openhab.binding.mycroft.internal.api.dto.MessageVolumeUnmute;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.types.Command;
30 * The channel responsible for muting the Mycroft speaker
32 * @author Gwendal Roulleau - Initial contribution
35 public class MuteChannel extends MycroftChannel<OnOffType> {
37 private int volumeRestorationLevel;
39 public MuteChannel(MycroftHandler handler, int volumeRestorationLevel) {
40 super(handler, MycroftBindingConstants.VOLUME_MUTE_CHANNEL);
41 this.volumeRestorationLevel = volumeRestorationLevel;
45 public List<MessageType> getMessageToListenTo() {
46 // we don't listen to mute/unmute message because duck/unduck seems sufficient
47 // and we don't want to change state twice for the same event
48 // but it should be tested on mark I, as volume is handled differently
49 return Arrays.asList(MessageType.mycroft_volume_duck, MessageType.mycroft_volume_unduck,
50 MessageType.mycroft_volume_set, MessageType.mycroft_volume_increase);
54 public void messageReceived(BaseMessage message) {
55 switch (message.type) {
56 case mycroft_volume_mute:
57 case mycroft_volume_duck:
58 updateMyState(OnOffType.ON);
60 case mycroft_volume_unmute:
61 case mycroft_volume_unduck:
62 case mycroft_volume_increase:
63 updateMyState(OnOffType.OFF);
65 case mycroft_volume_set:
66 if (((MessageVolumeSet) message).data.percent > 0) {
67 updateMyState(OnOffType.OFF);
74 private boolean sendVolumeSetMessage(float volume) {
75 String messageToSend = VolumeChannel.VOLUME_SETTER_MESSAGE.replaceAll("\\$\\$VOLUME",
76 Float.valueOf(volume).toString());
77 return handler.sendMessage(messageToSend);
81 public void handleCommand(Command command) {
82 if (command instanceof OnOffType) {
83 if (command == OnOffType.ON) {
84 if (handler.sendMessage(new MessageVolumeMute())) {
85 updateMyState(OnOffType.ON);
87 } else if (command == OnOffType.OFF) {
88 if (handler.sendMessage(new MessageVolumeUnmute())) {
89 updateMyState(OnOffType.OFF);
90 // if configured, we can restore the volume to a fixed amount
91 // usefull as a workaround for the broken Mycroft volume behavior
92 if (volumeRestorationLevel > 0) {
93 // we must wait 100ms for Mycroft to handle the message and
94 // setting old volume before forcing to our value
97 } catch (InterruptedException e) {
99 sendVolumeSetMessage(Float.valueOf(volumeRestorationLevel));