Skip to content

Commit

Permalink
[homeconnect] Use getLinkedChannel rather than getThingChannel (openh…
Browse files Browse the repository at this point in the history
…ab#11069)

when the purpose is only to call the method updateState

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Aug 4, 2021
1 parent 0325622 commit 3826936
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,16 +587,16 @@ protected void resetProgramStateChannels(boolean offline) {
protected void resetChannelsOnOfflineEvent() {
logger.debug("Resetting channel states due to OFFLINE event. thing={}, haId={}", getThingLabel(),
getThingHaId());
getThingChannel(CHANNEL_POWER_STATE).ifPresent(channel -> updateState(channel.getUID(), OnOffType.OFF));
getThingChannel(CHANNEL_OPERATION_STATE).ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_DOOR_STATE).ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE)
getLinkedChannel(CHANNEL_POWER_STATE).ifPresent(channel -> updateState(channel.getUID(), OnOffType.OFF));
getLinkedChannel(CHANNEL_OPERATION_STATE).ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_DOOR_STATE).ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE)
.ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_REMOTE_CONTROL_ACTIVE_STATE)
getLinkedChannel(CHANNEL_REMOTE_CONTROL_ACTIVE_STATE)
.ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_REMOTE_START_ALLOWANCE_STATE)
getLinkedChannel(CHANNEL_REMOTE_START_ALLOWANCE_STATE)
.ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
getLinkedChannel(CHANNEL_SELECTED_PROGRAM_STATE)
.ifPresent(channel -> updateState(channel.getUID(), UnDefType.UNDEF));
}

Expand Down Expand Up @@ -792,43 +792,43 @@ protected void handleAuthenticationError(AuthorizationException exception) {
}

protected EventHandler defaultElapsedProgramTimeEventHandler() {
return event -> getThingChannel(CHANNEL_ELAPSED_PROGRAM_TIME)
return event -> getLinkedChannel(CHANNEL_ELAPSED_PROGRAM_TIME)
.ifPresent(channel -> updateState(channel.getUID(), new QuantityType<>(event.getValueAsInt(), SECOND)));
}

protected EventHandler defaultPowerStateEventHandler() {
return event -> {
getThingChannel(CHANNEL_POWER_STATE).ifPresent(
getLinkedChannel(CHANNEL_POWER_STATE).ifPresent(
channel -> updateState(channel.getUID(), OnOffType.from(STATE_POWER_ON.equals(event.getValue()))));

if (STATE_POWER_ON.equals(event.getValue())) {
getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE).ifPresent(c -> updateChannel(c.getUID()));
} else {
resetProgramStateChannels(true);
getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
getLinkedChannel(CHANNEL_SELECTED_PROGRAM_STATE)
.ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
}
};
}

