Skip to content

Commit b1688ae

Browse files
authored
Transform bindings to getConfigAs (#21365)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
1 parent 6a39ada commit b1688ae

55 files changed

Lines changed: 180 additions & 104 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bundles/org.openhab.binding.airq/src/main/java/org/openhab/binding/airq/internal/AirqHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
312312

313313
@Override
314314
public void initialize() {
315-
config = getThing().getConfiguration().as(AirqConfiguration.class);
315+
config = getConfigAs(AirqConfiguration.class);
316316
updateStatus(ThingStatus.UNKNOWN);
317317

318318
pollingJob = scheduler.scheduleWithFixedDelay(this::pollData, 0, POLLING_PERIOD_DATA_MSEC,

bundles/org.openhab.binding.atlona/src/main/java/org/openhab/binding/atlona/internal/pro3/AtlonaPro3Handler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ private void retryConnect() {
594594
* @return {@link AtlonaPro3Config}
595595
*/
596596
private AtlonaPro3Config getAtlonaConfig() {
597-
return getThing().getConfiguration().as(AtlonaPro3Config.class);
597+
return getConfigAs(AtlonaPro3Config.class);
598598
}
599599

600600
/**

bundles/org.openhab.binding.cm11a/src/main/java/org/openhab/binding/cm11a/internal/handler/Cm11aBridgeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
6868
@Override
6969
public void initialize() {
7070
// Get serial port number from config
71-
cm11aConfig = getThing().getConfiguration().as(Cm11aConfig.class);
71+
cm11aConfig = getConfigAs(Cm11aConfig.class);
7272
logger.trace("********* cm11a initialize started *********");
7373

7474
// Verify the configuration is valid

bundles/org.openhab.binding.deutschebahn/src/main/java/org/openhab/binding/deutschebahn/internal/DeutscheBahnTimetableHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private static final class GroupedThings {
7272

7373
public void addThing(Thing thing) {
7474
if (isTrain(thing)) {
75-
int position = thing.getConfiguration().as(DeutscheBahnTrainConfiguration.class).position;
75+
int position = DeutscheBahnTimetableHandler.getThingConfig(thing,
76+
DeutscheBahnTrainConfiguration.class).position;
7677
this.maxPosition = Math.max(this.maxPosition, position);
7778
List<Thing> thingsAtPosition = this.thingsPerPosition.get(position);
7879
if (thingsAtPosition == null) {
@@ -107,6 +108,14 @@ public int getMaxPosition() {
107108
private final Logger logger = LoggerFactory.getLogger(DeutscheBahnTimetableHandler.class);
108109
private @Nullable TimetableLoader loader;
109110

111+
private static <T> T getThingConfig(Thing thing, Class<T> configurationClass) {
112+
ThingHandler handler = thing.getHandler();
113+
if (handler instanceof DeutscheBahnTrainHandler trainHandler) {
114+
return trainHandler.getConfigAs(configurationClass);
115+
}
116+
return thing.getConfiguration().as(configurationClass);
117+
}
118+
110119
private final TimetablesV1ApiFactory timetablesV1ApiFactory;
111120

112121
private final Supplier<Date> currentTimeProvider;

bundles/org.openhab.binding.deutschebahn/src/main/java/org/openhab/binding/deutschebahn/internal/DeutscheBahnTrainHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public DeutscheBahnTrainHandler(Thing thing) {
9494
super(thing);
9595
}
9696

97+
@Override
98+
public <T> T getConfigAs(Class<T> configurationClass) {
99+
return super.getConfigAs(configurationClass);
100+
}
101+
97102
@Override
98103
public void initialize() {
99104
this.updateStatus(ThingStatus.UNKNOWN);

bundles/org.openhab.binding.deutschebahn/src/test/java/org/openhab/binding/deutschebahn/internal/DeutscheBahnTimetableHandlerTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ private static Configuration createConfig(String trainFilter) {
6363
return config;
6464
}
6565

66+
private static DeutscheBahnTrainConfiguration createTrainConfig(int position) {
67+
final DeutscheBahnTrainConfiguration trainConfig = new DeutscheBahnTrainConfiguration();
68+
trainConfig.position = position;
69+
return trainConfig;
70+
}
71+
6672
private static Bridge mockBridge(String trainFilter) {
6773
final Bridge bridge = mock(Bridge.class);
6874
when(bridge.getUID()).thenReturn(new ThingUID(DeutscheBahnBindingConstants.TIMETABLE_TYPE, "timetable"));
@@ -72,9 +78,18 @@ private static Bridge mockBridge(String trainFilter) {
7278
things.add(DeutscheBahnTrainHandlerTest.mockThing(1));
7379
things.add(DeutscheBahnTrainHandlerTest.mockThing(2));
7480
things.add(DeutscheBahnTrainHandlerTest.mockThing(3));
75-
when(things.get(0).getHandler()).thenReturn(mock(DeutscheBahnTrainHandler.class));
76-
when(things.get(1).getHandler()).thenReturn(mock(DeutscheBahnTrainHandler.class));
77-
when(things.get(2).getHandler()).thenReturn(mock(DeutscheBahnTrainHandler.class));
81+
82+
DeutscheBahnTrainHandler trainHandler1 = mock(DeutscheBahnTrainHandler.class);
83+
DeutscheBahnTrainHandler trainHandler2 = mock(DeutscheBahnTrainHandler.class);
84+
DeutscheBahnTrainHandler trainHandler3 = mock(DeutscheBahnTrainHandler.class);
85+
86+
when(things.get(0).getHandler()).thenReturn(trainHandler1);
87+
when(things.get(1).getHandler()).thenReturn(trainHandler2);
88+
when(things.get(2).getHandler()).thenReturn(trainHandler3);
89+
90+
when(trainHandler1.getConfigAs(DeutscheBahnTrainConfiguration.class)).thenReturn(createTrainConfig(1));
91+
when(trainHandler2.getConfigAs(DeutscheBahnTrainConfiguration.class)).thenReturn(createTrainConfig(2));
92+
when(trainHandler3.getConfigAs(DeutscheBahnTrainConfiguration.class)).thenReturn(createTrainConfig(3));
7893

7994
when(bridge.getThings()).thenReturn(things);
8095

bundles/org.openhab.binding.enocean/src/main/java/org/openhab/binding/enocean/internal/handler/EnOceanBridgeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void run() {
177177

178178
private synchronized void initTransceiver() {
179179
try {
180-
EnOceanBridgeConfig c = getThing().getConfiguration().as(EnOceanBridgeConfig.class);
180+
EnOceanBridgeConfig c = getConfigAs(EnOceanBridgeConfig.class);
181181
EnOceanTransceiver localTransceiver = transceiver;
182182
if (localTransceiver != null) {
183183
localTransceiver.shutDown();
@@ -320,7 +320,7 @@ public Collection<ConfigStatusMessage> getConfigStatus() {
320320
Collection<ConfigStatusMessage> configStatusMessages = new LinkedList<>();
321321

322322
// The serial port must be provided
323-
String path = getThing().getConfiguration().as(EnOceanBridgeConfig.class).path;
323+
String path = getConfigAs(EnOceanBridgeConfig.class).path;
324324
if (path.isEmpty()) {
325325
ConfigStatusMessage statusMessage = ConfigStatusMessage.Builder.error(PATH)
326326
.withMessageKeySuffix(EnOceanConfigStatusMessage.PORT_MISSING.getMessageKey()).withArguments(PATH)

bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/handler/FineOffsetGatewayHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private void updateBridgeThing(Consumer<BridgeBuilder> customizer) {
267267
private void startDiscoverJob() {
268268
ScheduledFuture<?> job = discoverJob;
269269
if (job == null || job.isCancelled()) {
270-
int discoverInterval = thing.getConfiguration().as(FineOffsetGatewayConfiguration.class).discoverInterval;
270+
int discoverInterval = getConfigAs(FineOffsetGatewayConfiguration.class).discoverInterval;
271271
discoverJob = scheduler.scheduleWithFixedDelay(this::fetchAndUpdateSensors, 0, discoverInterval,
272272
TimeUnit.SECONDS);
273273
}
@@ -284,7 +284,7 @@ private void stopDiscoverJob() {
284284
private void startPollingJob() {
285285
ScheduledFuture<?> job = pollingJob;
286286
if (job == null || job.isCancelled()) {
287-
int pollingInterval = thing.getConfiguration().as(FineOffsetGatewayConfiguration.class).pollingInterval;
287+
int pollingInterval = getConfigAs(FineOffsetGatewayConfiguration.class).pollingInterval;
288288
pollingJob = scheduler.scheduleWithFixedDelay(this::updateLiveData, 5, pollingInterval, TimeUnit.SECONDS);
289289
}
290290
}

bundles/org.openhab.binding.fineoffsetweatherstation/src/main/java/org/openhab/binding/fineoffsetweatherstation/internal/handler/FineOffsetSensorHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public FineOffsetSensorHandler(Thing thing) {
4545
super(thing);
4646
}
4747

48+
@Override
49+
public <T> T getConfigAs(Class<T> configurationClass) {
50+
return super.getConfigAs(configurationClass);
51+
}
52+
4853
@Override
4954
public void handleCommand(ChannelUID channelUID, Command command) {
5055
}

bundles/org.openhab.binding.gardena/src/main/java/org/openhab/binding/gardena/internal/handler/GardenaAccountHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ public void setDiscoveryService(GardenaDeviceDiscoveryService discoveryService)
170170
*/
171171
private synchronized void initializeGardena() {
172172
try {
173-
GardenaConfig gardenaConfig = getThing().getConfiguration().as(GardenaConfig.class);
174-
logger.debug("{}", gardenaConfig);
175-
173+
GardenaConfig gardenaConfig = getConfigAs(GardenaConfig.class);
174+
logger.debug("Loaded Gardena config (timeout={}, apiKeySet={})", gardenaConfig.getConnectionTimeout(),
175+
gardenaConfig.getApiKey() != null && !gardenaConfig.getApiKey().isBlank());
176176
gardenaSmart = new GardenaSmartImpl(getThing().getUID(), gardenaConfig, this, scheduler, httpClientFactory,
177177
webSocketFactory);
178178
final GardenaDeviceDiscoveryService discoveryService = this.discoveryService;

0 commit comments

Comments
 (0)