2 * Copyright (c) 2010-2024 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.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.State;
26 * Represents the state of the handled DenonMarantz AVR
28 * @author Jan-Willem Veldhuis - Initial contribution
32 public class DenonMarantzState {
34 private @Nullable State power;
35 private @Nullable State mainZonePower;
36 private @Nullable State mute;
37 private @Nullable State mainVolume;
38 private @Nullable State mainVolumeDB;
39 private @Nullable State input;
40 private @Nullable State surroundProgram;
42 private @Nullable State artist;
43 private @Nullable State album;
44 private @Nullable State track;
46 // ------ Zones ------
47 private @Nullable State zone2Power;
48 private @Nullable State zone2Volume;
49 private @Nullable State zone2VolumeDB;
50 private @Nullable State zone2Mute;
51 private @Nullable State zone2Input;
53 private @Nullable State zone3Power;
54 private @Nullable State zone3Volume;
55 private @Nullable State zone3VolumeDB;
56 private @Nullable State zone3Mute;
57 private @Nullable State zone3Input;
59 private @Nullable State zone4Power;
60 private @Nullable State zone4Volume;
61 private @Nullable State zone4VolumeDB;
62 private @Nullable State zone4Mute;
63 private @Nullable State zone4Input;
65 private DenonMarantzStateChangedListener handler;
67 public DenonMarantzState(DenonMarantzStateChangedListener handler) {
68 this.handler = handler;
71 public void connectionError(String errorMessage) {
72 handler.connectionError(errorMessage);
75 public @Nullable State getStateForChannelID(String channelID) {
77 case DenonMarantzBindingConstants.CHANNEL_POWER:
79 case DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER:
81 case DenonMarantzBindingConstants.CHANNEL_MUTE:
83 case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME:
85 case DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB:
87 case DenonMarantzBindingConstants.CHANNEL_INPUT:
89 case DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM:
90 return surroundProgram;
92 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST:
94 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM:
96 case DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK:
99 case DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER:
101 case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME:
103 case DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB:
104 return zone2VolumeDB;
105 case DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE:
107 case DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT:
110 case DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER:
112 case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME:
114 case DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB:
115 return zone3VolumeDB;
116 case DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE:
118 case DenonMarantzBindingConstants.CHANNEL_ZONE3_INPUT:
121 case DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER:
123 case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME:
125 case DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB:
126 return zone4VolumeDB;
127 case DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE:
129 case DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT:
137 public void setPower(boolean power) {
138 OnOffType newVal = OnOffType.from(power);
139 if (newVal != this.power) {
141 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_POWER, newVal);
145 public void setMainZonePower(boolean mainPower) {
146 OnOffType newVal = OnOffType.from(mainPower);
147 if (newVal != this.mainZonePower) {
148 this.mainZonePower = newVal;
149 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_ZONE_POWER, newVal);
153 public void setMute(boolean mute) {
154 OnOffType newVal = OnOffType.from(mute);
155 if (newVal != this.mute) {
157 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MUTE, newVal);
161 public void setMainVolume(BigDecimal volume) {
162 PercentType newVal = new PercentType(volume);
163 if (!newVal.equals(this.mainVolume)) {
164 this.mainVolume = newVal;
165 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME, newVal);
166 // update the main volume in dB too
167 State mainVolumeDB = this.mainVolumeDB = DecimalType
168 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
169 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_MAIN_VOLUME_DB, mainVolumeDB);
173 public void setInput(String input) {
174 StringType newVal = StringType.valueOf(input);
175 if (!newVal.equals(this.input)) {
177 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_INPUT, newVal);
181 public void setSurroundProgram(String surroundProgram) {
182 StringType newVal = StringType.valueOf(surroundProgram);
183 if (!newVal.equals(this.surroundProgram)) {
184 this.surroundProgram = newVal;
185 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_SURROUND_PROGRAM, newVal);
189 public void setNowPlayingArtist(String artist) {
190 StringType newVal = artist.isBlank() ? StringType.EMPTY : StringType.valueOf(artist);
191 if (!newVal.equals(this.artist)) {
192 this.artist = newVal;
193 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ARTIST, newVal);
197 public void setNowPlayingAlbum(String album) {
198 StringType newVal = album.isBlank() ? StringType.EMPTY : StringType.valueOf(album);
199 if (!newVal.equals(this.album)) {
201 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_ALBUM, newVal);
205 public void setNowPlayingTrack(String track) {
206 StringType newVal = track.isBlank() ? StringType.EMPTY : StringType.valueOf(track);
207 if (!newVal.equals(this.track)) {
209 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_NOW_PLAYING_TRACK, newVal);
213 public void setZone2Power(boolean power) {
214 OnOffType newVal = OnOffType.from(power);
215 if (newVal != this.zone2Power) {
216 this.zone2Power = newVal;
217 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_POWER, newVal);
221 public void setZone2Volume(BigDecimal volume) {
222 PercentType newVal = new PercentType(volume);
223 if (!newVal.equals(this.zone2Volume)) {
224 this.zone2Volume = newVal;
225 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME, newVal);
226 // update the volume in dB too
227 State zone2VolumeDB = this.zone2VolumeDB = DecimalType
228 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
229 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_VOLUME_DB, zone2VolumeDB);
233 public void setZone2Mute(boolean mute) {
234 OnOffType newVal = OnOffType.from(mute);
235 if (newVal != this.zone2Mute) {
236 this.zone2Mute = newVal;
237 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_MUTE, newVal);
241 public void setZone2Input(String zone2Input) {
242 StringType newVal = StringType.valueOf(zone2Input);
243 if (!newVal.equals(this.zone2Input)) {
244 this.zone2Input = newVal;
245 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, newVal);
249 public void setZone3Power(boolean power) {
250 OnOffType newVal = OnOffType.from(power);
251 if (newVal != this.zone3Power) {
252 this.zone3Power = newVal;
253 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_POWER, newVal);
257 public void setZone3Volume(BigDecimal volume) {
258 PercentType newVal = new PercentType(volume);
259 if (!newVal.equals(this.zone3Volume)) {
260 this.zone3Volume = newVal;
261 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME, newVal);
262 // update the volume in dB too
263 State zone3VolumeDB = this.zone3VolumeDB = DecimalType
264 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
265 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_VOLUME_DB, zone3VolumeDB);
269 public void setZone3Mute(boolean mute) {
270 OnOffType newVal = OnOffType.from(mute);
271 if (newVal != this.zone3Mute) {
272 this.zone3Mute = newVal;
273 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE3_MUTE, newVal);
277 public void setZone3Input(String zone3Input) {
278 StringType newVal = StringType.valueOf(zone3Input);
279 if (!newVal.equals(this.zone3Input)) {
280 this.zone3Input = newVal;
281 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE2_INPUT, newVal);
285 public void setZone4Power(boolean power) {
286 OnOffType newVal = OnOffType.from(power);
287 if (newVal != this.zone4Power) {
288 this.zone4Power = newVal;
289 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_POWER, newVal);
293 public void setZone4Volume(BigDecimal volume) {
294 PercentType newVal = new PercentType(volume);
295 if (!newVal.equals(this.zone4Volume)) {
296 this.zone4Volume = newVal;
297 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME, newVal);
298 // update the volume in dB too
299 State zone4VolumeDB = this.zone4VolumeDB = DecimalType
300 .valueOf(volume.subtract(DenonMarantzBindingConstants.DB_OFFSET).toString());
301 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_VOLUME_DB, zone4VolumeDB);
305 public void setZone4Mute(boolean mute) {
306 OnOffType newVal = OnOffType.from(mute);
307 if (newVal != this.zone4Mute) {
308 this.zone4Mute = newVal;
309 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_MUTE, newVal);
313 public void setZone4Input(String zone4Input) {
314 StringType newVal = StringType.valueOf(zone4Input);
315 if (!newVal.equals(this.zone4Input)) {
316 this.zone4Input = newVal;
317 handler.stateChanged(DenonMarantzBindingConstants.CHANNEL_ZONE4_INPUT, newVal);