-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[shelly] Tolerate third-party BLE script event data in NotifyEvent frames #21385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ML19821
wants to merge
3
commits into
openhab:main
Choose a base branch
from
ML19821:fix/shelly-notifyevent-data-shape
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...c/test/java/org/openhab/binding/shelly/internal/api2/Shelly2NotifyEventDataShapeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Copyright (c) 2010-2026 Contributors to the openHAB project | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0 | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| */ | ||
| package org.openhab.binding.shelly.internal.api2; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.CoreMatchers.nullValue; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| import org.eclipse.jdt.annotation.NonNullByDefault; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openhab.binding.shelly.internal.api.ShellyApiException; | ||
| import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2NotifyEvent; | ||
| import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2NotifyEventData; | ||
| import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2RpcNotifyEvent; | ||
| import org.openhab.binding.shelly.internal.api2.ShellyBluJsonDTO.Shelly2NotifyBluEventData; | ||
| import org.openhab.binding.shelly.internal.util.ShellyUtils; | ||
|
|
||
| import com.google.gson.Gson; | ||
|
|
||
| /** | ||
| * Tests for {@link Shelly2NotifyEvent#getBluData(Gson)} handling of the polymorphic {@code data} field. | ||
| * | ||
| * <p> | ||
| * The binding's own {@code oh-blu.*} scanner script always sends {@code data} as a JSON object, but Gen2/Gen3 | ||
| * devices running Home Assistant's BLE-proxy script emit {@code NotifyEvent} frames with event | ||
| * {@code ble.scan_result} where {@code data} is a JSON array. Before {@code data} became a raw | ||
| * {@link com.google.gson.JsonElement}, Gson threw {@code Expected BEGIN_OBJECT but was BEGIN_ARRAY} while parsing | ||
| * the whole frame, discarding it and flooding the log. | ||
| * </p> | ||
| * | ||
| * @author Martin Littkovsky - Initial contribution | ||
| */ | ||
| @NonNullByDefault | ||
| public class Shelly2NotifyEventDataShapeTest { | ||
|
|
||
| private final Gson gson = new Gson(); | ||
|
|
||
| @Test | ||
| void objectShapedDataParsesAndYieldsBluData() throws ShellyApiException { | ||
| String json = """ | ||
| {"src":"shellyplusht-test","dst":"ohshelly-test-1","method":"NotifyEvent", | ||
| "params":{"ts":1700000000.0,"events":[{"component":"script:1","id":1,"event":"oh-blu.data", | ||
| "data":{"encryption":false,"BTHome_version":2,"pid":42,"Battery":85, | ||
| "addr":"aa:bb:cc:dd:ee:01","rssi":-70},"ts":1700000000.0}]}} | ||
| """; | ||
|
|
||
| Shelly2NotifyEvent event = firstEvent(json); | ||
| assertThat(event.event, is(equalTo("oh-blu.data"))); | ||
|
|
||
| Shelly2NotifyBluEventData blu = event.getBluData(gson); | ||
| assertNotNull(blu); | ||
| assertThat(blu.addr, is(equalTo("aa:bb:cc:dd:ee:01"))); | ||
| assertThat(blu.battery, is(equalTo(85))); | ||
| assertThat(blu.pid, is(equalTo(42))); | ||
| } | ||
|
|
||
| @Test | ||
| void arrayShapedDataParsesWithoutExceptionAndYieldsNoBluData() throws ShellyApiException { | ||
| // Home Assistant BLE-proxy script style frame: "data" is [scanType, [[addr, rssi, advData, scanRsp], ...]] | ||
| String json = """ | ||
| {"src":"shellyplugsg3-aabbccddeeff","dst":"ohshelly-Test-1","method":"NotifyEvent", | ||
| "params":{"ts":1786653121.63,"events":[{"component":"script:1","id":1,"event":"ble.scan_result", | ||
| "data":[2,[["aa:bb:cc:dd:ee:01",-97,"AgEEAwMH/hT=",""],["aa:bb:cc:dd:ee:02",-86,"AgEGEQYbxdU=",""]]], | ||
| "ts":1786653121.63}]}} | ||
| """; | ||
|
|
||
| Shelly2NotifyEvent event = firstEvent(json); | ||
| assertThat(event.event, is(equalTo("ble.scan_result"))); | ||
| assertThat(event.getBluData(gson), is(nullValue())); | ||
| } | ||
|
|
||
| @Test | ||
| void absentDataYieldsNoBluDataWithoutException() throws ShellyApiException { | ||
| String json = """ | ||
| {"src":"shellyplusht-test","params":{"ts":1700000000.0, | ||
| "events":[{"component":"sys","id":0,"event":"config_changed","ts":1700000000.0}]}} | ||
| """; | ||
|
|
||
| Shelly2NotifyEvent event = firstEvent(json); | ||
| assertThat(event.event, is(equalTo("config_changed"))); | ||
| assertThat(event.data, is(nullValue())); | ||
| assertThat(event.getBluData(gson), is(nullValue())); | ||
| } | ||
|
|
||
| private Shelly2NotifyEvent firstEvent(String json) throws ShellyApiException { | ||
| Shelly2RpcNotifyEvent message = ShellyUtils.fromJson(gson, json, Shelly2RpcNotifyEvent.class); | ||
| Shelly2NotifyEventData params = message.params; | ||
| assertNotNull(params); | ||
| ArrayList<Shelly2NotifyEvent> events = params.events; | ||
| assertNotNull(events); | ||
| assertThat(events.size(), is(equalTo(1))); | ||
| return events.get(0); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.