Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #33 from bazwilliams/hue-support
Browse files Browse the repository at this point in the history
Add Philips Hue action type
  • Loading branch information
ensonic committed May 24, 2017
2 parents 28a0228 + 70b8735 commit 9571936
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[run]
branch = True
source = src

[report]
exclude_lines =
if self.debug:
pragma: no cover
raise NotImplementedError
if __name__ == .__main__.:
ignore_errors = True
omit =
tests/*
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ google-assistant-sdk[auth_helpers]==0.1.0
grpc-google-cloud-speech-v1beta1==0.14.0
protobuf==3.1.0
configargparse==0.11.0

phue==0.9
rgbxy==0.5
41 changes: 41 additions & 0 deletions src/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import logging
import subprocess

import phue
from rgbxy import Converter

import actionbase

# =============================================================================
Expand Down Expand Up @@ -194,6 +197,44 @@ def run(self, voice_command):
self.say(to_repeat)


# Example: Change Philips Light Color
# ====================================
#
# This example will change the color of the named bulb to that of the
# HEX RGB color and respond with 'ok'
#
# actor.add_keyword(_('change to ocean blue'), \
# ChangeLightColor(say, "philips-hue", "Lounge Lamp", "0077be"))

class ChangeLightColor(object):

"""Change a Philips Hue bulb color."""

def __init__(self, say, bridge_address, bulb_name, hex_color):
self.converter = Converter()
self.say = say
self.hex_color = hex_color
self.bulb_name = bulb_name
self.bridge_address = bridge_address

def run(self):
bridge = self.find_bridge()
if bridge:
light = bridge.get_light_objects("name")[self.bulb_name]
light.on = True
light.xy = self.converter.hex_to_xy(self.hex_color)
self.say(_("Ok"))

def find_bridge(self):
try:
bridge = phue.Bridge(self.bridge_address)
bridge.connect()
return bridge
except phue.PhueRegistrationException:
logging.info("hue: No bridge registered, press button on bridge and try again")
self.say(_("No bridge registered, press button on bridge and try again"))


# =========================================
# Makers! Implement your own actions here.
# =========================================
Expand Down
63 changes: 63 additions & 0 deletions tests/test_change_light_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

"""Test the change light color action."""

import mock
import unittest

import phue
import action

action._ = lambda s: s


class TestChangeLightColor(unittest.TestCase):

def _say(self, text):
self._say_text = text

def setUp(self):
self._say_text = None

@mock.patch("action.phue")
def test_change_light_color_no_bridge(self, mockedPhue):
mockedPhue.PhueRegistrationException = phue.PhueRegistrationException
bridge = mock.MagicMock()
bridge.connect.side_effect = mockedPhue.PhueRegistrationException(0, "error")
mockedPhue.Bridge.return_value = bridge

action.ChangeLightColor(self._say, "philips-hue", "Lounge Lamp", "0077be").run()

self.assertEqual(self._say_text,
"No bridge registered, press button on bridge and try again")

@mock.patch("action.phue")
@mock.patch("action.Converter")
def test_change_light_color(self, Converter, mockedPhue):

xyValue = [0.1, 0.2]

converter = mock.MagicMock()
Converter.return_value = converter
converter.hex_to_xy.return_value = xyValue

light = mock.MagicMock()
lights = {
"Lounge Lamp": light
}
bridge = mock.MagicMock()
bridge.get_light_objects.return_value = lights
mockedPhue.Bridge.return_value = bridge

action.ChangeLightColor(self._say, "philips-hue", "Lounge Lamp", "0077be").run()

mockedPhue.Bridge.assert_called_with("philips-hue")
bridge.connect.assert_called()
bridge.get_light_objects.assert_called_with("name")
self.assertEqual(light.on, True)
converter.hex_to_xy.assert_called_with("0077be")
self.assertEqual(light.xy, xyValue)
self.assertEqual(self._say_text, "Ok")


if __name__ == "__main__":
unittest.main()

0 comments on commit 9571936

Please sign in to comment.