Audit external integration Philips Hue

Hi @cicoub13!

First of all, thanks for the Philips Hue integration, the work is really clean :clap:

I did a code audit to compare it with Home Assistant’s Hue integration, especially on the compatibility side. Everything is detailed in this issue:

An urgent point: since firmware 1.78, which is currently being deployed, Hue v2 bridges refuse HTTP and respond with a 403 error « HTTP access is blocked ». However, the integration chooses HTTP or HTTPS once, at the time of pairing, and tries HTTP first. Almost all existing installations are therefore in HTTP: as soon as the bridge updates, no commands go through anymore.

  • In the meantime, simply re-pair the bridge.
  • The fix does not require new pairing: just try again in HTTPS with the same key.

To reach Home Assistant’s level, three things are mainly missing:

  • the v2 API with the event stream, to have real-time state changes instead of a refresh every 60 seconds;
  • sensors (motion, brightness, temperature, opening) and remotes;
  • Hue rooms and zones.

All of this can be represented in Gladys.

There are also very good points: on HTTPS pairing and certificate verification, the integration even does better than Home Assistant. The issue ends with a proposal for a roadmap in 4 phases.

It’s a code review, I haven’t tested anything on a real bridge. Don’t hesitate if a point seems wrong to you or if you want to discuss it!

Thank you so much for this audit, it is very comprehensive and saved me a lot of time :folded_hands:

The entire phase 0 of the roadmap is corrected and published in the v2.1.0 :

  • :red_circle: Firmware 1.78 blocking HTTP : a bridge paired in HTTP automatically switches to HTTPS, with the same key, without pressing the button or re-pairing. New pairings try HTTPS first. If your bridge has updated recently and your lights no longer respond, update the integration.
  • Lamp turned off at the switch : it now appears off in Gladys, with an “unreachable” badge on the Devices page. Scene conditions of the type “if the lamp is on” become accurate again.
  • New “Forget Unreachable Bridges” button, to clean up after a bridge replacement or migration to a Hue Bridge Pro. A bridge that is simply busy or restarting is never forgotten.
  • The color no longer darkens when lowering the brightness : a dimmed warm white no longer appears brown.
  • The documentation now explains what to do when the bridge IP changes, and how to replace a bridge.

The details are in this issue comment. The remaining small points are tracked in #51.

I haven’t been able to test everything on real hardware yet. Feedback is therefore welcome, especially if you have a bridge with firmware 1.78 or a Hue Bridge Pro.

Next up is phase 1: switching to API v2, real-time updates instead of polling, and lights that keep their devices after a bridge replacement.

Phase 1 of the roadmap is published in the v2.2.0 release:

  • :high_voltage: Real-time updates: with a square bridge (firmware 1.48 or newer) or a Hue Bridge Pro, the integration receives the event stream from the API v2. A change made in the Hue app, with a dimmer, a wall switch, or a Hue automation appears immediately in Gladys. Before, you had to wait for the next refresh, up to one minute. Periodic refresh remains active for security and is used for the round bridge. A bridge paired in HTTP automatically switches to HTTPS to take advantage of this, without re-pairing.
  • Gladys scenes that control many lamps no longer lose commands: requests to a bridge are spread out, and retried when the bridge says it is busy.
  • Bridge changing IP address: it is found automatically on the network, without re-pairing. „Discover bridges“ also finds it immediately. The only exception is a bridge still paired in simple HTTP, like the round bridge, because there is no way to prove it is the same bridge at the new address. You then need to re-pair it with the button, and its lamps keep their devices.
  • Migration to a Hue Bridge Pro or bridge replacement: after pairing the new bridge, the integration recognizes that it carries the lamps of the old one. The already created devices continue to work, with their rooms, scenes, and history. Before, all lamps came back as new devices. The button of the new bridge remains necessary: for security, the key of the old one is never sent to a bridge that nothing authenticates.
  • „Discover bridges“ finds all bridges on the network, including a Pro Bridge next to a square bridge. Discovery goes through mDNS then SSDP and merges the two results. Before, it stopped at the first method that found something.
  • „Pair the bridge“ on an already paired bridge reuses its key, without a button, instead of creating a new user on the bridge.
  • Some robustness fixes, for example for two bridges at the same IP address, or a clock set late at startup.

The details are in this issue comment. The points of #51 are resolved, except for two known limitations.

For now, the API v2 is only used for real-time. Lamps are still read and controlled in v1, which ensures that existing devices do not change their identifier.

None of this has been tested on a real bridge yet, so your feedback is welcome. It is particularly useful if you have a Hue Bridge Pro, if you have done a migration, or if you can check real-time on a square bridge.

Next up is phase 2: Hue rooms and zones, sensors (motion, brightness, temperature, opening), remotes, and the Tap Dial, then effects.