Philips Hue API for Python

Getting Started

Let's give this a try. Taking a look at the Getting started document on developers.meethue.com, I see that the first task is getting on the same network as the Hue hub and confirming its connection and ours.

Finding the Hue bridge IP address

Since I've confirmed my hub is already connected and I can control lights with my phone, I'll go on to step two in Getting started, and begin finding the Hue hub on the local network. My choices are:

  1. Use a UPnP discovery app to find Philips hue in your network.
  2. Use our broker server discover process by visiting www.meethue.com/api/nupnp
  3. Log into your wireless router and look Philips hue up in the DHCP table.
  4. Hue App method: Download the official Philips hue app. Connect your phone to the network the hue bridge is on. Start the hue app(iOS described here). Push link connect to the bridge. Use the app to find the bridge and try controlling lights. All working -- Go to the settings menu in the app. Go to My Bridge. Go to Network settings. Switch off the DHCP toggle. The ip address of the bridge will show. Note the ip address, then switch DHCP back on

Since I want to do this automatically in the future, I'll opt for using the broker server (option 2).

In [1]:
"""Use the broker server at meethue.com to locate any Philips Hue hubs on the local network
(that have been portal-enabled and connected from this local network)."""

URL_FIND_BRIDGE_NUPNP = 'https://www.meethue.com/api/nupnp'

import urllib.request, json

def request_json(url):
    """Returns parsed JSON from server"""
    return json.load(urllib.request.urlopen(url))

def request_bridges():
    return request_json(URL_FIND_BRIDGE_NUPNP)

Now I can call request_bridges() to get a list:

In [2]:
my_bridges = request_bridges()

My bridge is the first bridge, 'cause I don't care:

In [3]:
my_bridge = my_bridges[0]

my_bridge
Out[3]:
{'id': '001788fffe24d54e', 'internalipaddress': '10.0.1.4'}

I see the IP address is under the internalipaddress attribute. I can do things with it, such as use it in strings.

In [4]:
f'My bridge is at {my_bridge["internalipaddress"]}.'
Out[4]:
'My bridge is at 10.0.1.4.'

If the IP address represents a hub, we can confirm it's online and connected by calling the config API, as suggested in Hue bridge discovery.

In [5]:
request_json(f'http://{my_bridge["internalipaddress"]}/api/config')
Out[5]:
{'apiversion': '1.19.0',
 'bridgeid': '001788FFFE24D54E',
 'datastoreversion': '61',
 'factorynew': False,
 'mac': '00:17:88:24:d5:4e',
 'modelid': 'BSB002',
 'name': 'RingoTree',
 'replacesbridgeid': None,
 'starterkitid': '',
 'swversion': '1705121051'}

Great! Let's continue to Create user in the Philips hue API.