Skip to content

Commit

Permalink
Update thing properties on device status update
Browse files Browse the repository at this point in the history
Also-by: Bert Plonus <[email protected]>
Also-by: Martin Lepsy <[email protected]>
Also-by: Benjamin Bolte <[email protected]>
Signed-off-by: Björn Lange <[email protected]>
  • Loading branch information
Björn Lange committed Dec 3, 2020
1 parent 6605375 commit 1e00ed6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.I18NKeys;
import org.openhab.binding.mielecloud.internal.discovery.ThingInformationExtractor;
import org.openhab.binding.mielecloud.internal.handler.channel.ActionsChannelState;
import org.openhab.binding.mielecloud.internal.handler.channel.DeviceChannelState;
import org.openhab.binding.mielecloud.internal.handler.channel.TransitionChannelState;
Expand Down Expand Up @@ -174,6 +175,7 @@ public final void onDeviceStateUpdated(DeviceState deviceState) {
latestTransitionState = new TransitionState(latestTransitionState, deviceState);
latestDeviceState = deviceState;

updateThingProperties(deviceState);
updateDeviceState(new DeviceChannelState(latestDeviceState));
updateTransitionState(new TransitionChannelState(latestTransitionState));
updateThingStatus(latestDeviceState);
Expand Down Expand Up @@ -268,6 +270,15 @@ protected PowerStatus getPowerStatus(StateType rawStatus) {
return POWER_ON;
}

/**
* Updates the thing properties. This is necessary if properties have not been set during discovery.
*/
private void updateThingProperties(DeviceState deviceState) {
var properties = editProperties();
properties.putAll(ThingInformationExtractor.extractProperties(getThing().getThingTypeUID(), deviceState));
updateProperties(properties);
}

/**
* Updates the device state channels.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,28 @@ public void testHandleCommandDoesNothingWhenPowerCommandIsNotOfOnOffType() {
// then:
verify(getWebserviceMock(), never()).putPowerState(anyString(), anyBoolean());
}

@Test
public void testMissingPropertiesAreSetWhenAStateUpdateIsReceivedFromTheCloud() {
// given:
assertFalse(getThingHandler().getThing().getProperties().containsKey(Thing.PROPERTY_SERIAL_NUMBER));
assertFalse(getThingHandler().getThing().getProperties().containsKey(Thing.PROPERTY_MODEL_ID));

var deviceState = mock(DeviceState.class);
when(deviceState.getRawType()).thenReturn(DeviceType.UNKNOWN);
when(deviceState.getDeviceIdentifier()).thenReturn(MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
when(deviceState.getFabNumber())
.thenReturn(Optional.of(MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER));
when(deviceState.getType()).thenReturn(Optional.of("Unknown device type"));
when(deviceState.getTechType()).thenReturn(Optional.of("UK-4567"));

// when:
getThingHandler().onDeviceStateUpdated(deviceState);

// then:
assertEquals(MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER,
getThingHandler().getThing().getProperties().get(Thing.PROPERTY_SERIAL_NUMBER));
assertEquals("Unknown device type UK-4567",
getThingHandler().getThing().getProperties().get(Thing.PROPERTY_MODEL_ID));
}
}

0 comments on commit 1e00ed6

Please sign in to comment.