Skip to content
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

[hue] Fix NUPnP discovery #14871

Merged
merged 2 commits into from Apr 22, 2023
Merged

Conversation

jlaur
Copy link
Contributor

@jlaur jlaur commented Apr 22, 2023

Fixes #14852
Regression of #11842

@jlaur jlaur added the bug An unexpected problem or unintended behavior of an add-on label Apr 22, 2023
@jlaur jlaur requested a review from cweitkamp as a code owner April 22, 2023 14:49
@jlaur jlaur requested a review from a team April 22, 2023 14:49
@jlaur
Copy link
Contributor Author

jlaur commented Apr 22, 2023

Please consider this for being backported to 3.4.x.

@jlaur jlaur force-pushed the 14852-hue-fix-nupnp-discovery branch from 54afadb to 12a9d5c Compare April 22, 2023 15:12
Fixes openhab#14852

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
@jlaur jlaur force-pushed the 14852-hue-fix-nupnp-discovery branch from 12a9d5c to 49cbb14 Compare April 22, 2023 15:13
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Copy link
Contributor

@lolodomo lolodomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lolodomo lolodomo merged commit b21913f into openhab:main Apr 22, 2023
3 checks passed
@lolodomo lolodomo added this to the 4.0 milestone Apr 22, 2023
@jlaur jlaur deleted the 14852-hue-fix-nupnp-discovery branch April 29, 2023 09:37
@andrewfg
Copy link
Contributor

@jlaur / @lolodomo as with all ongoing PRs for this binding, your #14871 is merged into my #13570 .. however I have a question for you both concerning that..

Background: when mDNS discovery is used, then the discovery service checks if the respective bridge supports API v2 (it makes an API v2 GET call which return 200 OK if API v2 is supported). And if it is supported it puts an API v2 thing in the inbox (and NOT a legacy API v1 thing). Reason is that for newly discovered things it is better for the user to start with the new technology and not the legacy.

Now that you have re-enabled NUPnP discovery via this PR, the (legacy) mechanism once more puts a legacy thing in the inbox. So now if the bridge supports API v2, the mDNS discovery puts a v2 thing in the inbox, and the legacy NUPnP discovery puts a second (legacy) thing in the inbox. The risk of this is that the user might click on the legacy bridge thing, and start creating a whole system based mistakenly on legacy stuff.

There are two possible solutions..

  1. Either I do the same with NUPnP discovery as I have done with mDNS i.e. it checks if the discovered thing supports API v2 and depending on the result it puts either a v2 or a legacy thing in the inbox.
  2. Or I disable the NUPnP discovery again..
  3. Or I do nothing..

I suppose 1. is the right answer, but perhaps you can confirm? Note in case of 1. if the bridge really does not support API v2 then a legacy thing will anyway be placed in the inbox (via either mDNS or NUPnP or both).

@lolodomo
Copy link
Contributor

In fact, I am not sure what is and what was the interest of the NUPnP cloud discovery ?

@lolodomo
Copy link
Contributor

By the way, we need to include again the UPnP discovery class (removed in OH3) as there is no more discovery of old bridge with the current code. I hope the old bridge will still expose itself through UPnP.

@jlaur
Copy link
Contributor Author

jlaur commented Apr 29, 2023

  1. Either I do the same with NUPnP discovery as I have done with mDNS i.e. it checks if the discovered thing supports API v2 and depending on the result it puts either a v2 or a legacy thing in the inbox.
  2. Or I disable the NUPnP discovery again..

It was not disabled, it was broken - and by that of course indirectly disabled.

  1. Or I do nothing..

I suppose 1. is the right answer, but perhaps you can confirm? Note in case of 1. if the bridge really does not support API v2 then a legacy thing will anyway be placed in the inbox (via either mDNS or NUPnP or both).

See #14852 (comment) - asking the same question.

I don't understand why we have an implementation of cloud discovery when the binding works locally and we have local discovery options. So I would opt for completely removing the NUPnP discovery, unless someone steps up and explains why there would be a need for this.

@andrewfg
Copy link
Contributor

