2 * Copyright (c) 2010-2021 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.denonmarantz.internal;
15 import java.math.BigDecimal;
17 import org.apache.commons.lang.StringUtils;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.library.types.PercentType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.State;
25 * Represents the state of the handled DenonMarantz AVR
27 * @author Jan-Willem Veldhuis - Initial contribution
30 public class DenonMarantzState {
33 private State mainZonePower;
35 private State mainVolume;
36 private State mainVolumeDB;
38 private State surroundProgram;
44 // ------ Zones ------
45 private State zone2Power;
46 private State zone2Volume;
47 private State zone2VolumeDB;
48 private State zone2Mute;
49 private State zone2Input;
51 private State zone3Power;
52 private State zone3Volume;
53 private State zone3VolumeDB;
54 private State zone3Mute;
55 private State zone3Input;
57 private State zone4Power;
58 private State zone4Volume;
59 private State zone4VolumeDB;
60 private State zone4Mute;
61 private State zone4Input;
63 private DenonMarantzStateChangedListener handler;
65 public DenonMarantzState(DenonMarantzStateChangedListener handler) {
66 this.handler = handler;
69 public void connectionError(String errorMessage) {
70 handler.connectionError(errorMessage);
73 public State getStateForChannelID(String channelID) {
75 case DenonMarantzBindingConstants.CHANNEL_POWER:
77 case DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER:
79 case DenonMarantzBindingConstants.CHANNEL_MUTE:
81 case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME:
83 case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB:
85 case DenonMarantzBindingConstants.CHANNEL_INPUT:
87 case DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM:
88 return surroundProgram;
90 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST:
92 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM:
94 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK:
97 case DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER:
99 case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME:
101 case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB:
102 return zone2VolumeDB;
103 case DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE:
105 case DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT:
108 case DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER:
110 case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME:
112 case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB:
113 return zone3VolumeDB;
114 case DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE:
116 case DenonMarantzBindingConstants.CHANNEL_ZONE3_INPUT:
119 case DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER:
121 case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME:
123 case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB:
124 return zone4VolumeDB;
125 case DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE:
127 case DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT:
135 public void setPower(boolean power) {
136 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
137 if (newVal != this.power) {
139 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_POWER, this.power);
143 public void setMainZonePower(boolean mainPower) {
144 OnOffType newVal = mainPower ? OnOffType.ON : OnOffType.OFF;
145 if (newVal != this.mainZonePower) {
146 this.mainZonePower = newVal;
147 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER, this.mainZonePower);
151 public void setMute(boolean mute) {
152 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
153 if (newVal != this.mute) {
155 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MUTE, this.mute);
159 public void setMainVolume(BigDecimal volume) {
160 PercentType newVal = new PercentType(volume);
161 if (!newVal.equals(this.mainVolume)) {
162 this.mainVolume = newVal;
163 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME, this.mainVolume);
164 // update the main volume in dB too
165 this.mainVolumeDB = DecimalType.valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
166 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB, this.mainVolumeDB);
170 public void setInput(String input) {
171 StringType newVal = StringType.valueOf(input);
172 if (!newVal.equals(this.input)) {
174 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_INPUT, this.input);
178 public void setSurroundProgram(String surroundProgram) {
179 StringType newVal = StringType.valueOf(surroundProgram);
180 if (!newVal.equals(this.surroundProgram)) {
181 this.surroundProgram = newVal;
182 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM, this.surroundProgram);
186 public void setNowPlayingArtist(String artist) {
187 StringType newVal = StringUtils.isBlank(artist) ? StringType.EMPTY : StringType.valueOf(artist);
188 if (!newVal.equals(this.artist)) {
189 this.artist = newVal;
190 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST, this.artist);
194 public void setNowPlayingAlbum(String album) {
195 StringType newVal = StringUtils.isBlank(album) ? StringType.EMPTY : StringType.valueOf(album);
196 if (!newVal.equals(this.album)) {
198 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM, this.album);
202 public void setNowPlayingTrack(String track) {
203 StringType newVal = StringUtils.isBlank(track) ? StringType.EMPTY : StringType.valueOf(track);
204 if (!newVal.equals(this.track)) {
206 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK, this.track);
210 public void setZone2Power(boolean power) {
211 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
212 if (newVal != this.zone2Power) {
213 this.zone2Power = newVal;
214 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER, this.zone2Power);
218 public void setZone2Volume(BigDecimal volume) {
219 PercentType newVal = new PercentType(volume);
220 if (!newVal.equals(this.zone2Volume)) {
221 this.zone2Volume = newVal;
222 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME, this.zone2Volume);
223 // update the volume in dB too
224 this.zone2VolumeDB = DecimalType
225 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
226 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB, this.zone2VolumeDB);
230 public void setZone2Mute(boolean mute) {
231 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
232 if (newVal != this.zone2Mute) {
233 this.zone2Mute = newVal;
234 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE, this.zone2Mute);
238 public void setZone2Input(String zone2Input) {
239 StringType newVal = StringType.valueOf(zone2Input);
240 if (!newVal.equals(this.zone2Input)) {
241 this.zone2Input = newVal;
242 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, this.zone2Input);
246 public void setZone3Power(boolean power) {
247 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
248 if (newVal != this.zone3Power) {
249 this.zone3Power = newVal;
250 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER, this.zone3Power);
254 public void setZone3Volume(BigDecimal volume) {
255 PercentType newVal = new PercentType(volume);
256 if (!newVal.equals(this.zone3Volume)) {
257 this.zone3Volume = newVal;
258 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME, this.zone3Volume);
259 // update the volume in dB too
260 this.zone3VolumeDB = DecimalType
261 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
262 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB, this.zone3VolumeDB);
266 public void setZone3Mute(boolean mute) {
267 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
268 if (newVal != this.zone3Mute) {
269 this.zone3Mute = newVal;
270 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE, this.zone3Mute);
274 public void setZone3Input(String zone3Input) {
275 StringType newVal = StringType.valueOf(zone3Input);
276 if (!newVal.equals(this.zone3Input)) {
277 this.zone3Input = newVal;
278 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, this.zone3Input);
282 public void setZone4Power(boolean power) {
283 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
284 if (newVal != this.zone4Power) {
285 this.zone4Power = newVal;
286 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER, this.zone4Power);
290 public void setZone4Volume(BigDecimal volume) {
291 PercentType newVal = new PercentType(volume);
292 if (!newVal.equals(this.zone4Volume)) {
293 this.zone4Volume = newVal;
294 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME, this.zone4Volume);
295 // update the volume in dB too
296 this.zone4VolumeDB = DecimalType
297 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
298 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB, this.zone4VolumeDB);
302 public void setZone4Mute(boolean mute) {
303 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
304 if (newVal != this.zone4Mute) {
305 this.zone4Mute = newVal;
306 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE, this.zone4Mute);
310 public void setZone4Input(String zone4Input) {
311 StringType newVal = StringType.valueOf(zone4Input);
312 if (!newVal.equals(this.zone4Input)) {
313 this.zone4Input = newVal;
314 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT, this.zone4Input);