Skip to content

Commit

Permalink
Add missing docstrings (fix PEP257 issues) (#3098)
Browse files Browse the repository at this point in the history
* Add missing docstrings (fix PEP257 issues)

* Finish sentence
  • Loading branch information
fabaff authored and robbiet480 committed Sep 2, 2016
1 parent 5222faa commit 7557285
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions tests/components/test_emulated_hue.py
@@ -1,3 +1,4 @@
"""The tests for the emulated Hue component."""
import time
import json
import threading
Expand All @@ -11,8 +12,7 @@
from homeassistant.components import emulated_hue, http, light, mqtt
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.components.emulated_hue import (
HUE_API_STATE_ON, HUE_API_STATE_BRI
)
HUE_API_STATE_ON, HUE_API_STATE_BRI)

from tests.common import get_test_instance_port, get_test_home_assistant

Expand All @@ -27,19 +27,22 @@


def setUpModule():
"""Setup things to be run when tests are started."""
global mqtt_broker

mqtt_broker = MQTTBroker('127.0.0.1', MQTT_BROKER_PORT)
mqtt_broker.start()


def tearDownModule():
"""Stop everything that was started."""
global mqtt_broker

mqtt_broker.stop()


def setup_hass_instance(emulated_hue_config):
"""Setup the Home Assistant instance to test."""
hass = get_test_home_assistant()

# We need to do this to get access to homeassistant/turn_(on,off)
Expand All @@ -55,15 +58,19 @@ def setup_hass_instance(emulated_hue_config):


def start_hass_instance(hass):
"""Start the Home Assistant instance to test."""
hass.start()
time.sleep(0.05)


class TestEmulatedHue(unittest.TestCase):
"""Test the emulated Hue component."""

hass = None

@classmethod
def setUpClass(cls):
"""Setup the class."""
cls.hass = setup_hass_instance({
emulated_hue.DOMAIN: {
emulated_hue.CONF_LISTEN_PORT: BRIDGE_SERVER_PORT
Expand All @@ -73,9 +80,11 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
"""Stop the class."""
cls.hass.stop()

def test_description_xml(self):
"""Test the description."""
import xml.etree.ElementTree as ET

result = requests.get(
Expand All @@ -91,6 +100,7 @@ def test_description_xml(self):
self.fail('description.xml is not valid XML!')

def test_create_username(self):
"""Test the creation of an username."""
request_json = {'devicetype': 'my_device'}

result = requests.post(
Expand All @@ -107,6 +117,7 @@ def test_create_username(self):
self.assertTrue('username' in success_json['success'])

def test_valid_username_request(self):
"""Test request with a valid username."""
request_json = {'invalid_key': 'my_device'}

result = requests.post(
Expand All @@ -117,8 +128,11 @@ def test_valid_username_request(self):


class TestEmulatedHueExposedByDefault(unittest.TestCase):
"""Test class for emulated hue component."""

@classmethod
def setUpClass(cls):
"""Setup the class."""
cls.hass = setup_hass_instance({
emulated_hue.DOMAIN: {
emulated_hue.CONF_LISTEN_PORT: BRIDGE_SERVER_PORT,
Expand Down Expand Up @@ -177,9 +191,11 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
"""Stop the class."""
cls.hass.stop()

def test_discover_lights(self):
"""Test the discovery of lights."""
result = requests.get(
BRIDGE_URL_BASE.format('/api/username/lights'), timeout=5)

Expand All @@ -194,6 +210,7 @@ def test_discover_lights(self):
self.assertTrue('light.kitchen_light' not in result_json)

def test_get_light_state(self):
"""Test the getting of light state."""
# Turn office light on and set to 127 brightness
self.hass.services.call(
light.DOMAIN, const.SERVICE_TURN_ON,
Expand Down Expand Up @@ -229,6 +246,7 @@ def test_get_light_state(self):
self.assertEqual(kitchen_result.status_code, 404)

def test_put_light_state(self):
"""Test the seeting of light states."""
self.perform_put_test_on_office_light()

# Turn the bedroom light on first
Expand Down Expand Up @@ -264,6 +282,7 @@ def test_put_light_state(self):
self.assertEqual(kitchen_result.status_code, 404)

def test_put_with_form_urlencoded_content_type(self):
"""Test the form with urlencoded content."""
# Needed for Alexa
self.perform_put_test_on_office_light(
'application/x-www-form-urlencoded')
Expand All @@ -278,6 +297,7 @@ def test_put_with_form_urlencoded_content_type(self):
self.assertEqual(result.status_code, 400)

def test_entity_not_found(self):
"""Test for entity which are not found."""
result = requests.get(
BRIDGE_URL_BASE.format(
'/api/username/lights/{}'.format("not.existant_entity")),
Expand All @@ -293,6 +313,7 @@ def test_entity_not_found(self):
self.assertEqual(result.status_code, 404)

def test_allowed_methods(self):
"""Test the allowed methods."""
result = requests.get(
BRIDGE_URL_BASE.format(
'/api/username/lights/{}/state'.format("light.office_light")))
Expand All @@ -313,6 +334,7 @@ def test_allowed_methods(self):
self.assertEqual(result.status_code, 405)

def test_proper_put_state_request(self):
"""Test the request to set the state."""
# Test proper on value parsing
result = requests.put(
BRIDGE_URL_BASE.format(
Expand All @@ -334,6 +356,7 @@ def test_proper_put_state_request(self):

def perform_put_test_on_office_light(self,
content_type='application/json'):
"""Test the setting of a light."""
# Turn the office light off first
self.hass.services.call(
light.DOMAIN, const.SERVICE_TURN_OFF,
Expand Down Expand Up @@ -361,6 +384,7 @@ def perform_put_test_on_office_light(self,
self.assertEqual(office_light.attributes[light.ATTR_BRIGHTNESS], 56)

def perform_get_light_state(self, entity_id, expected_status):
"""Test the gettting of a light state."""
result = requests.get(
BRIDGE_URL_BASE.format(
'/api/username/lights/{}'.format(entity_id)), timeout=5)
Expand All @@ -377,6 +401,7 @@ def perform_get_light_state(self, entity_id, expected_status):

def perform_put_light_state(self, entity_id, is_on, brightness=None,
content_type='application/json'):
"""Test the setting of a light state."""
url = BRIDGE_URL_BASE.format(
'/api/username/lights/{}/state'.format(entity_id))

Expand Down Expand Up @@ -432,6 +457,7 @@ def stop(self):
self._thread.join()

def _run_loop(self):
"""Run the loop."""
asyncio.set_event_loop(self._loop)
self._loop.run_until_complete(self._broker_coroutine())

Expand All @@ -442,4 +468,5 @@ def _run_loop(self):

@asyncio.coroutine
def _broker_coroutine(self):
"""The Broker coroutine."""
yield from self._broker.start()

0 comments on commit 7557285

Please sign in to comment.