andrewfg commented Apr 29, 2023

In the meantime I made this commit but we can revert it later if you wish..

don't understand why we have an implementation of cloud discovery

Good point.

The following is not directly related to discovery, but may nevertheless be relevant..

Currently there are two ways to authenticate OH on the bridge -- namely locally (press button on hub) to get an application key, and via cloud V1 OAuth2 authentication. Philips / Signify implies that a local application key is not very secure, and they imply that better security can be had from using OAuth2 cloud authentication. Notwithstanding this, Philips / Signify have just announced that V1 OAuth2 will be discontinued on July 1st 2023 and say 'in the future there will be another way to authorize on the V2 API'. So, I interpret all of this to mean that Philips / Signify is planning to introduce some API v2 OAuth2 cloud based work flow with a username and password whereby the application key would probably become time limited in some way. This all just for info..

https://developers.meethue.com/v1-oauth2-to-be-disabled-on-july-1-2023/

EDIT: 4yi I have not implemented API v2 OAuthV2 in my PR because, I don't really understand how OAuth2 works :(

@kaikreuzer
Copy link
Member

I don't understand why we have an implementation of cloud discovery when the binding works locally and we have local discovery options.

Afair the history, the reason for the introduction was that it there were quite some users where local UPnP discovery didn't work as expected - be it due to different local subnets, blocked UPnP services IPv6 or whatsoever. So the as the cloud discovery was offered by Philips (and the hue app was using it as a fallback as well), this was also implemented in openHAB as a secondary discovery option.

@jlaur
Copy link
Contributor Author

jlaur commented Apr 30, 2023

Afair the history, the reason for the introduction was that it there were quite some users where local UPnP discovery didn't work as expected - be it due to different local subnets, blocked UPnP services IPv6 or whatsoever. So the as the cloud discovery was offered by Philips (and the hue app was using it as a fallback as well), this was also implemented in openHAB as a secondary discovery option.

Thanks for sharing this, @kaikreuzer. So we should keep this.

@lolodomo
Copy link
Contributor

lolodomo commented May 1, 2023

I am going to restore UPnP discovery for old bridges but I would like to do it only for old bridges.
Here is the data exposed by my hue bridge through UPnP:
image
What field should I consider to filter only old bridges ? The year in "Model name" ? Model number ? Something from the serial number ?
Or should I filter nothing because all recent bridges do not expose themself through UPnP ?

@andrewfg
Copy link
Contributor

andrewfg commented May 1, 2023

I will run Device Spy on mine, and let you know what I get.

@andrewfg
Copy link
Contributor

andrewfg commented May 1, 2023

Here you go. There seem to be quite a few potential markers that you could use.

2023-05-01_12-45-25

@lolodomo
Copy link
Contributor

lolodomo commented May 1, 2023

Ok so UPnP is still available for recent bridges. In that case, I have to apply a filter.
"Model number" seems to be the ideal field. WDYT if I ignore bridges when the model number starts with "B" ?

@lolodomo
Copy link
Contributor

lolodomo commented May 1, 2023

I submitted a PR. Of course it is tested and working with a bridge V1. And it is expected to not discover bridge V2.

I can see that the model id retrieved from API is BSB001 in my case (the modelId property is updated when the binding starts) while "Model number" in UPnP discovery is a different value.
For more recent bridges, it looks like BSB002 is probably the value in both places.

@andrewfg
Copy link
Contributor

andrewfg commented May 1, 2023

There might be problems if they would release a new bridge with a model number not starting with B. So perhaps do a Regex search for a year in model name, and only search if it is below 2015?
Assuming BSB001 was 2012 and BSB002 was 2015.

@lolodomo
Copy link
Contributor

lolodomo commented May 1, 2023

There might be problems if they would release a new bridge with a model number not starting with B. So perhaps do a Regex search for a year in model name, and only search if it is below 2015?
Assuming BSB001 was 2012 and BSB002 was 2015.

Done, I believe it is a better option.

@jlaur jlaur mentioned this pull request May 1, 2023
@jlaur
Copy link
Contributor Author

