Added test coverage for core components

This commit is contained in:
Paulus Schoutsen 2014-11-23 00:31:09 -08:00
parent ec59c3c793
commit 5278fe2f47
2 changed files with 67 additions and 4 deletions

View File

@ -59,7 +59,7 @@ def is_on(hass, entity_id=None):
if entity_id: if entity_id:
group = get_component('group') group = get_component('group')
entity_ids = group.expand_entity_ids([entity_id]) entity_ids = group.expand_entity_ids(hass, [entity_id])
else: else:
entity_ids = hass.states.entity_ids entity_ids = hass.states.entity_ids
@ -80,13 +80,19 @@ def is_on(hass, entity_id=None):
return False return False
def turn_on(hass, **service_data): def turn_on(hass, entity_id=None, **service_data):
""" Turns specified entity on if possible. """ """ Turns specified entity on if possible. """
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.call_service(ha.DOMAIN, SERVICE_TURN_ON, service_data) hass.call_service(ha.DOMAIN, SERVICE_TURN_ON, service_data)
def turn_off(hass, **service_data): def turn_off(hass, entity_id=None, **service_data):
""" Turns specified entity off. """ """ Turns specified entity off. """
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.call_service(ha.DOMAIN, SERVICE_TURN_OFF, service_data) hass.call_service(ha.DOMAIN, SERVICE_TURN_OFF, service_data)
@ -169,7 +175,6 @@ def setup(hass, config):
def handle_turn_service(service): def handle_turn_service(service):
""" Method to handle calls to homeassistant.turn_on/off. """ """ Method to handle calls to homeassistant.turn_on/off. """
entity_ids = extract_entity_ids(hass, service) entity_ids = extract_entity_ids(hass, service)
# Generic turn on/off method requires entity id # Generic turn on/off method requires entity id

View File

@ -20,6 +20,7 @@ import homeassistant as ha
import homeassistant.loader as loader import homeassistant.loader as loader
import homeassistant.util as util import homeassistant.util as util
import homeassistant.remote as remote import homeassistant.remote as remote
import homeassistant.components as comps
import homeassistant.components.http as http import homeassistant.components.http as http
API_PASSWORD = "test1234" API_PASSWORD = "test1234"
@ -456,6 +457,63 @@ class TestUtil(unittest.TestCase):
util.ensure_unique_string("Beer", ["Beer", "Beer_2"])) util.ensure_unique_string("Beer", ["Beer", "Beer_2"]))
class TestComponentsCore(unittest.TestCase):
""" Tests homeassistant.components module. """
def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = ha.HomeAssistant()
loader.prepare(self.hass)
self.assertTrue(comps.setup(self.hass, {}))
self.hass.states.set('light.Bowl', comps.STATE_ON)
self.hass.states.set('light.Ceiling', comps.STATE_OFF)
def test_is_on(self):
""" Test is_on method. """
self.assertTrue(comps.is_on(self.hass, 'light.Bowl'))
self.assertFalse(comps.is_on(self.hass, 'light.Ceiling'))
self.assertTrue(comps.is_on(self.hass))
def test_turn_on(self):
""" Test turn_on method. """
runs = []
self.hass.services.register(
'light', comps.SERVICE_TURN_ON, lambda x: runs.append(1))
comps.turn_on(self.hass, 'light.Ceiling')
self.hass._pool.block_till_done()
self.assertEqual(1, len(runs))
def test_turn_off(self):
""" Test turn_off method. """
runs = []
self.hass.services.register(
'light', comps.SERVICE_TURN_OFF, lambda x: runs.append(1))
comps.turn_off(self.hass, 'light.Bowl')
self.hass._pool.block_till_done()
self.assertEqual(1, len(runs))
def test_extract_entity_ids(self):
""" Test extract_entity_ids method. """
call = ha.ServiceCall('light', 'turn_on',
{comps.ATTR_ENTITY_ID: 'light.Bowl'})
self.assertEqual(['light.Bowl'],
comps.extract_entity_ids(self.hass, call))
call = ha.ServiceCall('light', 'turn_on',
{comps.ATTR_ENTITY_ID: ['light.Bowl']})
self.assertEqual(['light.Bowl'],
comps.extract_entity_ids(self.hass, call))
class TestHTTP(unittest.TestCase): class TestHTTP(unittest.TestCase):
""" Test the HTTP debug interface and API. """ """ Test the HTTP debug interface and API. """