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.denonmarantz.internal;
15 import java.math.BigDecimal;
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.types.State;
24 * Represents the state of the handled DenonMarantz AVR
26 * @author Jan-Willem Veldhuis - Initial contribution
29 public class DenonMarantzState {
32 private State mainZonePower;
34 private State mainVolume;
35 private State mainVolumeDB;
37 private State surroundProgram;
43 // ------ Zones ------
44 private State zone2Power;
45 private State zone2Volume;
46 private State zone2VolumeDB;
47 private State zone2Mute;
48 private State zone2Input;
50 private State zone3Power;
51 private State zone3Volume;
52 private State zone3VolumeDB;
53 private State zone3Mute;
54 private State zone3Input;
56 private State zone4Power;
57 private State zone4Volume;
58 private State zone4VolumeDB;
59 private State zone4Mute;
60 private State zone4Input;
62 private DenonMarantzStateChangedListener handler;
64 public DenonMarantzState(DenonMarantzStateChangedListener handler) {
65 this.handler = handler;
68 public void connectionError(String errorMessage) {
69 handler.connectionError(errorMessage);
72 public State getStateForChannelID(String channelID) {
74 case DenonMarantzBindingConstants.CHANNEL_POWER:
76 case DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER:
78 case DenonMarantzBindingConstants.CHANNEL_MUTE:
80 case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME:
82 case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB:
84 case DenonMarantzBindingConstants.CHANNEL_INPUT:
86 case DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM:
87 return surroundProgram;
89 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST:
91 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM:
93 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK:
96 case DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER:
98 case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME:
100 case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB:
101 return zone2VolumeDB;
102 case DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE:
104 case DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT:
107 case DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER:
109 case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME:
111 case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB:
112 return zone3VolumeDB;
113 case DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE:
115 case DenonMarantzBindingConstants.CHANNEL_ZONE3_INPUT:
118 case DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER:
120 case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME:
122 case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB:
123 return zone4VolumeDB;
124 case DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE:
126 case DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT:
134 public void setPower(boolean power) {
135 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
136 if (newVal != this.power) {
138 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_POWER, this.power);
142 public void setMainZonePower(boolean mainPower) {
143 OnOffType newVal = mainPower ? OnOffType.ON : OnOffType.OFF;
144 if (newVal != this.mainZonePower) {
145 this.mainZonePower = newVal;
146 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER, this.mainZonePower);
150 public void setMute(boolean mute) {
151 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
152 if (newVal != this.mute) {
154 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MUTE, this.mute);
158 public void setMainVolume(BigDecimal volume) {
159 PercentType newVal = new PercentType(volume);
160 if (!newVal.equals(this.mainVolume)) {
161 this.mainVolume = newVal;
162 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME, this.mainVolume);
163 // update the main volume in dB too
164 this.mainVolumeDB = DecimalType.valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
165 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB, this.mainVolumeDB);
169 public void setInput(String input) {
170 StringType newVal = StringType.valueOf(input);
171 if (!newVal.equals(this.input)) {
173 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_INPUT, this.input);
177 public void setSurroundProgram(String surroundProgram) {
178 StringType newVal = StringType.valueOf(surroundProgram);
179 if (!newVal.equals(this.surroundProgram)) {
180 this.surroundProgram = newVal;
181 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM, this.surroundProgram);
185 public void setNowPlayingArtist(String artist) {
186 StringType newVal = artist == null || artist.isBlank() ? StringType.EMPTY : StringType.valueOf(artist);
187 if (!newVal.equals(this.artist)) {
188 this.artist = newVal;
189 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST, this.artist);
193 public void setNowPlayingAlbum(String album) {
194 StringType newVal = album == null || album.isBlank() ? StringType.EMPTY : StringType.valueOf(album);
195 if (!newVal.equals(this.album)) {
197 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM, this.album);
201 public void setNowPlayingTrack(String track) {
202 StringType newVal = track == null || track.isBlank() ? StringType.EMPTY : StringType.valueOf(track);
203 if (!newVal.equals(this.track)) {
205 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK, this.track);
209 public void setZone2Power(boolean power) {
210 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
211 if (newVal != this.zone2Power) {
212 this.zone2Power = newVal;
213 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER, this.zone2Power);
217 public void setZone2Volume(BigDecimal volume) {
218 PercentType newVal = new PercentType(volume);
219 if (!newVal.equals(this.zone2Volume)) {
220 this.zone2Volume = newVal;
221 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME, this.zone2Volume);
222 // update the volume in dB too
223 this.zone2VolumeDB = DecimalType
224 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
225 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB, this.zone2VolumeDB);
229 public void setZone2Mute(boolean mute) {
230 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
231 if (newVal != this.zone2Mute) {
232 this.zone2Mute = newVal;
233 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE, this.zone2Mute);
237 public void setZone2Input(String zone2Input) {
238 StringType newVal = StringType.valueOf(zone2Input);
239 if (!newVal.equals(this.zone2Input)) {
240 this.zone2Input = newVal;
241 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, this.zone2Input);
245 public void setZone3Power(boolean power) {
246 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
247 if (newVal != this.zone3Power) {
248 this.zone3Power = newVal;
249 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER, this.zone3Power);
253 public void setZone3Volume(BigDecimal volume) {
254 PercentType newVal = new PercentType(volume);
255 if (!newVal.equals(this.zone3Volume)) {
256 this.zone3Volume = newVal;
257 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME, this.zone3Volume);
258 // update the volume in dB too
259 this.zone3VolumeDB = DecimalType
260 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
261 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB, this.zone3VolumeDB);
265 public void setZone3Mute(boolean mute) {
266 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
267 if (newVal != this.zone3Mute) {
268 this.zone3Mute = newVal;
269 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE, this.zone3Mute);
273 public void setZone3Input(String zone3Input) {
274 StringType newVal = StringType.valueOf(zone3Input);
275 if (!newVal.equals(this.zone3Input)) {
276 this.zone3Input = newVal;
277 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, this.zone3Input);
281 public void setZone4Power(boolean power) {
282 OnOffType newVal = power ? OnOffType.ON : OnOffType.OFF;
283 if (newVal != this.zone4Power) {
284 this.zone4Power = newVal;
285 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER, this.zone4Power);
289 public void setZone4Volume(BigDecimal volume) {
290 PercentType newVal = new PercentType(volume);
291 if (!newVal.equals(this.zone4Volume)) {
292 this.zone4Volume = newVal;
293 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME, this.zone4Volume);
294 // update the volume in dB too
295 this.zone4VolumeDB = DecimalType
296 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
297 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB, this.zone4VolumeDB);
301 public void setZone4Mute(boolean mute) {
302 OnOffType newVal = mute ? OnOffType.ON : OnOffType.OFF;
303 if (newVal != this.zone4Mute) {
304 this.zone4Mute = newVal;
305 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE, this.zone4Mute);
309 public void setZone4Input(String zone4Input) {
310 StringType newVal = StringType.valueOf(zone4Input);
311 if (!newVal.equals(this.zone4Input)) {
312 this.zone4Input = newVal;
313 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT, this.zone4Input);