jlaur commented May 1, 2023

So the as the cloud discovery was offered by Philips (and the hue app was using it as a fallback as well), this was also implemented in openHAB as a secondary discovery option.

Getting back to NUPnP as "secondary" discovery option, I noticed that the label for the discovery result changed back and forth when scanning (XXXXXX is last 6 characters of the serial number):

  • Philips Hue - XXXXXX (192.168.0.251)
  • Philips Hue (192.168.0.251)

I added some more logging:

2023-05-01 21:54:54.859 [INFO ] [al.discovery.HueBridgeNupnpDiscovery] - Hue bridge discovered by NUPnP
2023-05-01 21:54:56.027 [INFO ] [ry.HueBridgeMDNSDiscoveryParticipant] - Hue bridge discovered by mDNS

So it seems that the two discovery results are fighting. It shouldn't be a real issue except the flickering caused by the label switching. It should be possible to make the labels consistent, I'll have a look at that.

FYI @kaikreuzer, @lolodomo, @andrewfg

@andrewfg
Copy link
Contributor

andrewfg commented May 2, 2023

flickering caused by the label switching. It should be possible to make the labels consistent

Good point. Obviously the most important thing is the representationProperty which must be identical otherwise othwerwise one gets two things 'flickering' rather than one thing with its label 'flickering..

@lolodomo
Copy link
Contributor

lolodomo commented May 2, 2023

flickering caused by the label switching. It should be possible to make the labels consistent

Good point. Obviously the most important thing is the representationProperty which must be identical otherwise othwerwise one gets two things 'flickering' rather than one thing with its label 'flickering..

But I believe the configuration parameters should also be the same ?
And even with identical label and configuration parameters, I guess it will trigger an update of the thing (if the thing was added from inbox without changing its id. So two discoveries will trigger two thing updates.

@andrewfg
Copy link
Contributor

andrewfg commented May 2, 2023

believe the configuration parameters should also be the same
guess it will trigger an update of the thing (if the thing was added from inbox without changing its id)

Hmm. I don't think so -> https://www.openhab.org/docs/developer/bindings/thing-xml.html#representation-property-and-discovery

EDIT: oops -- I see this "and both the properties and the configuration parameters of existing Things"

@andrewfg
Copy link
Contributor

andrewfg commented May 13, 2023

@jlaur there is unfortunately still a bug here. As you know I synchronized your PR into my own API v2 PR, and two of the people who are running that version reported that the nupnp discovery crashes with the stack trace shown below.

The issue is that the getBridgeList() method calls doGetRequest() and this does not return HTTP 200 'Ok' containing a valid JSON content, but instead returns HTTP 429 'too many requests' and no JSON content. And that in turn causes the Objects.requireNonNull(..) check to fail.

One of the two users also cross checked it by opening the https://discovery.meethue.com/ link in his browser (from inside his LAN), and you can see the 429 reported by the browser.

image

12:56:53.733 [WARN ] [ommon.WrappedScheduledExecutorService] - Scheduled runnable ended with an exception:
java.lang.NullPointerException: null
        at java.util.Objects.requireNonNull(Objects.java:208) ~[?:?]
        at org.openhab.binding.hue.internal.discovery.HueBridgeNupnpDiscovery.getBridgeList(HueBridgeNupnpDiscovery.java:146) ~[?:?]
        at org.openhab.binding.hue.internal.discovery.HueBridgeNupnpDiscovery.discoverHueBridges(HueBridgeNupnpDiscovery.java:75) ~[?:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]

@openhab-bot
Copy link
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/hue-binding-with-added-support-for-api-v2/146648/10

@jlaur
Copy link
Contributor Author

jlaur commented May 13, 2023

there is unfortunately still a bug here

Thanks, fixed in #14989.

matchews pushed a commit to matchews/openhab-addons that referenced this pull request Aug 9, 2023
* Fix NUPnP discovery

Fixes openhab#14852

* Declare hybrid connection due to cloud discovery

---------

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Matt Myers <mmyers75@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[hue] NUPnP discovery broken
5 participants