]> git.basschouten.com Git - openhab-addons.git/blob
698f2a89de3ff73257e69f4758cfbe2a233925e4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.avmfritz.internal.handler;
14
15 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
16 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.*;
17
18 import java.math.BigDecimal;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
22 import java.util.Map;
23
24 import javax.measure.quantity.Temperature;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.avmfritz.internal.config.AVMFritzDeviceConfiguration;
29 import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
30 import org.openhab.binding.avmfritz.internal.dto.AlertModel;
31 import org.openhab.binding.avmfritz.internal.dto.BatteryModel;
32 import org.openhab.binding.avmfritz.internal.dto.ColorControlModel;
33 import org.openhab.binding.avmfritz.internal.dto.DeviceModel;
34 import org.openhab.binding.avmfritz.internal.dto.HeatingModel;
35 import org.openhab.binding.avmfritz.internal.dto.HeatingModel.NextChangeModel;
36 import org.openhab.binding.avmfritz.internal.dto.HumidityModel;
37 import org.openhab.binding.avmfritz.internal.dto.LevelControlModel;
38 import org.openhab.binding.avmfritz.internal.dto.PowerMeterModel;
39 import org.openhab.binding.avmfritz.internal.dto.SimpleOnOffModel;
40 import org.openhab.binding.avmfritz.internal.dto.SwitchModel;
41 import org.openhab.binding.avmfritz.internal.dto.TemperatureModel;
42 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaStatusListener;
43 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
44 import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetBlindTargetCallback.BlindCommand;
45 import org.openhab.core.config.core.Configuration;
46 import org.openhab.core.library.types.DateTimeType;
47 import org.openhab.core.library.types.DecimalType;
48 import org.openhab.core.library.types.HSBType;
49 import org.openhab.core.library.types.IncreaseDecreaseType;
50 import org.openhab.core.library.types.OnOffType;
51 import org.openhab.core.library.types.OpenClosedType;
52 import org.openhab.core.library.types.PercentType;
53 import org.openhab.core.library.types.QuantityType;
54 import org.openhab.core.library.types.StopMoveType;
55 import org.openhab.core.library.types.StringType;
56 import org.openhab.core.library.types.UpDownType;
57 import org.openhab.core.library.unit.SIUnits;
58 import org.openhab.core.library.unit.Units;
59 import org.openhab.core.thing.Bridge;
60 import org.openhab.core.thing.Channel;
61 import org.openhab.core.thing.ChannelUID;
62 import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
63 import org.openhab.core.thing.Thing;
64 import org.openhab.core.thing.ThingStatus;
65 import org.openhab.core.thing.ThingStatusDetail;
66 import org.openhab.core.thing.ThingUID;
67 import org.openhab.core.thing.binding.BaseThingHandler;
68 import org.openhab.core.thing.binding.BridgeHandler;
69 import org.openhab.core.thing.binding.ThingHandlerCallback;
70 import org.openhab.core.thing.type.ChannelTypeUID;
71 import org.openhab.core.types.Command;
72 import org.openhab.core.types.RefreshType;
73 import org.openhab.core.types.State;
74 import org.openhab.core.types.UnDefType;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78 /**
79  * Abstract handler for a FRITZ! thing. Handles commands, which are sent to one of the channels.
80  *
81  * @author Robert Bausdorf - Initial contribution
82  * @author Christoph Weitkamp - Added support for AVM FRITZ!DECT 300 and Comet DECT
83  * @author Christoph Weitkamp - Added support for groups
84  * @author Ulrich Mertin - Added support for HAN-FUN blinds
85  */
86 @NonNullByDefault
87 public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implements FritzAhaStatusListener {
88
89     private final Logger logger = LoggerFactory.getLogger(AVMFritzBaseThingHandler.class);
90
91     /**
92      * keeps track of the current state for handling of increase/decrease
93      */
94     private AVMFritzBaseModel currentDevice = new DeviceModel();
95     private @Nullable String identifier;
96
97     /**
98      * Constructor
99      *
100      * @param thing Thing object representing a FRITZ! device
101      */
102     public AVMFritzBaseThingHandler(Thing thing) {
103         super(thing);
104     }
105
106     @Override
107     public void initialize() {
108         final AVMFritzDeviceConfiguration config = getConfigAs(AVMFritzDeviceConfiguration.class);
109         final String newIdentifier = config.ain;
110         if (newIdentifier == null || newIdentifier.isBlank()) {
111             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
112                     "The 'ain' parameter must be configured.");
113         } else {
114             this.identifier = newIdentifier;
115             updateStatus(ThingStatus.UNKNOWN);
116         }
117     }
118
119     @Override
120     public void onDeviceAdded(AVMFritzBaseModel device) {
121         // nothing to do
122     }
123
124     @Override
125     public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
126         if (thing.getUID().equals(thingUID)) {
127             logger.debug("Update thing '{}' with device model: {}", thingUID, device);
128             if (device.getPresent() == 1) {
129                 updateStatus(ThingStatus.ONLINE);
130             } else {
131                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Device not present");
132             }
133             currentDevice = device;
134
135             updateProperties(device, editProperties());
136
137             if (device.isPowermeter()) {
138                 updatePowermeter(device.getPowermeter());
139             }
140             if (device.isSwitchableOutlet()) {
141                 updateSwitchableOutlet(device.getSwitch());
142             }
143             if (device.isHeatingThermostat()) {
144                 updateHeatingThermostat(device.getHkr());
145             }
146             if (device instanceof DeviceModel) {
147                 DeviceModel deviceModel = (DeviceModel) device;
148                 if (deviceModel.isTemperatureSensor()) {
149                     updateTemperatureSensor(deviceModel.getTemperature());
150                 }
151                 if (deviceModel.isHumiditySensor()) {
152                     updateHumiditySensor(deviceModel.getHumidity());
153                 }
154                 if (deviceModel.isHANFUNAlarmSensor()) {
155                     updateHANFUNAlarmSensor(deviceModel.getAlert());
156                 }
157                 if (deviceModel.isHANFUNBlinds()) {
158                     updateLevelControl(deviceModel.getLevelControlModel());
159                 } else if (deviceModel.isColorLight()) {
160                     updateColorLight(deviceModel.getColorControlModel(), deviceModel.getLevelControlModel());
161                 } else if (deviceModel.isDimmableLight()) {
162                     updateDimmableLight(deviceModel.getLevelControlModel());
163                 } else if (deviceModel.isHANFUNUnit() && deviceModel.isHANFUNOnOff()) {
164                     updateSimpleOnOffUnit(deviceModel.getSimpleOnOffUnit());
165                 }
166             }
167         }
168     }
169
170     private void updateHANFUNAlarmSensor(@Nullable AlertModel alertModel) {
171         if (alertModel != null) {
172             updateThingChannelState(CHANNEL_CONTACT_STATE,
173                     AlertModel.ON.equals(alertModel.getState()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
174         }
175     }
176
177     protected void updateTemperatureSensor(@Nullable TemperatureModel temperatureModel) {
178         if (temperatureModel != null) {
179             updateThingChannelState(CHANNEL_TEMPERATURE,
180                     new QuantityType<>(temperatureModel.getCelsius(), SIUnits.CELSIUS));
181             updateThingChannelConfiguration(CHANNEL_TEMPERATURE, CONFIG_CHANNEL_TEMP_OFFSET,
182                     temperatureModel.getOffset());
183         }
184     }
185
186     protected void updateHumiditySensor(@Nullable HumidityModel humidityModel) {
187         if (humidityModel != null) {
188             updateThingChannelState(CHANNEL_HUMIDITY,
189                     new QuantityType<>(humidityModel.getRelativeHumidity(), Units.PERCENT));
190         }
191     }
192
193     protected void updateLevelControl(@Nullable LevelControlModel levelControlModel) {
194         if (levelControlModel != null) {
195             updateThingChannelState(CHANNEL_ROLLERSHUTTER, new PercentType(levelControlModel.getLevelPercentage()));
196         }
197     }
198
199     private void updateDimmableLight(@Nullable LevelControlModel levelControlModel) {
200         if (levelControlModel != null) {
201             updateThingChannelState(CHANNEL_BRIGHTNESS, new PercentType(levelControlModel.getLevelPercentage()));
202         }
203     }
204
205     private void updateColorLight(@Nullable ColorControlModel colorControlModel,
206             @Nullable LevelControlModel levelControlModel) {
207         if (colorControlModel != null && levelControlModel != null) {
208             DecimalType hue = new DecimalType(colorControlModel.hue);
209             PercentType saturation = ColorControlModel.toPercent(colorControlModel.saturation);
210             PercentType brightness = new PercentType(levelControlModel.getLevelPercentage());
211             updateThingChannelState(CHANNEL_COLOR, new HSBType(hue, saturation, brightness));
212         }
213     }
214
215     private void updateHeatingThermostat(@Nullable HeatingModel heatingModel) {
216         if (heatingModel != null) {
217             updateThingChannelState(CHANNEL_MODE, new StringType(heatingModel.getMode()));
218             updateThingChannelState(CHANNEL_LOCKED,
219                     BigDecimal.ZERO.equals(heatingModel.getLock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
220             updateThingChannelState(CHANNEL_DEVICE_LOCKED,
221                     BigDecimal.ZERO.equals(heatingModel.getDevicelock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
222             updateThingChannelState(CHANNEL_ACTUALTEMP,
223                     new QuantityType<>(toCelsius(heatingModel.getTist()), SIUnits.CELSIUS));
224             updateThingChannelState(CHANNEL_SETTEMP,
225                     new QuantityType<>(toCelsius(heatingModel.getTsoll()), SIUnits.CELSIUS));
226             updateThingChannelState(CHANNEL_ECOTEMP,
227                     new QuantityType<>(toCelsius(heatingModel.getAbsenk()), SIUnits.CELSIUS));
228             updateThingChannelState(CHANNEL_COMFORTTEMP,
229                     new QuantityType<>(toCelsius(heatingModel.getKomfort()), SIUnits.CELSIUS));
230             updateThingChannelState(CHANNEL_RADIATOR_MODE, new StringType(heatingModel.getRadiatorMode()));
231             NextChangeModel nextChange = heatingModel.getNextchange();
232             if (nextChange != null) {
233                 int endPeriod = nextChange.getEndperiod();
234                 updateThingChannelState(CHANNEL_NEXT_CHANGE, endPeriod == 0 ? UnDefType.UNDEF
235                         : new DateTimeType(
236                                 ZonedDateTime.ofInstant(Instant.ofEpochSecond(endPeriod), ZoneId.systemDefault())));
237                 BigDecimal nextTemperature = nextChange.getTchange();
238                 updateThingChannelState(CHANNEL_NEXTTEMP, TEMP_FRITZ_UNDEFINED.equals(nextTemperature) ? UnDefType.UNDEF
239                         : new QuantityType<>(toCelsius(nextTemperature), SIUnits.CELSIUS));
240             }
241             updateBattery(heatingModel);
242         }
243     }
244
245     protected void updateBattery(BatteryModel batteryModel) {
246         BigDecimal batteryLevel = batteryModel.getBattery();
247         updateThingChannelState(CHANNEL_BATTERY,
248                 batteryLevel == null ? UnDefType.UNDEF : new DecimalType(batteryLevel));
249         BigDecimal lowBattery = batteryModel.getBatterylow();
250         if (lowBattery == null) {
251             updateThingChannelState(CHANNEL_BATTERY_LOW, UnDefType.UNDEF);
252         } else {
253             updateThingChannelState(CHANNEL_BATTERY_LOW, OnOffType.from(BatteryModel.BATTERY_ON.equals(lowBattery)));
254         }
255     }
256
257     private void updateSimpleOnOffUnit(@Nullable SimpleOnOffModel simpleOnOffUnit) {
258         if (simpleOnOffUnit != null) {
259             updateThingChannelState(CHANNEL_ON_OFF, OnOffType.from(simpleOnOffUnit.state));
260         }
261     }
262
263     private void updateSwitchableOutlet(@Nullable SwitchModel switchModel) {
264         if (switchModel != null) {
265             updateThingChannelState(CHANNEL_MODE, new StringType(switchModel.getMode()));
266             updateThingChannelState(CHANNEL_LOCKED,
267                     BigDecimal.ZERO.equals(switchModel.getLock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
268             updateThingChannelState(CHANNEL_DEVICE_LOCKED,
269                     BigDecimal.ZERO.equals(switchModel.getDevicelock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
270             BigDecimal state = switchModel.getState();
271             if (state == null) {
272                 updateThingChannelState(CHANNEL_OUTLET, UnDefType.UNDEF);
273             } else {
274                 updateThingChannelState(CHANNEL_OUTLET, OnOffType.from(SwitchModel.ON.equals(state)));
275             }
276         }
277     }
278
279     private void updatePowermeter(@Nullable PowerMeterModel powerMeterModel) {
280         if (powerMeterModel != null) {
281             updateThingChannelState(CHANNEL_ENERGY, new QuantityType<>(powerMeterModel.getEnergy(), Units.WATT_HOUR));
282             updateThingChannelState(CHANNEL_POWER, new QuantityType<>(powerMeterModel.getPower(), Units.WATT));
283             updateThingChannelState(CHANNEL_VOLTAGE, new QuantityType<>(powerMeterModel.getVoltage(), Units.VOLT));
284         }
285     }
286
287     /**
288      * Updates thing properties.
289      *
290      * @param device the {@link AVMFritzBaseModel}
291      * @param editProperties map of existing properties
292      */
293     protected void updateProperties(AVMFritzBaseModel device, Map<String, String> editProperties) {
294         editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, device.getFirmwareVersion());
295         updateProperties(editProperties);
296     }
297
298     /**
299      * Updates thing channels and creates dynamic channels if missing.
300      *
301      * @param channelId ID of the channel to be updated.
302      * @param state State to be set.
303      */
304     protected void updateThingChannelState(String channelId, State state) {
305         Channel channel = thing.getChannel(channelId);
306         if (channel != null) {
307             updateState(channel.getUID(), state);
308         } else {
309             logger.debug("Channel '{}' in thing '{}' does not exist, recreating thing.", channelId, thing.getUID());
310             createChannel(channelId);
311         }
312     }
313
314     /**
315      * Creates a {@link ChannelTypeUID} from the given channel id.
316      *
317      * @param channelId ID of the channel type UID to be created.
318      * @return the channel type UID
319      */
320     private ChannelTypeUID createChannelTypeUID(String channelId) {
321         int pos = channelId.indexOf(ChannelUID.CHANNEL_GROUP_SEPARATOR);
322         String id = pos > -1 ? channelId.substring(pos + 1) : channelId;
323         return CHANNEL_BATTERY.equals(id) ? DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_BATTERY_LEVEL.getUID()
324                 : new ChannelTypeUID(BINDING_ID, id);
325     }
326
327     /**
328      * Creates new channels for the thing.
329      *
330      * @param channelId ID of the channel to be created.
331      */
332     private void createChannel(String channelId) {
333         ThingHandlerCallback callback = getCallback();
334         if (callback != null) {
335             ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
336             ChannelTypeUID channelTypeUID = createChannelTypeUID(channelId);
337             Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).build();
338             updateThing(editThing().withoutChannel(channelUID).withChannel(channel).build());
339         }
340     }
341
342     /**
343      * Updates thing channel configurations.
344      *
345      * @param channelId ID of the channel which configuration to be updated.
346      * @param configId ID of the configuration to be updated.
347      * @param value Value to be set.
348      */
349     protected void updateThingChannelConfiguration(String channelId, String configId, Object value) {
350         Channel channel = thing.getChannel(channelId);
351         if (channel != null) {
352             Configuration editConfig = channel.getConfiguration();
353             editConfig.put(configId, value);
354         }
355     }
356
357     @Override
358     public void onDeviceGone(ThingUID thingUID) {
359         if (thing.getUID().equals(thingUID)) {
360             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Device not present in response");
361         }
362     }
363
364     @Override
365     public void handleCommand(ChannelUID channelUID, Command command) {
366         String channelId = channelUID.getIdWithoutGroup();
367         logger.debug("Handle command '{}' for channel {}", command, channelId);
368         if (command == RefreshType.REFRESH) {
369             handleRefreshCommand();
370             return;
371         }
372         FritzAhaWebInterface fritzBox = getWebInterface();
373         if (fritzBox == null) {
374             logger.debug("Cannot handle command '{}' because connection is missing", command);
375             return;
376         }
377         String ain = getIdentifier();
378         if (ain == null) {
379             logger.debug("Cannot handle command '{}' because AIN is missing", command);
380             return;
381         }
382         switch (channelId) {
383             case CHANNEL_MODE:
384             case CHANNEL_LOCKED:
385             case CHANNEL_DEVICE_LOCKED:
386             case CHANNEL_TEMPERATURE:
387             case CHANNEL_HUMIDITY:
388             case CHANNEL_ENERGY:
389             case CHANNEL_POWER:
390             case CHANNEL_VOLTAGE:
391             case CHANNEL_ACTUALTEMP:
392             case CHANNEL_ECOTEMP:
393             case CHANNEL_COMFORTTEMP:
394             case CHANNEL_NEXT_CHANGE:
395             case CHANNEL_NEXTTEMP:
396             case CHANNEL_BATTERY:
397             case CHANNEL_BATTERY_LOW:
398             case CHANNEL_CONTACT_STATE:
399             case CHANNEL_LAST_CHANGE:
400                 logger.debug("Channel {} is a read-only channel and cannot handle command '{}'", channelId, command);
401                 break;
402             case CHANNEL_OUTLET:
403             case CHANNEL_ON_OFF:
404                 if (command instanceof OnOffType) {
405                     fritzBox.setSwitch(ain, OnOffType.ON.equals(command));
406                 }
407                 break;
408             case CHANNEL_COLOR:
409             case CHANNEL_BRIGHTNESS:
410                 BigDecimal brightness = null;
411                 if (command instanceof HSBType) {
412                     HSBType hsbType = (HSBType) command;
413                     brightness = hsbType.getBrightness().toBigDecimal();
414                     fritzBox.setHueAndSaturation(ain, hsbType.getHue().intValue(),
415                             ColorControlModel.fromPercent(hsbType.getSaturation()), 0);
416                 } else if (command instanceof PercentType) {
417                     brightness = ((PercentType) command).toBigDecimal();
418                 } else if (command instanceof OnOffType) {
419                     fritzBox.setSwitch(ain, OnOffType.ON.equals(command));
420                 }
421                 if (brightness != null) {
422                     fritzBox.setLevelPercentage(ain, brightness);
423                 }
424                 break;
425             case CHANNEL_SETTEMP:
426                 BigDecimal temperature = null;
427                 if (command instanceof DecimalType) {
428                     temperature = normalizeCelsius(((DecimalType) command).toBigDecimal());
429                 } else if (command instanceof QuantityType) {
430                     @SuppressWarnings("unchecked")
431                     QuantityType<Temperature> convertedCommand = ((QuantityType<Temperature>) command)
432                             .toUnit(SIUnits.CELSIUS);
433                     if (convertedCommand != null) {
434                         temperature = normalizeCelsius(convertedCommand.toBigDecimal());
435                     } else {
436                         logger.warn("Unable to convert unit from '{}' to '{}'. Skipping command.",
437                                 ((QuantityType<?>) command).getUnit(), SIUnits.CELSIUS);
438                     }
439                 } else if (command instanceof IncreaseDecreaseType) {
440                     temperature = currentDevice.getHkr().getTsoll();
441                     if (IncreaseDecreaseType.INCREASE.equals(command)) {
442                         temperature.add(BigDecimal.ONE);
443                     } else {
444                         temperature.subtract(BigDecimal.ONE);
445                     }
446                 } else if (command instanceof OnOffType) {
447                     temperature = OnOffType.ON.equals(command) ? TEMP_FRITZ_ON : TEMP_FRITZ_OFF;
448                 }
449                 if (temperature != null) {
450                     fritzBox.setSetTemp(ain, fromCelsius(temperature));
451                     HeatingModel heatingModel = currentDevice.getHkr();
452                     heatingModel.setTsoll(temperature);
453                     updateState(CHANNEL_RADIATOR_MODE, new StringType(heatingModel.getRadiatorMode()));
454                 }
455                 break;
456             case CHANNEL_RADIATOR_MODE:
457                 BigDecimal targetTemperature = null;
458                 if (command instanceof StringType) {
459                     switch (command.toString()) {
460                         case MODE_ON:
461                             targetTemperature = TEMP_FRITZ_ON;
462                             break;
463                         case MODE_OFF:
464                             targetTemperature = TEMP_FRITZ_OFF;
465                             break;
466                         case MODE_COMFORT:
467                             targetTemperature = currentDevice.getHkr().getKomfort();
468                             break;
469                         case MODE_ECO:
470                             targetTemperature = currentDevice.getHkr().getAbsenk();
471                             break;
472                         case MODE_BOOST:
473                             targetTemperature = TEMP_FRITZ_MAX;
474                             break;
475                         case MODE_UNKNOWN:
476                         case MODE_WINDOW_OPEN:
477                             logger.debug("Command '{}' is a read-only command for channel {}.", command, channelId);
478                             break;
479                     }
480                     if (targetTemperature != null) {
481                         fritzBox.setSetTemp(ain, targetTemperature);
482                         currentDevice.getHkr().setTsoll(targetTemperature);
483                         updateState(CHANNEL_SETTEMP, new QuantityType<>(toCelsius(targetTemperature), SIUnits.CELSIUS));
484                     }
485                 }
486                 break;
487             case CHANNEL_ROLLERSHUTTER:
488                 if (command instanceof StopMoveType) {
489                     StopMoveType rollershutterCommand = (StopMoveType) command;
490                     if (StopMoveType.STOP.equals(rollershutterCommand)) {
491                         fritzBox.setBlind(ain, BlindCommand.STOP);
492                     } else {
493                         logger.debug("Received unknown rollershutter StopMove command MOVE");
494                     }
495                 } else if (command instanceof UpDownType) {
496                     UpDownType rollershutterCommand = (UpDownType) command;
497                     if (UpDownType.UP.equals(rollershutterCommand)) {
498                         fritzBox.setBlind(ain, BlindCommand.OPEN);
499                     } else {
500                         fritzBox.setBlind(ain, BlindCommand.CLOSE);
501                     }
502                 } else if (command instanceof PercentType) {
503                     BigDecimal levelPercentage = ((PercentType) command).toBigDecimal();
504                     fritzBox.setLevelPercentage(ain, levelPercentage);
505                 } else {
506                     logger.debug("Received unknown rollershutter command type '{}'", command.toString());
507                 }
508                 break;
509             default:
510                 logger.debug("Received unknown channel {}", channelId);
511                 break;
512         }
513     }
514
515     /**
516      * Handles a command for a given action.
517      *
518      * @param action
519      * @param duration
520      */
521     protected void handleAction(String action, long duration) {
522         FritzAhaWebInterface fritzBox = getWebInterface();
523         if (fritzBox == null) {
524             logger.debug("Cannot handle action '{}' because connection is missing", action);
525             return;
526         }
527         String ain = getIdentifier();
528         if (ain == null) {
529             logger.debug("Cannot handle action '{}' because AIN is missing", action);
530             return;
531         }
532         if (duration < 0 || 86400 < duration) {
533             throw new IllegalArgumentException("Duration must not be less than zero or greater than 86400");
534         }
535         switch (action) {
536             case MODE_BOOST:
537                 fritzBox.setBoostMode(ain,
538                         duration > 0 ? ZonedDateTime.now().plusSeconds(duration).toEpochSecond() : 0);
539                 break;
540             case MODE_WINDOW_OPEN:
541                 fritzBox.setWindowOpenMode(ain,
542                         duration > 0 ? ZonedDateTime.now().plusSeconds(duration).toEpochSecond() : 0);
543                 break;
544             default:
545                 logger.debug("Received unknown action '{}'", action);
546                 break;
547         }
548     }
549
550     /**
551      * Provides the web interface object.
552      *
553      * @return The web interface object
554      */
555     private @Nullable FritzAhaWebInterface getWebInterface() {
556         Bridge bridge = getBridge();
557         if (bridge != null) {
558             BridgeHandler handler = bridge.getHandler();
559             if (handler instanceof AVMFritzBaseBridgeHandler) {
560                 return ((AVMFritzBaseBridgeHandler) handler).getWebInterface();
561             }
562         }
563         return null;
564     }
565
566     /**
567      * Handles a refresh command.
568      */
569     private void handleRefreshCommand() {
570         Bridge bridge = getBridge();
571         if (bridge != null) {
572             BridgeHandler handler = bridge.getHandler();
573             if (handler instanceof AVMFritzBaseBridgeHandler) {
574                 ((AVMFritzBaseBridgeHandler) handler).handleRefreshCommand();
575             }
576         }
577     }
578
579     /**
580      * Returns the AIN.
581      *
582      * @return the AIN
583      */
584     public @Nullable String getIdentifier() {
585         return identifier;
586     }
587 }