diff --git a/homeassistant/components/__init__.py b/homeassistant/components/__init__.py index 3cb5f49939b..73fb18f1537 100644 --- a/homeassistant/components/__init__.py +++ b/homeassistant/components/__init__.py @@ -59,7 +59,7 @@ def is_on(hass, entity_id=None): if entity_id: group = get_component('group') - entity_ids = group.expand_entity_ids([entity_id]) + entity_ids = group.expand_entity_ids(hass, [entity_id]) else: entity_ids = hass.states.entity_ids @@ -80,13 +80,19 @@ def is_on(hass, entity_id=None): return False -def turn_on(hass, **service_data): +def turn_on(hass, entity_id=None, **service_data): """ 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) -def turn_off(hass, **service_data): +def turn_off(hass, entity_id=None, **service_data): """ 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) @@ -169,7 +175,6 @@ def setup(hass, config): def handle_turn_service(service): """ Method to handle calls to homeassistant.turn_on/off. """ - entity_ids = extract_entity_ids(hass, service) # Generic turn on/off method requires entity id diff --git a/homeassistant/test.py b/homeassistant/test.py index c141a6a9946..e960596a58d 100644 --- a/homeassistant/test.py +++ b/homeassistant/test.py @@ -20,6 +20,7 @@ import homeassistant as ha import homeassistant.loader as loader import homeassistant.util as util import homeassistant.remote as remote +import homeassistant.components as comps import homeassistant.components.http as http API_PASSWORD = "test1234" @@ -456,6 +457,63 @@ class TestUtil(unittest.TestCase): 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): """ Test the HTTP debug interface and API. """