[wundergroundupdatereceiver] Check whether the thing is managed (#12998)

Ie. created in the UI or by discovery
Signed-off-by: Daniel Demus <daniel-github@demus.dk>
This commit is contained in:
Daniel Demus 2022-06-24 19:04:29 +02:00 committed by GitHub
parent b5d7d22c1f
commit ccbc6a8f11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 9 deletions

View File

@ -33,6 +33,7 @@ import org.openhab.core.library.types.StringType;
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;
@ -63,6 +64,7 @@ public class WundergroundUpdateReceiverHandler extends BaseThingHandler {
private final WundergroundUpdateReceiverDiscoveryService discoveryService;
private final WundergroundUpdateReceiverUnknownChannelTypeProvider channelTypeProvider;
private final ChannelTypeRegistry channelTypeRegistry;
private final ManagedThingProvider managedThingProvider;
private final ChannelUID dateutcDatetimeChannel;
private final ChannelUID lastReceivedChannel;
@ -75,11 +77,12 @@ public class WundergroundUpdateReceiverHandler extends BaseThingHandler {
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);
@ -205,7 +208,8 @@ public class WundergroundUpdateReceiverHandler extends BaseThingHandler {
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());

View File

@ -16,6 +16,7 @@ import static org.openhab.binding.wundergroundupdatereceiver.internal.Wundergrou
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;
@ -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;
@ -70,7 +73,8 @@ public class WundergroundUpdateReceiverHandlerFactory extends BaseThingHandlerFa
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;

View File

@ -41,6 +41,7 @@ import org.mockito.Answers;
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;
@ -129,8 +130,10 @@ class WundergroundUpdateReceiverDiscoveryServiceTest {
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);
@ -173,8 +176,10 @@ class WundergroundUpdateReceiverDiscoveryServiceTest {
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
@ -216,6 +221,70 @@ class WundergroundUpdateReceiverDiscoveryServiceTest {
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() {

View File

@ -65,6 +65,7 @@ import org.openhab.core.library.unit.ImperialUnits;
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;
@ -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() {
@ -231,7 +233,7 @@ class WundergroundUpdateReceiverServletTest {
.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);
@ -330,7 +332,8 @@ class WundergroundUpdateReceiverServletTest {
.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();
@ -443,7 +446,8 @@ class WundergroundUpdateReceiverServletTest {
.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();