Skip to content

Commit

Permalink
[wundergroundupdatereceiver] Check whether the thing is managed (open…
Browse files Browse the repository at this point in the history
…hab#12998)

Ie. created in the UI or by discovery
Signed-off-by: Daniel Demus <daniel-github@demus.dk>
  • Loading branch information
danieldemus authored and leifbladt committed Oct 15, 2022
1 parent b3c0aa5 commit 5787544
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelGroupUID;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ManagedThingProvider;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
Expand Down Expand Up @@ -63,6 +64,7 @@ public String getStationId() {
private final WundergroundUpdateReceiverDiscoveryService discoveryService;
private final WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider;
private final ChannelTypeRegistry channelTypeRegistry;
private final ManagedThingProvider managedThingProvider;

private final ChannelUID dateutcDatetimeChannel;
private final ChannelUID lastReceivedChannel;
Expand All @@ -75,11 +77,12 @@ public WundergroundUpdateReceiverHandler(Thing thing,
WundergroundUpdateReceiverServlet wunderGroundUpdateReceiverServlet,
WundergroundUpdateReceiverDiscoveryService discoveryService,
WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider,
ChannelTypeRegistry channelTypeRegistry) {
ChannelTypeRegistry channelTypeRegistry, ManagedThingProvider managedThingProvider) {
super(thing);
this.discoveryService = discoveryService;
this.channelTypeProvider = channelTypeProvider;
this.channelTypeRegistry = channelTypeRegistry;
this.managedThingProvider = managedThingProvider;

final ChannelGroupUID metadatGroupUID = new ChannelGroupUID(getThing().getUID(), METADATA_GROUP);

Expand Down Expand Up @@ -205,7 +208,8 @@ public void updateChannelState(String parameterName, String state) {
updateState(channelUID, new DecimalType(numberValue));
}
} else if (this.discoveryService.isDiscovering()
&& !WundergroundUpdateReceiverParameterMapping.isExcluded(parameterName)) {
&& !WundergroundUpdateReceiverParameterMapping.isExcluded(parameterName)
&& this.managedThingProvider.get(this.thing.getUID()) != null) {
ThingBuilder thingBuilder = editThing();
buildChannel(thingBuilder, parameterName, state);
updateThing(thingBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.ManagedThingProvider;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
Expand All @@ -42,15 +43,17 @@ public class WundergroundUpdateReceiverHandlerFactory extends BaseThingHandlerFa
private final ChannelTypeRegistry channelTypeRegistry;
private final WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider;
private final WundergroundUpdateReceiverServlet wunderGroundUpdateReceiverServlet;
private final ManagedThingProvider managedThingProvider;

@Activate
public WundergroundUpdateReceiverHandlerFactory(@Reference HttpService httpService,
@Reference WundergroundUpdateReceiverDiscoveryService discoveryService,
@Reference WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider,
@Reference ChannelTypeRegistry channelTypeRegistry) {
@Reference ChannelTypeRegistry channelTypeRegistry, @Reference ManagedThingProvider managedThingProvider) {
this.discoveryService = discoveryService;
this.channelTypeRegistry = channelTypeRegistry;
this.channelTypeProvider = channelTypeProvider;
this.managedThingProvider = managedThingProvider;
this.wunderGroundUpdateReceiverServlet = new WundergroundUpdateReceiverServlet(httpService,
this.discoveryService);
this.discoveryService.servletControls = this.wunderGroundUpdateReceiverServlet;
Expand All @@ -70,7 +73,8 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {

if (THING_TYPE_UPDATE_RECEIVER.equals(thingTypeUID)) {
return new WundergroundUpdateReceiverHandler(thing, this.wunderGroundUpdateReceiverServlet,
this.discoveryService, this.channelTypeProvider, this.channelTypeRegistry);
this.discoveryService, this.channelTypeProvider, this.channelTypeRegistry,
this.managedThingProvider);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.openhab.core.config.core.Configuration;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
import org.openhab.core.thing.ManagedThingProvider;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandlerCallback;
Expand Down Expand Up @@ -129,8 +130,10 @@ void multipleIndexedParametersOfTheSameChanneltypeAreCorrectlyDiscovered() throw
Thing thing = ThingBuilder.create(SUPPORTED_THING_TYPES_UIDS.stream().findFirst().get(), TEST_THING_UID)
.withConfiguration(new Configuration(Map.of(REPRESENTATION_PROPERTY, REQ_STATION_ID)))
.withLabel("test thing").withLocation("location").build();
ManagedThingProvider managedThingProvider = mock(ManagedThingProvider.class);
when(managedThingProvider.get(TEST_THING_UID)).thenReturn(thing);
WundergroundUpdateReceiverHandler handler = new WundergroundUpdateReceiverHandler(thing, sut, discoveryService,
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry);
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry, managedThingProvider);
handler.setCallback(mock(ThingHandlerCallback.class));
handler.initialize();
sut.addHandler(handler);
Expand Down Expand Up @@ -173,8 +176,10 @@ void unregisteredChannelsAreAddedOnTheFlyWhenDiscovered() throws IOException {
Thing thing = ThingBuilder.create(SUPPORTED_THING_TYPES_UIDS.stream().findFirst().get(), TEST_THING_UID)
.withConfiguration(new Configuration(Map.of(REPRESENTATION_PROPERTY, REQ_STATION_ID)))
.withLabel("test thing").withLocation("location").build();
ManagedThingProvider managedThingProvider = mock(ManagedThingProvider.class);
when(managedThingProvider.get(any())).thenReturn(thing);
WundergroundUpdateReceiverHandler handler = new WundergroundUpdateReceiverHandler(thing, sut, discoveryService,
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry);
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry, managedThingProvider);
handler.setCallback(mock(ThingHandlerCallback.class));

// When
Expand Down Expand Up @@ -216,6 +221,70 @@ void unregisteredChannelsAreAddedOnTheFlyWhenDiscovered() throws IOException {
assertThat(actual, hasItems(expectedActual));
}

@Test
void unregisteredChannelsAreNotAddedOnUnmanagedThings() throws IOException {
// Given
final String firstDeviceQueryString = "ID=dfggger&" + "PASSWORD=XXXXXX&" + "tempf=26.1&" + "humidity=74&"
+ "dateutc=2021-02-07%2014:04:03&" + "softwaretype=WH2600%20V2.2.8&" + "action=updateraw&"
+ "realtime=1&" + "rtfreq=5";
MetaData.Request request1 = new MetaData.Request("GET", new HttpURI(
"http://localhost" + WundergroundUpdateReceiverServlet.SERVLET_URL + "?" + firstDeviceQueryString),
HttpVersion.HTTP_1_1, new HttpFields());
HttpChannel httpChannel = mock(HttpChannel.class);
Request req1 = new Request(httpChannel, null);
req1.setMetaData(request1);

TestChannelTypeRegistry channelTypeRegistry = new TestChannelTypeRegistry();
WundergroundUpdateReceiverDiscoveryService discoveryService = new WundergroundUpdateReceiverDiscoveryService(
true);
discoveryService.addUnhandledStationId(REQ_STATION_ID, req1.getParameterMap());
HttpService httpService = mock(HttpService.class);
WundergroundUpdateReceiverServlet sut = new WundergroundUpdateReceiverServlet(httpService, discoveryService);
Thing thing = ThingBuilder.create(SUPPORTED_THING_TYPES_UIDS.stream().findFirst().get(), TEST_THING_UID)
.withConfiguration(new Configuration(Map.of(REPRESENTATION_PROPERTY, REQ_STATION_ID)))
.withLabel("test thing").withLocation("location").build();
ManagedThingProvider managedThingProvider = mock(ManagedThingProvider.class);
when(managedThingProvider.get(any())).thenReturn(null);
WundergroundUpdateReceiverHandler handler = new WundergroundUpdateReceiverHandler(thing, sut, discoveryService,
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry, managedThingProvider);
handler.setCallback(mock(ThingHandlerCallback.class));

// When
handler.initialize();
sut.addHandler(handler);

// Then
ChannelTypeUID[] expectedBefore = new ChannelTypeUID[] { TEMPERATURE_CHANNELTYPEUID, HUMIDITY_CHANNELTYPEUID,
DATEUTC_CHANNELTYPEUID, SOFTWARETYPE_CHANNELTYPEUID, REALTIME_FREQUENCY_CHANNELTYPEUID,
LAST_QUERY_STATE_CHANNELTYPEUID, LAST_RECEIVED_DATETIME_CHANNELTYPEUID,
LAST_QUERY_TRIGGER_CHANNELTYPEUID };
List<ChannelTypeUID> before = handler.getThing().getChannels().stream().map(Channel::getChannelTypeUID)
.collect(Collectors.toList());
assertThat(before, hasItems(expectedBefore));

// When
final String secondDeviceQueryString = "ID=dfggger&" + "PASSWORD=XXXXXX&" + "lowbatt=1&" + "soilmoisture1=78&"
+ "soilmoisture2=73&" + "solarradiation=42.24&" + "dateutc=2021-02-07%2014:04:03&"
+ "softwaretype=WH2600%20V2.2.8&" + "action=updateraw&" + "realtime=1&" + "rtfreq=5";
MetaData.Request request = new MetaData.Request("GET", new HttpURI(
"http://localhost" + WundergroundUpdateReceiverServlet.SERVLET_URL + "?" + secondDeviceQueryString),
HttpVersion.HTTP_1_1, new HttpFields());
Request req2 = new Request(httpChannel, null);
req2.setMetaData(request);
sut.activate();

// Then
assertThat(sut.isActive(), is(true));

// When
sut.doGet(req2, mock(HttpServletResponse.class, Answers.RETURNS_MOCKS));

// Then
List<ChannelTypeUID> actual = handler.getThing().getChannels().stream().map(Channel::getChannelTypeUID)
.collect(Collectors.toList());
assertThat(actual, equalTo(before));
}

class TestChannelTypeRegistry extends ChannelTypeRegistry {

TestChannelTypeRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.ManagedThingProvider;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingUID;
Expand Down Expand Up @@ -92,6 +93,7 @@ class WundergroundUpdateReceiverServletTest {
private @Mock HttpService httpService;
private @Mock ChannelTypeRegistry channelTypeRegistry;
private @Mock WundergroundUpdateReceiverDiscoveryService discoveryService;
private @Mock ManagedThingProvider managedThingProvider;

@BeforeEach
public void setUp() {
Expand Down Expand Up @@ -231,7 +233,7 @@ void changedStationIdPropagatesToHandlerKey() throws ServletException, Namespace
.thenReturn(ChannelTypeBuilder.trigger(LAST_QUERY_TRIGGER_CHANNELTYPEUID, "Label").build());
WundergroundUpdateReceiverServlet sut = new WundergroundUpdateReceiverServlet(httpService, discoveryService);
WundergroundUpdateReceiverHandler handler = new WundergroundUpdateReceiverHandler(thing, sut, discoveryService,
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry);
new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry, managedThingProvider);
ThingHandlerCallback mockCallback = mock(ThingHandlerCallback.class);
handler.setCallback(mockCallback);

Expand Down Expand Up @@ -330,7 +332,8 @@ void aGetRequestIsCorrectlyParsed() throws IOException {
.withChannels(channels).withConfiguration(config).build();
ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
WundergroundUpdateReceiverHandler handler = new WundergroundUpdateReceiverHandler(testThing, sut,
discoveryService, new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry);
discoveryService, new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry,
managedThingProvider);
handler.setCallback(callback);
handler.initialize();

Expand Down Expand Up @@ -443,7 +446,8 @@ void aGetRequestWithIndexedParametresAreCorrectlyParsed() throws IOException {
.withChannels(channels).withConfiguration(config).build();
ThingHandlerCallback callback = mock(ThingHandlerCallback.class);
WundergroundUpdateReceiverHandler handler = new WundergroundUpdateReceiverHandler(testThing, sut,
discoveryService, new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry);
discoveryService, new WundergroundUpdateReceiverUnknownChannelTypeProvider(), channelTypeRegistry,
managedThingProvider);
handler.setCallback(callback);
handler.initialize();

Expand Down

0 comments on commit 5787544

Please sign in to comment.