Skip to content

Commit

Permalink
Merge pull request ansible#252 from IPvSean/IPvSean/2019demo
Browse files Browse the repository at this point in the history
uploading demo of phillips hue
  • Loading branch information
IPvSean authored May 22, 2019
2 parents a319fc1 + ff882ce commit 945b1c5
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 0 deletions.
71 changes: 71 additions & 0 deletions phillips_hue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## HUE LIGHTS WITH ANSIBLE

This README written on September 5th, 2018 by [Sean Cavanaugh](https://github.com/ipvsean). The original username feature seems to have been disabled since James Cammarata wrote the original modules and URI based Playbooks in 2016. I updated my Phillips Hue Bulbs to the latest firmware as of this post and made some new Playbooks based off the original Cammarata URI playbooks :)

## PREREQUISITES

- Ansible
- [Install Guide](https://www.amazon.com/gp/product/B07D1J5QC7/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1)
- Phillips Hue Lights
- The kit used for this particular demo was a Philips Hue White and Color Ambiance A19 60W Equivalent LED Smart Light Bulb Starter Kit. The link on Amazon can [be found here](https://www.amazon.com/gp/product/B07D1J5QC7/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1)
- IP Address of the Hue Controller (known as the bridge).
- Most home networks will allow you to pin a static IP address to the Hue bridge. I use Google Wifi and this took a couple clicks on my iPhone.

Also make sure that you can control the lights normally from the free iPhone or Android app. This may require you adding the serial numbers manually which can be found on the side of the bulbs.

## SETUP

### Step 1

Open up the username_info.yml file and change the IP Address to the IP address of your Hue bridge.

```nano username_info.yml```

```yaml
---
username: FmAXS-XpnLIxBQwyaw1tkIw04YzIt-BIG4YL0v8X
ip_address: "192.168.86.30"
body_info:
devicetype: "Ansible!"
```
Ignore the `username` and `body_info` fields. The `username` is automatically generated by the Hue bridge and updated in this file in the following step. The `body_info` is just used to register this particular computer (the computer you are executing Ansible from) in the following step, the `devicetype` could be anything. Please refer to the [getting started guide](https://www.developers.meethue.com/documentation/getting-started) from Phillips for more information.

### Step 2

For the Playbooks to work correctly you must run the `register.yml` playbook first->

```ansible-playbook register.yml```

The playbook will run and then prompt you on the terminal window->
```
[PROMPT USER TO PRESS PHYSICAL BUTTON HUE HUB]
Press the button on the hub now...:
```
You must physically touch the button on the Hue bridge (the top of it where the word PHILLIPS is clearly printed) as a security measure. Press enter on the terminal window after you have pressed the Hue bridge button.
This will save a unique Hue generated authorized user to `username_info` which will look like a long string of text (e.g. `elY1xx9p5twUBYDjELgMUuQT99kLaVqGT1p0eDrl`).
## PLAYBOOKS
There are three demo playbooks included. All three of them use the [include_vars](https://docs.ansible.com/ansible/latest/modules/include_vars_module.html) task to grab the IP address and username information from `username_info.yml`. Run them with `ansible-playbook <name>.yml` e.g. `ansible-playbook on_off.yml`.
- `on_off.yml `
This playbook turns off all bulbs that are registered to the Hue bridge. It then prompts the user, and then turns them back on.
- `ansible_colors.yml `
This playbook cycles all bulbs between Ansible mango and Ansible pool a couple times You can find the [official Ansible colors and logos here](https://www.ansible.com/logos).
- `effect.yml`
This playbook takes all bulbs and puts them into a mode called colorloop where the bulbs will randomly cycle colors. This will happen for 5 seconds then it will turn off the effect.
## DEMONSTRATION
![hue screencast](hue.gif)
## License
GPLv3
22 changes: 22 additions & 0 deletions phillips_hue/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# config file for ansible -- http://ansible.com/
# ==============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

inventory = hosts
forks = 50
host_key_checking = False
retry_files_enabled = False
no_target_syslog = False
callback_whitelist = time

[ssh_connection]
scp_if_ssh = True
51 changes: 51 additions & 0 deletions phillips_hue/ansible_colors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
- hosts: localhost
gather_facts: no
connection: local
vars:
ansible_mango:
"on": true
"bri": 254
"xy": [0.5701, 0.313]
ansible_pool:
"on": true
"bri": 254
"xy": [0.1593, 0.2522]
tasks:
- name: INCLUDE UNIQUE USERNAME FROM REGISTER.YML
include_vars:
file: username_info.yml

- name: GRAB HUE LIGHT INFORMATION
uri:
url: "http://{{ip_address}}/api/{{username}}"
method: GET
body: '{{body_info|to_json}}'
register: light_info

- name: TURN LIGHTS TO MANGO
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{ansible_mango|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"

- name: TURN LIGHTS TO POOL
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{ansible_pool|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"

- name: TURN LIGHTS TO MANGO
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{ansible_mango|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"

- name: TURN LIGHTS TO POOL
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{ansible_pool|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"
39 changes: 39 additions & 0 deletions phillips_hue/effect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
- hosts: localhost
gather_facts: no
connection: local
vars:
ansible_effect:
"on": true
"effect": "colorloop"
ansible_none:
"on": true
"effect": "none"
tasks:
- name: INCLUDE UNIQUE USERNAME FROM REGISTER.YML
include_vars:
file: username_info.yml

- name: GRAB HUE LIGHT INFORMATION
uri:
url: "http://{{ip_address}}/api/{{username}}"
method: GET
body: '{{body_info|to_json}}'
register: light_info

- name: TURN LIGHTS INTO COLORLOOP EFFECT
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{ansible_effect|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"

# Pause for 10 seconds
- pause:
seconds: 5

- name: TURN LIGHTS INTO COLORLOOP EFFECT
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{ansible_none|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"
1 change: 1 addition & 0 deletions phillips_hue/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
localhost
Binary file added phillips_hue/hue.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions phillips_hue/on_off.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
- hosts: localhost
gather_facts: no
connection: local

vars:
off_state:
"on": false
on_state:
"on": true

tasks:
- name: INCLUDE UNIQUE USERNAME FROM REGISTER.YML
include_vars:
file: username_info.yml

- name: GRAB HUE LIGHT INFORMATION
uri:
url: "http://{{ip_address}}/api/{{username}}"
method: GET
body: '{{body_info|to_json}}'
register: light_info

- name: PRINT DATA TO TERMINAL WINDOW
debug:
var: light_info.json.lights

- name: PRINT AMOUNT OF LIGHTS TO TERMINAL WINDOW
debug:
msg: "THERE ARE {{light_info.json.lights | length}} HUE LIGHTS PRESENT"

# - name: PRINT OUT LOOP VARS
# debug:
# msg: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
# loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"

- name: TURN LIGHTS OFF
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{off_state|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"

- name: PROMPT USER TO TURN BACK ON
pause:
prompt: "Turn them back on?"

- name: TURN LIGHTS ON
uri:
url: "http://{{ip_address}}/api/{{username}}/lights/{{item}}/state"
method: PUT
body: '{{on_state|to_json}}'
loop: "{{ range(1, light_info.json.lights | length + 1)|list }}"
29 changes: 29 additions & 0 deletions phillips_hue/register.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- hosts: localhost
gather_facts: no
connection: local

tasks:

- name: PROMPT USER TO PRESS PHYSICAL BUTTON HUE HUB
pause:
prompt: "Press the button on the hub now..."

- name: INCLUDE IP ADDRESS FROM username_info.yml
include_vars:
file: username_info.yml

- name: GRAB UNIQUE USERNAME
uri:
url: "http://{{ip_address}}/api"
method: POST
body: '{{body_info|to_json}}'
register: username_info

- name: PRINT DATA TO TERMINAL WINDOW
debug:
var: username_info.json
- lineinfile:
path: "./username_info.yml"
regexp: '^username'
insertafter: EOF
line: 'username: {{username_info.json[0]["success"]["username"]}}'
5 changes: 5 additions & 0 deletions phillips_hue/username_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
username: elY1xx9p5twUBYDjELgMUuQT99kLaVqGT1p0eDrl
ip_address: "192.168.86.30"
body_info:
devicetype: "Ansible!"

0 comments on commit 945b1c5

Please sign in to comment.