Skip to content

Instantly share code, notes, and snippets.

@kfsone
Created June 12, 2017 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfsone/0c923f62672ed258319095c5bb468b38 to your computer and use it in GitHub Desktop.
Save kfsone/0c923f62672ed258319095c5bb468b38 to your computer and use it in GitHub Desktop.
Simple demonstration of talking to a hue hub to control a lamp.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import requests\n",
"\n",
"hub = \"philips.lan\" # or provide the ip address\n",
"appname = \"ipytest\" # call it what you like\n",
"\n",
"username = None\n",
"\n",
"\n",
"def query(query_path, body=None):\n",
" if body is None:\n",
" result = requests.get(\"http://\" + hub + query_path)\n",
" else:\n",
" result = requests.post(\"http://\" + hub + query_path, json=body)\n",
" return result.json()\n",
"\n",
"def update(query_path, body=None):\n",
" result = requests.put(\"http://\" + hub + query_path, json=body)\n",
" return result.json()\n",
"\n",
"def register():\n",
" global username # Because we are going to modify it.\n",
" while username is None:\n",
" result = query(\"/api\", {'devicetype': appname})\n",
" status = result[0]\n",
" error = status.get('error')\n",
" if error:\n",
" # Check for code 101, not authorized\n",
" if error['type'] == 101:\n",
" input(\"NOT AUTHORIZED! \"\n",
" \"Press the button on your hub and hit return.\")\n",
" continue\n",
" raise Exception(\"Unknown error from hub: \" + str(error))\n",
"\n",
" success = status['success']\n",
" username = success['username']\n",
" print(\"* Registered as\", username)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def hue_query(query_path, body=None):\n",
" register()\n",
" full_path = \"/api/\" + username + query_path\n",
" return query(full_path, body)\n",
"\n",
"def hue_update(query_path, body=None):\n",
" register()\n",
" full_path = \"/api/\" + username + query_path\n",
" return update(full_path, body)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Find the light you want...\n",
"for lightId, light in hue_query(\"/lights\").items():\n",
" print(\"%04s = %s\" % (lightId, light[\"name\"]))\n",
"\n",
"lamp = str(input(\"Enter the ID (from the left) of the light you want: \"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pprint import pprint\n",
"pprint(hue_query(\"/lights/\"+lamp)['state'])\n",
"pprint(hue_update(\"/lights/\"+lamp+\"/state\", body={'on': True}))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"time.sleep(5)\n",
"# quick on and off\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"on\": True, \"bri\":254, \"sat\":0, \"transitiontime\":0})\n",
"time.sleep(3)\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"on\": False, \"transitiontime\":0})\n",
"time.sleep(4)\n",
"\n",
"# slow on and off\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"on\": True, \"bri\":254, \"sat\":0, \"transitiontime\":15})\n",
"time.sleep(3)\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"on\": False, \"bri\":0, \"sat\":0, \"hue\":1000, \"transitiontime\":15})\n",
"time.sleep(4)\n",
"\n",
"# some color effects\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"hue\":30000, \"transitiontime\":0}) # set the initial hue\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"on\": True, \"bri\":254, \"sat\":254, \"hue\":60000, \"transitiontime\":50})\n",
"time.sleep(5)\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"hue\":1000, \"transitiontime\":50})\n",
"time.sleep(5)\n",
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"ct\": 153, \"transitiontime\":50})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hue_update(\"/lights/\"+lamp+\"/state\", body={\"on\": False})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.4.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment