Last active
September 24, 2021 22:01
-
-
Save devvyn/d728ed67526d6b7676740d732babfdb0 to your computer and use it in GitHub Desktop.
Philips Hue API - let's play
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Philips Hue API for Python\n", | |
| "\n", | |
| "## Getting Started\n", | |
| "\n", | |
| "Let's give this a try.\n", | |
| "Taking a look at the [Getting started] document on [developers.meethue.com],\n", | |
| "I see that the first task is getting on the same network as the Hue hub\n", | |
| "and confirming its connection and ours.\n", | |
| "\n", | |
| "### Finding the Hue bridge IP address\n", | |
| "\n", | |
| "Since I've confirmed my hub is already connected\n", | |
| "and I can control lights with my phone,\n", | |
| "I'll go on to step two in [Getting started],\n", | |
| "and begin finding the Hue hub on the local network.\n", | |
| "My choices are:\n", | |
| "\n", | |
| "> 1. Use a UPnP discovery app to find Philips hue in your network.\n", | |
| "> 2. Use our broker server discover process by visiting www.meethue.com/api/nupnp\n", | |
| "> 3. Log into your wireless router and look Philips hue up in the DHCP table.\n", | |
| "> 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\n", | |
| "\n", | |
| "Since I want to do this automatically in the future,\n", | |
| "I'll opt for using the broker server (option 2).\n", | |
| "\n", | |
| "[Getting started]: https://developers.meethue.com/documentation/getting-started\n", | |
| "[developers.meethue.com]: https://developers.meethue.com" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "\"\"\"Use the broker server at meethue.com to locate any Philips Hue hubs on the local network\n", | |
| "(that have been portal-enabled and connected from this local network).\"\"\"\n", | |
| "\n", | |
| "URL_FIND_BRIDGE_NUPNP = 'https://www.meethue.com/api/nupnp'\n", | |
| "\n", | |
| "import urllib.request, json\n", | |
| "\n", | |
| "def request_json(url):\n", | |
| " \"\"\"Returns parsed JSON from server\"\"\"\n", | |
| " return json.load(urllib.request.urlopen(url))\n", | |
| "\n", | |
| "def request_bridges():\n", | |
| " return request_json(URL_FIND_BRIDGE_NUPNP)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Now I can call `request_bridges()` to get a list:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "my_bridges = request_bridges()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "My bridge is the first bridge, 'cause I don't care:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'id': '001788fffe24d54e', 'internalipaddress': '10.0.1.4'}" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "my_bridge = my_bridges[0]\n", | |
| "\n", | |
| "my_bridge" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "I see the IP address is under the `internalipaddress` attribute. I can do things with it, such as use it in strings." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "'My bridge is at 10.0.1.4.'" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "f'My bridge is at {my_bridge[\"internalipaddress\"]}.'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "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].\n", | |
| "\n", | |
| "[Hue bridge discovery]: https://developers.meethue.com/documentation/hue-bridge-discovery" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'apiversion': '1.19.0',\n", | |
| " 'bridgeid': '001788FFFE24D54E',\n", | |
| " 'datastoreversion': '61',\n", | |
| " 'factorynew': False,\n", | |
| " 'mac': '00:17:88:24:d5:4e',\n", | |
| " 'modelid': 'BSB002',\n", | |
| " 'name': 'RingoTree',\n", | |
| " 'replacesbridgeid': None,\n", | |
| " 'starterkitid': '',\n", | |
| " 'swversion': '1705121051'}" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "request_json(f'http://{my_bridge[\"internalipaddress\"]}/api/config')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Great! Let's continue to [Create user] in the [Philips hue API].\n", | |
| "\n", | |
| "[Create user]: https://developers.meethue.com/documentation/configuration-api#71_create_user\n", | |
| "[Philips hue API]: https://developers.meethue.com/philips-hue-api" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.6.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could use an introduction that elucidates the purpose and expected outcome, to get the reader interested.