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.avmfritz.internal.handler;
15 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.BINDING_ID;
16 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ACTUALTEMP;
17 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_BATTERY;
18 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_BATTERY_LOW;
19 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_BRIGHTNESS;
20 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COLOR;
21 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COLORTEMPERATURE;
22 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COLORTEMPERATURE_ABS;
23 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_COMFORTTEMP;
24 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_CONTACT_STATE;
25 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_DEVICE_LOCKED;
26 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ECOTEMP;
27 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ENERGY;
28 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_HUMIDITY;
29 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_LAST_CHANGE;
30 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_LOCKED;
31 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_MODE;
32 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_NEXTTEMP;
33 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_NEXT_CHANGE;
34 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_OBSTRUCTION_ALARM;
35 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ON_OFF;
36 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_OUTLET;
37 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_POWER;
38 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_RADIATOR_MODE;
39 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_ROLLERSHUTTER;
40 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_SETTEMP;
41 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_TEMPERATURE;
42 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_TEMPERATURE_ALARM;
43 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CHANNEL_VOLTAGE;
44 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.CONFIG_CHANNEL_TEMP_OFFSET;
45 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_BOOST;
46 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_COMFORT;
47 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_ECO;
48 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_OFF;
49 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_ON;
50 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_UNKNOWN;
51 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.MODE_WINDOW_OPEN;
52 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_MAX;
53 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_OFF;
54 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_ON;
55 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.TEMP_FRITZ_UNDEFINED;
56 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.fromCelsius;
57 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.normalizeCelsius;
58 import static org.openhab.binding.avmfritz.internal.dto.HeatingModel.toCelsius;
60 import java.math.BigDecimal;
61 import java.time.Instant;
62 import java.time.ZoneId;
63 import java.time.ZonedDateTime;
66 import javax.measure.quantity.Temperature;
68 import org.eclipse.jdt.annotation.NonNullByDefault;
69 import org.eclipse.jdt.annotation.Nullable;
70 import org.openhab.binding.avmfritz.internal.config.AVMFritzDeviceConfiguration;
71 import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
72 import org.openhab.binding.avmfritz.internal.dto.AlertModel;
73 import org.openhab.binding.avmfritz.internal.dto.BatteryModel;
74 import org.openhab.binding.avmfritz.internal.dto.ColorControlModel;
75 import org.openhab.binding.avmfritz.internal.dto.DeviceModel;
76 import org.openhab.binding.avmfritz.internal.dto.HeatingModel;
77 import org.openhab.binding.avmfritz.internal.dto.HeatingModel.NextChangeModel;
78 import org.openhab.binding.avmfritz.internal.dto.HumidityModel;
79 import org.openhab.binding.avmfritz.internal.dto.LevelControlModel;
80 import org.openhab.binding.avmfritz.internal.dto.PowerMeterModel;
81 import org.openhab.binding.avmfritz.internal.dto.SimpleOnOffModel;
82 import org.openhab.binding.avmfritz.internal.dto.SwitchModel;
83 import org.openhab.binding.avmfritz.internal.dto.TemperatureModel;
84 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaStatusListener;
85 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
86 import org.openhab.binding.avmfritz.internal.hardware.callbacks.FritzAhaSetBlindTargetCallback.BlindCommand;
87 import org.openhab.core.config.core.Configuration;
88 import org.openhab.core.library.types.DateTimeType;
89 import org.openhab.core.library.types.DecimalType;
90 import org.openhab.core.library.types.HSBType;
91 import org.openhab.core.library.types.IncreaseDecreaseType;
92 import org.openhab.core.library.types.OnOffType;
93 import org.openhab.core.library.types.OpenClosedType;
94 import org.openhab.core.library.types.PercentType;
95 import org.openhab.core.library.types.QuantityType;
96 import org.openhab.core.library.types.StopMoveType;
97 import org.openhab.core.library.types.StringType;
98 import org.openhab.core.library.types.UpDownType;
99 import org.openhab.core.library.unit.SIUnits;
100 import org.openhab.core.library.unit.Units;
101 import org.openhab.core.thing.Bridge;
102 import org.openhab.core.thing.Channel;
103 import org.openhab.core.thing.ChannelUID;
104 import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
105 import org.openhab.core.thing.Thing;
106 import org.openhab.core.thing.ThingStatus;
107 import org.openhab.core.thing.ThingStatusDetail;
108 import org.openhab.core.thing.ThingUID;
109 import org.openhab.core.thing.binding.BaseThingHandler;
110 import org.openhab.core.thing.binding.BridgeHandler;
111 import org.openhab.core.thing.binding.ThingHandlerCallback;
112 import org.openhab.core.thing.type.ChannelTypeUID;
113 import org.openhab.core.types.Command;
114 import org.openhab.core.types.RefreshType;
115 import org.openhab.core.types.State;
116 import org.openhab.core.types.UnDefType;
117 import org.slf4j.Logger;
118 import org.slf4j.LoggerFactory;
121 * Abstract handler for a FRITZ! thing. Handles commands, which are sent to one of the channels.
123 * @author Robert Bausdorf - Initial contribution
124 * @author Christoph Weitkamp - Added support for AVM FRITZ!DECT 300 and Comet DECT
125 * @author Christoph Weitkamp - Added support for groups
126 * @author Ulrich Mertin - Added support for HAN-FUN blinds
127 * @author Christoph Sommer - Added support for color temperature
128 * @author Tobias Lange - Added abs color temperature and fixed on/off behavior of light blub
131 public abstract class AVMFritzBaseThingHandler extends BaseThingHandler implements FritzAhaStatusListener {
133 private final Logger logger = LoggerFactory.getLogger(AVMFritzBaseThingHandler.class);
136 * keeps track of the current state for handling of increase/decrease
138 private AVMFritzBaseModel currentDevice = new DeviceModel();
139 private @Nullable String identifier;
144 * @param thing Thing object representing a FRITZ! device
146 public AVMFritzBaseThingHandler(Thing thing) {
151 public void initialize() {
152 final AVMFritzDeviceConfiguration config = getConfigAs(AVMFritzDeviceConfiguration.class);
153 final String newIdentifier = config.ain;
154 if (newIdentifier == null || newIdentifier.isBlank()) {
155 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
156 "The 'ain' parameter must be configured.");
158 this.identifier = newIdentifier;
159 updateStatus(ThingStatus.UNKNOWN);
164 public void onDeviceAdded(AVMFritzBaseModel device) {
169 public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
170 if (thing.getUID().equals(thingUID)) {
171 logger.debug("Update thing '{}' with device model: {}", thingUID, device);
172 if (device.getPresent() == 1) {
173 updateStatus(ThingStatus.ONLINE);
175 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Device not present");
177 currentDevice = device;
179 updateProperties(device, editProperties());
181 if (device.isPowermeter()) {
182 updatePowermeter(device.getPowermeter());
184 if (device.isSwitchableOutlet()) {
185 updateSwitchableOutlet(device.getSwitch());
187 if (device.isHeatingThermostat()) {
188 updateHeatingThermostat(device.getHkr());
190 if (device instanceof DeviceModel deviceModel) {
191 if (deviceModel.isTemperatureSensor()) {
192 updateTemperatureSensor(deviceModel.getTemperature());
194 if (deviceModel.isHumiditySensor()) {
195 updateHumiditySensor(deviceModel.getHumidity());
197 if (deviceModel.isHANFUNAlarmSensor()) {
198 if (deviceModel.isHANFUNBlinds()) {
199 updateHANFUNBlindsAlarmSensor(deviceModel.getAlert());
201 updateHANFUNAlarmSensor(deviceModel.getAlert());
204 if (deviceModel.isHANFUNBlinds()) {
205 updateLevelControl(deviceModel.getLevelControlModel());
206 } else if (deviceModel.isColorLight()) {
207 updateColorLight(deviceModel.getColorControlModel(), deviceModel.getLevelControlModel(),
208 deviceModel.getSimpleOnOffUnit());
209 } else if (deviceModel.isDimmableLight() && !deviceModel.isHANFUNBlinds()) {
210 updateDimmableLight(deviceModel.getLevelControlModel());
211 } else if (deviceModel.isHANFUNUnit() && deviceModel.isHANFUNOnOff()) {
212 updateSimpleOnOffUnit(deviceModel.getSimpleOnOffUnit());
218 private void updateHANFUNAlarmSensor(@Nullable AlertModel alertModel) {
219 if (alertModel != null) {
220 updateThingChannelState(CHANNEL_CONTACT_STATE,
221 AlertModel.ON.equals(alertModel.getState()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
225 private void updateHANFUNBlindsAlarmSensor(@Nullable AlertModel alertModel) {
226 if (alertModel != null) {
227 updateThingChannelState(CHANNEL_OBSTRUCTION_ALARM,
228 OnOffType.from(alertModel.hasObstructionAlarmOccurred()));
229 updateThingChannelState(CHANNEL_TEMPERATURE_ALARM, OnOffType.from(alertModel.hasTemperaturAlarmOccurred()));
230 if (alertModel.hasUnknownAlarmOccurred()) {
231 logger.warn("Unknown blinds alarm {}", alertModel.getState());
236 protected void updateTemperatureSensor(@Nullable TemperatureModel temperatureModel) {
237 if (temperatureModel != null) {
238 updateThingChannelState(CHANNEL_TEMPERATURE,
239 new QuantityType<>(temperatureModel.getCelsius(), SIUnits.CELSIUS));
240 updateThingChannelConfiguration(CHANNEL_TEMPERATURE, CONFIG_CHANNEL_TEMP_OFFSET,
241 temperatureModel.getOffset());
245 protected void updateHumiditySensor(@Nullable HumidityModel humidityModel) {
246 if (humidityModel != null) {
247 updateThingChannelState(CHANNEL_HUMIDITY,
248 new QuantityType<>(humidityModel.getRelativeHumidity(), Units.PERCENT));
252 protected void updateLevelControl(@Nullable LevelControlModel levelControlModel) {
253 if (levelControlModel != null) {
254 updateThingChannelState(CHANNEL_ROLLERSHUTTER, new PercentType(levelControlModel.getLevelPercentage()));
258 private void updateDimmableLight(@Nullable LevelControlModel levelControlModel) {
259 if (levelControlModel != null) {
260 updateThingChannelState(CHANNEL_BRIGHTNESS, new PercentType(levelControlModel.getLevelPercentage()));
264 private void updateColorLight(@Nullable ColorControlModel colorControlModel,
265 @Nullable LevelControlModel levelControlModel, @Nullable SimpleOnOffModel simpleOnOff) {
266 if (colorControlModel != null && levelControlModel != null && simpleOnOff != null) {
267 DecimalType hue = new DecimalType(colorControlModel.hue);
268 PercentType saturation = ColorControlModel.toPercent(colorControlModel.saturation);
269 PercentType brightness;
270 if (simpleOnOff.state) {
271 brightness = new PercentType(levelControlModel.getLevelPercentage());
273 brightness = PercentType.ZERO;
275 updateThingChannelState(CHANNEL_COLOR, new HSBType(hue, saturation, brightness));
277 if (colorControlModel.currentMode == 4) {
278 int temperature = colorControlModel.temperature;
279 int tempMired = 1000000 / temperature;
280 int tempMinMired = 1000000 / 2700;
281 int tempMaxMired = 1000000 / 6500;
282 int pct = (tempMired - tempMinMired) * 100 / (tempMaxMired - tempMinMired);
283 updateThingChannelState(CHANNEL_COLORTEMPERATURE, new PercentType(pct));
284 updateThingChannelState(CHANNEL_COLORTEMPERATURE_ABS,
285 new QuantityType<>(BigDecimal.valueOf(temperature), Units.KELVIN));
290 private void updateHeatingThermostat(@Nullable HeatingModel heatingModel) {
291 if (heatingModel != null) {
292 updateThingChannelState(CHANNEL_MODE, new StringType(heatingModel.getMode()));
293 updateThingChannelState(CHANNEL_LOCKED,
294 BigDecimal.ZERO.equals(heatingModel.getLock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
295 updateThingChannelState(CHANNEL_DEVICE_LOCKED,
296 BigDecimal.ZERO.equals(heatingModel.getDevicelock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
297 updateThingChannelState(CHANNEL_ACTUALTEMP,
298 new QuantityType<>(toCelsius(heatingModel.getTist()), SIUnits.CELSIUS));
299 updateThingChannelState(CHANNEL_SETTEMP,
300 new QuantityType<>(toCelsius(heatingModel.getTsoll()), SIUnits.CELSIUS));
301 updateThingChannelState(CHANNEL_ECOTEMP,
302 new QuantityType<>(toCelsius(heatingModel.getAbsenk()), SIUnits.CELSIUS));
303 updateThingChannelState(CHANNEL_COMFORTTEMP,
304 new QuantityType<>(toCelsius(heatingModel.getKomfort()), SIUnits.CELSIUS));
305 updateThingChannelState(CHANNEL_RADIATOR_MODE, new StringType(heatingModel.getRadiatorMode()));
306 NextChangeModel nextChange = heatingModel.getNextchange();
307 if (nextChange != null) {
308 int endPeriod = nextChange.getEndperiod();
309 updateThingChannelState(CHANNEL_NEXT_CHANGE, endPeriod == 0 ? UnDefType.UNDEF
311 ZonedDateTime.ofInstant(Instant.ofEpochSecond(endPeriod), ZoneId.systemDefault())));
312 BigDecimal nextTemperature = nextChange.getTchange();
313 updateThingChannelState(CHANNEL_NEXTTEMP, TEMP_FRITZ_UNDEFINED.equals(nextTemperature) ? UnDefType.UNDEF
314 : new QuantityType<>(toCelsius(nextTemperature), SIUnits.CELSIUS));
316 updateBattery(heatingModel);
320 protected void updateBattery(BatteryModel batteryModel) {
321 BigDecimal batteryLevel = batteryModel.getBattery();
322 updateThingChannelState(CHANNEL_BATTERY,
323 batteryLevel == null ? UnDefType.UNDEF : new DecimalType(batteryLevel));
324 BigDecimal lowBattery = batteryModel.getBatterylow();
325 if (lowBattery == null) {
326 updateThingChannelState(CHANNEL_BATTERY_LOW, UnDefType.UNDEF);
328 updateThingChannelState(CHANNEL_BATTERY_LOW, OnOffType.from(BatteryModel.BATTERY_ON.equals(lowBattery)));
332 private void updateSimpleOnOffUnit(@Nullable SimpleOnOffModel simpleOnOffUnit) {
333 if (simpleOnOffUnit != null) {
334 updateThingChannelState(CHANNEL_ON_OFF, OnOffType.from(simpleOnOffUnit.state));
338 private void updateSwitchableOutlet(@Nullable SwitchModel switchModel) {
339 if (switchModel != null) {
340 updateThingChannelState(CHANNEL_MODE, new StringType(switchModel.getMode()));
341 updateThingChannelState(CHANNEL_LOCKED,
342 BigDecimal.ZERO.equals(switchModel.getLock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
343 updateThingChannelState(CHANNEL_DEVICE_LOCKED,
344 BigDecimal.ZERO.equals(switchModel.getDevicelock()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
345 BigDecimal state = switchModel.getState();
347 updateThingChannelState(CHANNEL_OUTLET, UnDefType.UNDEF);
349 updateThingChannelState(CHANNEL_OUTLET, OnOffType.from(SwitchModel.ON.equals(state)));
354 private void updatePowermeter(@Nullable PowerMeterModel powerMeterModel) {
355 if (powerMeterModel != null) {
356 updateThingChannelState(CHANNEL_ENERGY, new QuantityType<>(powerMeterModel.getEnergy(), Units.WATT_HOUR));
357 updateThingChannelState(CHANNEL_POWER, new QuantityType<>(powerMeterModel.getPower(), Units.WATT));
358 updateThingChannelState(CHANNEL_VOLTAGE, new QuantityType<>(powerMeterModel.getVoltage(), Units.VOLT));
363 * Updates thing properties.
365 * @param device the {@link AVMFritzBaseModel}
366 * @param editProperties map of existing properties
368 protected void updateProperties(AVMFritzBaseModel device, Map<String, String> editProperties) {
369 editProperties.put(Thing.PROPERTY_FIRMWARE_VERSION, device.getFirmwareVersion());
370 updateProperties(editProperties);
374 * Updates thing channels and creates dynamic channels if missing.
376 * @param channelId ID of the channel to be updated.
377 * @param state State to be set.
379 protected void updateThingChannelState(String channelId, State state) {
380 Channel channel = thing.getChannel(channelId);
381 if (channel != null) {
382 updateState(channel.getUID(), state);
384 logger.debug("Channel '{}' in thing '{}' does not exist, recreating thing.", channelId, thing.getUID());
385 createChannel(channelId);
390 * Creates a {@link ChannelTypeUID} from the given channel id.
392 * @param channelId ID of the channel type UID to be created.
393 * @return the channel type UID
395 private ChannelTypeUID createChannelTypeUID(String channelId) {
396 int pos = channelId.indexOf(ChannelUID.CHANNEL_GROUP_SEPARATOR);
397 String id = pos > -1 ? channelId.substring(pos + 1) : channelId;
398 final ChannelTypeUID channelTypeUID;
400 case CHANNEL_BATTERY:
401 channelTypeUID = DefaultSystemChannelTypeProvider.SYSTEM_CHANNEL_BATTERY_LEVEL.getUID();
403 case CHANNEL_VOLTAGE:
404 channelTypeUID = DefaultSystemChannelTypeProvider.SYSTEM_ELECTRIC_VOLTAGE.getUID();
407 channelTypeUID = new ChannelTypeUID(BINDING_ID, id);
410 return channelTypeUID;
414 * Creates new channels for the thing.
416 * @param channelId ID of the channel to be created.
418 private void createChannel(String channelId) {
419 ThingHandlerCallback callback = getCallback();
420 if (callback != null) {
421 final ChannelUID channelUID = new ChannelUID(thing.getUID(), channelId);
422 final ChannelTypeUID channelTypeUID = createChannelTypeUID(channelId);
423 final Channel channel = callback.createChannelBuilder(channelUID, channelTypeUID).build();
424 updateThing(editThing().withoutChannel(channelUID).withChannel(channel).build());
429 * Updates thing channel configurations.
431 * @param channelId ID of the channel which configuration to be updated.
432 * @param configId ID of the configuration to be updated.
433 * @param value Value to be set.
435 protected void updateThingChannelConfiguration(String channelId, String configId, Object value) {
436 Channel channel = thing.getChannel(channelId);
437 if (channel != null) {
438 Configuration editConfig = channel.getConfiguration();
439 editConfig.put(configId, value);
444 public void onDeviceGone(ThingUID thingUID) {
445 if (thing.getUID().equals(thingUID)) {
446 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "Device not present in response");
451 public void handleCommand(ChannelUID channelUID, Command command) {
452 String channelId = channelUID.getIdWithoutGroup();
453 logger.debug("Handle command '{}' for channel {}", command, channelId);
454 if (command == RefreshType.REFRESH) {
455 handleRefreshCommand();
458 FritzAhaWebInterface fritzBox = getWebInterface();
459 if (fritzBox == null) {
460 logger.debug("Cannot handle command '{}' because connection is missing", command);
463 String ain = getIdentifier();
465 logger.debug("Cannot handle command '{}' because AIN is missing", command);
471 case CHANNEL_DEVICE_LOCKED:
472 case CHANNEL_TEMPERATURE:
473 case CHANNEL_HUMIDITY:
476 case CHANNEL_VOLTAGE:
477 case CHANNEL_ACTUALTEMP:
478 case CHANNEL_ECOTEMP:
479 case CHANNEL_COMFORTTEMP:
480 case CHANNEL_NEXT_CHANGE:
481 case CHANNEL_NEXTTEMP:
482 case CHANNEL_BATTERY:
483 case CHANNEL_BATTERY_LOW:
484 case CHANNEL_CONTACT_STATE:
485 case CHANNEL_LAST_CHANGE:
486 case CHANNEL_OBSTRUCTION_ALARM:
487 case CHANNEL_TEMPERATURE_ALARM:
488 logger.debug("Channel {} is a read-only channel and cannot handle command '{}'", channelId, command);
492 if (command instanceof OnOffType) {
493 fritzBox.setSwitch(ain, OnOffType.ON.equals(command));
497 case CHANNEL_BRIGHTNESS:
498 BigDecimal brightness = null;
499 if (command instanceof HSBType hsbCommand) {
500 brightness = hsbCommand.getBrightness().toBigDecimal();
501 fritzBox.setUnmappedHueAndSaturation(ain, hsbCommand.getHue().intValue(),
502 ColorControlModel.fromPercent(hsbCommand.getSaturation()), 0);
503 } else if (command instanceof PercentType brightnessPercent) {
504 brightness = brightnessPercent.toBigDecimal();
505 } else if (command instanceof OnOffType) {
506 fritzBox.setSwitch(ain, OnOffType.ON.equals(command));
507 } else if (command instanceof IncreaseDecreaseType) {
508 brightness = ((DeviceModel) currentDevice).getLevelControlModel().getLevelPercentage();
509 if (IncreaseDecreaseType.INCREASE.equals(command)) {
510 brightness.add(BigDecimal.TEN);
512 brightness.subtract(BigDecimal.TEN);
515 if (brightness != null) {
516 if (brightness.equals(BigDecimal.ZERO)) {
517 fritzBox.setSwitch(ain, false);
519 fritzBox.setSwitch(ain, true);
520 fritzBox.setLevelPercentage(ain, brightness);
524 case CHANNEL_COLORTEMPERATURE:
525 BigDecimal colorTemperaturePct = null;
526 if (command instanceof PercentType percentCommand) {
527 colorTemperaturePct = percentCommand.toBigDecimal();
529 if (colorTemperaturePct != null) {
530 int pct = colorTemperaturePct.intValue();
531 // AHA-HTTP-Inteface docu say that the values typically between 2700K and 6500K
532 int tempMinMired = 1000000 / 2700;
533 int tempMaxMired = 1000000 / 6500;
534 int tempScaledMired = tempMinMired + ((tempMaxMired - tempMinMired) * pct / 100);
535 int tempKelvin = 1000000 / tempScaledMired;
536 fritzBox.setColorTemperature(ain, tempKelvin, 0);
539 case CHANNEL_COLORTEMPERATURE_ABS:
540 BigDecimal colorTemperature = null;
541 if (command instanceof QuantityType quantityCommand) {
542 QuantityType<?> convertedCommand = quantityCommand.toInvertibleUnit(Units.KELVIN);
543 if (convertedCommand != null) {
544 colorTemperature = convertedCommand.toBigDecimal();
546 } else if (command instanceof DecimalType decimalCommand) {
547 colorTemperature = decimalCommand.toBigDecimal();
549 if (colorTemperature != null) {
550 fritzBox.setColorTemperature(ain, colorTemperature.intValue(), 0);
553 case CHANNEL_SETTEMP:
554 BigDecimal temperature = null;
555 if (command instanceof DecimalType decimalCommand) {
556 temperature = normalizeCelsius(decimalCommand.toBigDecimal());
557 } else if (command instanceof QuantityType quantityCommand) {
558 @SuppressWarnings("unchecked")
559 QuantityType<Temperature> convertedCommand = ((QuantityType<Temperature>) command)
560 .toUnit(SIUnits.CELSIUS);
561 if (convertedCommand != null) {
562 temperature = normalizeCelsius(convertedCommand.toBigDecimal());
564 logger.warn("Unable to convert unit from '{}' to '{}'. Skipping command.",
565 quantityCommand.getUnit(), SIUnits.CELSIUS);
567 } else if (command instanceof IncreaseDecreaseType) {
568 temperature = currentDevice.getHkr().getTsoll();
569 if (IncreaseDecreaseType.INCREASE.equals(command)) {
570 temperature.add(BigDecimal.ONE);
572 temperature.subtract(BigDecimal.ONE);
574 } else if (command instanceof OnOffType) {
575 temperature = OnOffType.ON.equals(command) ? TEMP_FRITZ_ON : TEMP_FRITZ_OFF;
577 if (temperature != null) {
578 fritzBox.setSetTemp(ain, fromCelsius(temperature));
579 HeatingModel heatingModel = currentDevice.getHkr();
580 heatingModel.setTsoll(temperature);
581 updateState(CHANNEL_RADIATOR_MODE, new StringType(heatingModel.getRadiatorMode()));
584 case CHANNEL_RADIATOR_MODE:
585 BigDecimal targetTemperature = null;
586 if (command instanceof StringType) {
587 switch (command.toString()) {
589 targetTemperature = TEMP_FRITZ_ON;
592 targetTemperature = TEMP_FRITZ_OFF;
595 targetTemperature = currentDevice.getHkr().getKomfort();
598 targetTemperature = currentDevice.getHkr().getAbsenk();
601 targetTemperature = TEMP_FRITZ_MAX;
604 case MODE_WINDOW_OPEN:
605 logger.debug("Command '{}' is a read-only command for channel {}.", command, channelId);
608 if (targetTemperature != null) {
609 fritzBox.setSetTemp(ain, targetTemperature);
610 currentDevice.getHkr().setTsoll(targetTemperature);
611 updateState(CHANNEL_SETTEMP, new QuantityType<>(toCelsius(targetTemperature), SIUnits.CELSIUS));
615 case CHANNEL_ROLLERSHUTTER:
616 if (command instanceof StopMoveType rollershutterCommand) {
617 if (StopMoveType.STOP.equals(rollershutterCommand)) {
618 fritzBox.setBlind(ain, BlindCommand.STOP);
620 logger.debug("Received unknown rollershutter StopMove command MOVE");
622 } else if (command instanceof UpDownType rollershutterCommand) {
623 if (UpDownType.UP.equals(rollershutterCommand)) {
624 fritzBox.setBlind(ain, BlindCommand.OPEN);
626 fritzBox.setBlind(ain, BlindCommand.CLOSE);
628 } else if (command instanceof PercentType percentCommand) {
629 BigDecimal levelPercentage = percentCommand.toBigDecimal();
630 fritzBox.setLevelPercentage(ain, levelPercentage);
632 logger.debug("Received unknown rollershutter command type '{}'", command.toString());
636 logger.debug("Received unknown channel {}", channelId);
642 * Handles a command for a given action.
647 protected void handleAction(String action, long duration) {
648 FritzAhaWebInterface fritzBox = getWebInterface();
649 if (fritzBox == null) {
650 logger.debug("Cannot handle action '{}' because connection is missing", action);
653 String ain = getIdentifier();
655 logger.debug("Cannot handle action '{}' because AIN is missing", action);
658 if (duration < 0 || 86400 < duration) {
659 throw new IllegalArgumentException("Duration must not be less than zero or greater than 86400");
663 fritzBox.setBoostMode(ain,
664 duration > 0 ? ZonedDateTime.now().plusSeconds(duration).toEpochSecond() : 0);
666 case MODE_WINDOW_OPEN:
667 fritzBox.setWindowOpenMode(ain,
668 duration > 0 ? ZonedDateTime.now().plusSeconds(duration).toEpochSecond() : 0);
671 logger.debug("Received unknown action '{}'", action);
677 * Provides the web interface object.
679 * @return The web interface object
681 private @Nullable FritzAhaWebInterface getWebInterface() {
682 Bridge bridge = getBridge();
683 if (bridge != null) {
684 BridgeHandler handler = bridge.getHandler();
685 if (handler instanceof AVMFritzBaseBridgeHandler bridgeHandler) {
686 return bridgeHandler.getWebInterface();
693 * Handles a refresh command.
695 private void handleRefreshCommand() {
696 Bridge bridge = getBridge();
697 if (bridge != null) {
698 BridgeHandler handler = bridge.getHandler();
699 if (handler instanceof AVMFritzBaseBridgeHandler bridgeHandler) {
700 bridgeHandler.handleRefreshCommand();
710 public @Nullable String getIdentifier() {