Skip to content

Commit

Permalink
Changed 'apply_template' channel to command options (openhab#7547)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
Signed-off-by: MPH80 <[email protected]>
  • Loading branch information
cweitkamp authored and MPH80 committed Aug 3, 2020
1 parent 7fba71f commit aa2f59c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@
package org.openhab.binding.avmfritz.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.binding.BaseDynamicStateDescriptionProvider;
import org.eclipse.smarthome.core.thing.binding.BaseDynamicCommandDescriptionProvider;
import org.eclipse.smarthome.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider;
import org.eclipse.smarthome.core.thing.type.DynamicCommandDescriptionProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* Dynamic provider of state options while leaving other state description fields as original.
* Dynamic provider of command options.
*
* @author Christoph Weitkamp - Initial contribution
*/
@Component(service = { DynamicStateDescriptionProvider.class, AVMFritzDynamicStateDescriptionProvider.class })
@Component(service = { DynamicCommandDescriptionProvider.class, AVMFritzDynamicCommandDescriptionProvider.class })
@NonNullByDefault
public class AVMFritzDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
public class AVMFritzDynamicCommandDescriptionProvider extends BaseDynamicCommandDescriptionProvider {

@Reference
protected void setChannelTypeI18nLocalizationService(
final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
@Activate
public AVMFritzDynamicCommandDescriptionProvider(
final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
}

protected void unsetChannelTypeI18nLocalizationService(
final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
this.channelTypeI18nLocalizationService = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public class AVMFritzHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(AVMFritzHandlerFactory.class);

private final HttpClient httpClient;
private final AVMFritzDynamicStateDescriptionProvider stateDescriptionProvider;
private final AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider;

@Activate
public AVMFritzHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
final @Reference AVMFritzDynamicStateDescriptionProvider stateDescriptionProvider) {
final @Reference AVMFritzDynamicCommandDescriptionProvider stateDescriptionProvider) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.stateDescriptionProvider = stateDescriptionProvider;
this.commandDescriptionProvider = stateDescriptionProvider;
}

/**
Expand All @@ -71,9 +71,9 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (BRIDGE_THING_TYPE.equals(thingTypeUID)) {
return new BoxHandler((Bridge) thing, httpClient, stateDescriptionProvider);
return new BoxHandler((Bridge) thing, httpClient, commandDescriptionProvider);
} else if (PL546E_STANDALONE_THING_TYPE.equals(thingTypeUID)) {
return new Powerline546EHandler((Bridge) thing, httpClient, stateDescriptionProvider);
return new Powerline546EHandler((Bridge) thing, httpClient, commandDescriptionProvider);
} else if (SUPPORTED_BUTTON_THING_TYPES_UIDS.contains(thingTypeUID)) {
return new AVMFritzButtonHandler(thing);
} else if (SUPPORTED_DEVICE_THING_TYPES_UIDS.contains(thingTypeUID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.smarthome.core.types.CommandOption;

/**
* See {@link TemplateListModel}.
*
Expand Down Expand Up @@ -68,6 +70,10 @@ public ApplyMaskListModel getApplyMaskList() {
return applyMaskList;
}

public CommandOption toCommandOption() {
return new CommandOption(getIdentifier(), getName());
}

@Override
public String toString() {
return new StringBuilder().append("[identifier=").append(identifier).append(",id=").append(templateId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
import static org.openhab.binding.avmfritz.internal.dto.DeviceModel.ETSUnitInfoModel.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -44,10 +43,8 @@
import org.eclipse.smarthome.core.thing.binding.ThingHandlerService;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.StateOption;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicStateDescriptionProvider;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicCommandDescriptionProvider;
import org.openhab.binding.avmfritz.internal.config.AVMFritzBoxConfiguration;
import org.openhab.binding.avmfritz.internal.discovery.AVMFritzDiscoveryService;
import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
Expand Down Expand Up @@ -95,7 +92,7 @@ public abstract class AVMFritzBaseBridgeHandler extends BaseBridgeHandler {
*/
private final HttpClient httpClient;

private final AVMFritzDynamicStateDescriptionProvider stateDescriptionProvider;
private final AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider;

protected final List<FritzAhaStatusListener> listeners = new CopyOnWriteArrayList<>();

Expand All @@ -110,10 +107,10 @@ public abstract class AVMFritzBaseBridgeHandler extends BaseBridgeHandler {
* @param bridge Bridge object representing a FRITZ!Box
*/
public AVMFritzBaseBridgeHandler(Bridge bridge, HttpClient httpClient,
AVMFritzDynamicStateDescriptionProvider stateDescriptionProvider) {
AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
super(bridge);
this.httpClient = httpClient;
this.stateDescriptionProvider = stateDescriptionProvider;
this.commandDescriptionProvider = commandDescriptionProvider;

applyTemplateChannelUID = new ChannelUID(bridge.getUID(), CHANNEL_APPLY_TEMPLATE);
}
Expand Down Expand Up @@ -241,12 +238,8 @@ public void setStatusInfo(ThingStatus status, ThingStatusDetail statusDetail, @N
* @param templateList list of template models
*/
public void addTemplateList(List<TemplateModel> templateList) {
List<StateOption> options = new ArrayList<>();
for (TemplateModel template : templateList) {
logger.debug("Process template model: {}", template);
options.add(new StateOption(template.getIdentifier(), template.getName()));
}
stateDescriptionProvider.setStateOptions(applyTemplateChannelUID, options);
commandDescriptionProvider.setCommandOptions(applyTemplateChannelUID,
templateList.stream().map(TemplateModel::toCommandOption).collect(Collectors.toList()));
}

/**
Expand Down Expand Up @@ -350,21 +343,13 @@ public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("Cannot handle command '{}' because connection is missing", command);
return;
}
switch (channelId) {
case CHANNEL_APPLY_TEMPLATE:
applyTemplate(command, fritzBox);
break;
default:
logger.debug("Received unknown channel {}", channelId);
break;
}
}

protected void applyTemplate(Command command, FritzAhaWebInterface fritzBox) {
if (command instanceof StringType) {
fritzBox.applyTemplate(command.toString());
if (CHANNEL_APPLY_TEMPLATE.equals(channelId)) {
if (command instanceof StringType) {
fritzBox.applyTemplate(command.toString());
}
} else {
logger.debug("Received unknown channel {}", channelId);
}
updateState(CHANNEL_APPLY_TEMPLATE, UnDefType.UNDEF);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.smarthome.core.thing.Bridge;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicStateDescriptionProvider;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicCommandDescriptionProvider;

/**
* Handler for a FRITZ!Box device. Handles polling of values from AHA devices.
Expand All @@ -32,7 +32,7 @@ public class BoxHandler extends AVMFritzBaseBridgeHandler {
* @param bridge Bridge object representing a FRITZ!Box
*/
public BoxHandler(Bridge bridge, HttpClient httpClient,
AVMFritzDynamicStateDescriptionProvider stateDescriptionProvider) {
super(bridge, httpClient, stateDescriptionProvider);
AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
super(bridge, httpClient, commandDescriptionProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicStateDescriptionProvider;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicCommandDescriptionProvider;
import org.openhab.binding.avmfritz.internal.config.AVMFritzBoxConfiguration;
import org.openhab.binding.avmfritz.internal.config.AVMFritzDeviceConfiguration;
import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
Expand Down Expand Up @@ -80,8 +80,8 @@ public class Powerline546EHandler extends AVMFritzBaseBridgeHandler implements F
* @param bridge Bridge object representing a FRITZ!Powerline 546E
*/
public Powerline546EHandler(Bridge bridge, HttpClient httpClient,
AVMFritzDynamicStateDescriptionProvider stateDescriptionProvider) {
super(bridge, httpClient, stateDescriptionProvider);
AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
super(bridge, httpClient, commandDescriptionProvider);
}

@Override
Expand Down Expand Up @@ -287,7 +287,9 @@ public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("Channel {} is a read-only channel and cannot handle command '{}'", channelId, command);
break;
case CHANNEL_APPLY_TEMPLATE:
applyTemplate(command, fritzBox);
if (command instanceof StringType) {
fritzBox.applyTemplate(command.toString());
}
break;
case CHANNEL_OUTLET:
fritzBox.setSwitch(ain, OnOffType.ON.equals(command));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicStateDescriptionProvider;
import org.openhab.binding.avmfritz.internal.AVMFritzDynamicCommandDescriptionProvider;

/**
* Tests for {@link AVMFritzThingHandlerOSGiTest}.
Expand Down Expand Up @@ -70,7 +70,7 @@ public void setUp() {

ThingHandlerCallback callback = mock(ThingHandlerCallback.class);

bridgeHandler = new BoxHandler(bridge, httpClient, new AVMFritzDynamicStateDescriptionProvider());
bridgeHandler = new BoxHandler(bridge, httpClient, mock(AVMFritzDynamicCommandDescriptionProvider.class));
assertNotNull(bridgeHandler);

bridgeHandler.setCallback(callback);
Expand Down

0 comments on commit aa2f59c

Please sign in to comment.