protected EventHandler defaultDoorStateEventHandler() {
return event -> getThingChannel(CHANNEL_DOOR_STATE).ifPresent(channel -> updateState(channel.getUID(),
return event -> getLinkedChannel(CHANNEL_DOOR_STATE).ifPresent(channel -> updateState(channel.getUID(),
STATE_DOOR_OPEN.equals(event.getValue()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED));
}

protected EventHandler defaultOperationStateEventHandler() {
return event -> {
String value = event.getValue();
getThingChannel(CHANNEL_OPERATION_STATE).ifPresent(channel -> updateState(channel.getUID(),
getLinkedChannel(CHANNEL_OPERATION_STATE).ifPresent(channel -> updateState(channel.getUID(),
value == null ? UnDefType.UNDEF : new StringType(mapStringType(value))));

if (STATE_OPERATION_FINISHED.equals(value)) {
getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
.ifPresent(c -> updateState(c.getUID(), new QuantityType<>(100, PERCENT)));
getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
getLinkedChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
.ifPresent(c -> updateState(c.getUID(), new QuantityType<>(0, SECOND)));
} else if (STATE_OPERATION_RUN.equals(value)) {
getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
.ifPresent(c -> updateState(c.getUID(), new QuantityType<>(0, PERCENT)));
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateChannel(c.getUID()));
} else if (STATE_OPERATION_READY.equals(value)) {
Expand All @@ -840,7 +840,7 @@ protected EventHandler defaultOperationStateEventHandler() {
protected EventHandler defaultActiveProgramEventHandler() {
return event -> {
String value = event.getValue();
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(channel -> updateState(channel.getUID(),
getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(channel -> updateState(channel.getUID(),
value == null ? UnDefType.UNDEF : new StringType(mapStringType(value))));
if (value == null) {
resetProgramStateChannels(false);
Expand All @@ -851,7 +851,7 @@ protected EventHandler defaultActiveProgramEventHandler() {
protected EventHandler updateProgramOptionsAndActiveProgramStateEventHandler() {
return event -> {
String value = event.getValue();
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(channel -> updateState(channel.getUID(),
getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(channel -> updateState(channel.getUID(),
value == null ? UnDefType.UNDEF : new StringType(mapStringType(value))));
if (value == null) {
resetProgramStateChannels(false);
Expand All @@ -874,34 +874,34 @@ protected EventHandler updateProgramOptionsAndActiveProgramStateEventHandler() {
}

protected EventHandler defaultEventPresentStateEventHandler(String channelId) {
return event -> getThingChannel(channelId).ifPresent(channel -> updateState(channel.getUID(),
return event -> getLinkedChannel(channelId).ifPresent(channel -> updateState(channel.getUID(),
OnOffType.from(!STATE_EVENT_PRESENT_STATE_OFF.equals(event.getValue()))));
}

protected EventHandler defaultBooleanEventHandler(String channelId) {
return event -> getThingChannel(channelId)
return event -> getLinkedChannel(channelId)
.ifPresent(channel -> updateState(channel.getUID(), OnOffType.from(event.getValueAsBoolean())));
}

protected EventHandler defaultRemainingProgramTimeEventHandler() {
return event -> getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
return event -> getLinkedChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE)
.ifPresent(channel -> updateState(channel.getUID(), new QuantityType<>(event.getValueAsInt(), SECOND)));
}

protected EventHandler defaultSelectedProgramStateEventHandler() {
return event -> getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE)
return event -> getLinkedChannel(CHANNEL_SELECTED_PROGRAM_STATE)
.ifPresent(channel -> updateState(channel.getUID(),
event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue())));
}

protected EventHandler defaultAmbientLightColorStateEventHandler() {
return event -> getThingChannel(CHANNEL_AMBIENT_LIGHT_COLOR_STATE)
return event -> getLinkedChannel(CHANNEL_AMBIENT_LIGHT_COLOR_STATE)
.ifPresent(channel -> updateState(channel.getUID(),
event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue())));
}

protected EventHandler defaultAmbientLightCustomColorStateEventHandler() {
return event -> getThingChannel(CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE).ifPresent(channel -> {
return event -> getLinkedChannel(CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE).ifPresent(channel -> {
String value = event.getValue();
if (value != null) {
updateState(channel.getUID(), mapColor(value));
Expand Down Expand Up @@ -979,12 +979,12 @@ protected EventHandler updateProgramOptionsAndSelectedProgramStateEventHandler()
}

protected EventHandler defaultPercentQuantityTypeEventHandler(String channelId) {
return event -> getThingChannel(channelId).ifPresent(
return event -> getLinkedChannel(channelId).ifPresent(
channel -> updateState(channel.getUID(), new QuantityType<>(event.getValueAsInt(), PERCENT)));
}

protected EventHandler defaultPercentHandler(String channelId) {
return event -> getThingChannel(channelId)
return event -> getLinkedChannel(channelId)
.ifPresent(channel -> updateState(channel.getUID(), new PercentType(event.getValueAsInt())));
}

Expand Down Expand Up @@ -1030,18 +1030,18 @@ protected ChannelUpdateHandler defaultAmbientLightChannelUpdateHandler() {
if (enabled) {
// brightness
Data brightnessData = apiClient.get().getAmbientLightBrightnessState(getThingHaId());
getThingChannel(CHANNEL_AMBIENT_LIGHT_BRIGHTNESS_STATE)
getLinkedChannel(CHANNEL_AMBIENT_LIGHT_BRIGHTNESS_STATE)
.ifPresent(channel -> updateState(channel.getUID(),
new PercentType(brightnessData.getValueAsInt())));

// color
Data colorData = apiClient.get().getAmbientLightColorState(getThingHaId());
getThingChannel(CHANNEL_AMBIENT_LIGHT_COLOR_STATE).ifPresent(
getLinkedChannel(CHANNEL_AMBIENT_LIGHT_COLOR_STATE).ifPresent(
channel -> updateState(channel.getUID(), new StringType(colorData.getValue())));

// custom color
Data customColorData = apiClient.get().getAmbientLightCustomColorState(getThingHaId());
getThingChannel(CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE).ifPresent(channel -> {
getLinkedChannel(CHANNEL_AMBIENT_LIGHT_CUSTOM_COLOR_STATE).ifPresent(channel -> {
String value = customColorData.getValue();
if (value != null) {
updateState(channel.getUID(), mapColor(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected void configureEventHandlers(Map<String, EventHandler> handlers) {
// register coffee maker specific SSE event handlers
handlers.put(EVENT_PROGRAM_PROGRESS, event -> {
if (event.getValue() == null || event.getValueAsInt() == 0) {
getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
.ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
} else {
defaultPercentQuantityTypeEventHandler(CHANNEL_PROGRAM_PROGRESS_STATE).handle(event);
Expand All @@ -97,7 +97,7 @@ public String toString() {
@Override
protected void resetProgramStateChannels(boolean offline) {
super.resetProgramStateChannels(offline);
getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public String toString() {
@Override
protected void resetProgramStateChannels(boolean offline) {
super.resetProgramStateChannels(offline);
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public String toString() {
@Override
protected void resetProgramStateChannels(boolean offline) {
super.resetProgramStateChannels(offline);
getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ protected void configureEventHandlers(Map<String, EventHandler> handlers) {

// register dryer specific event handlers
handlers.put(EVENT_DRYER_DRYING_TARGET,
event -> getThingChannel(CHANNEL_DRYER_DRYING_TARGET).ifPresent(channel -> updateState(channel.getUID(),
event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
event -> getLinkedChannel(CHANNEL_DRYER_DRYING_TARGET)
.ifPresent(channel -> updateState(channel.getUID(),
event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
}

@Override
Expand Down Expand Up @@ -116,11 +117,11 @@ public String toString() {
@Override
protected void resetProgramStateChannels(boolean offline) {
super.resetProgramStateChannels(offline);
getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
if (offline) {
getThingChannel(CHANNEL_DRYER_DRYING_TARGET).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
getLinkedChannel(CHANNEL_DRYER_DRYING_TARGET).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
*/
package org.openhab.binding.homeconnect.internal.handler;

import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_DOOR_STATE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_FREEZER_SETPOINT_TEMPERATURE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_FREEZER_SUPER_MODE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_REFRIGERATOR_SUPER_MODE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_DOOR_STATE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FREEZER_SETPOINT_TEMPERATURE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FREEZER_SUPER_MODE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FRIDGE_SETPOINT_TEMPERATURE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FRIDGE_SUPER_MODE;
import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;

import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -130,11 +121,11 @@ protected void configureEventHandlers(Map<String, EventHandler> handlers) {

// register fridge/freezer specific event handlers
handlers.put(EVENT_FREEZER_SETPOINT_TEMPERATURE,
event -> getThingChannel(CHANNEL_FREEZER_SETPOINT_TEMPERATURE)
event -> getLinkedChannel(CHANNEL_FREEZER_SETPOINT_TEMPERATURE)
.ifPresent(channel -> updateState(channel.getUID(),
new QuantityType<>(event.getValueAsInt(), mapTemperature(event.getUnit())))));
handlers.put(EVENT_FRIDGE_SETPOINT_TEMPERATURE,
event -> getThingChannel(CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE)
event -> getLinkedChannel(CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE)
.ifPresent(channel -> updateState(channel.getUID(),
new QuantityType<>(event.getValueAsInt(), mapTemperature(event.getUnit())))));
}
Expand Down
Loading

0 comments on commit 3826936

Please sign in to comment.