diff --git a/homeassistant/components/__init__.py b/homeassistant/components/__init__.py index 73fb18f1537..a7eeadabc69 100644 --- a/homeassistant/components/__init__.py +++ b/homeassistant/components/__init__.py @@ -145,7 +145,7 @@ class ToggleDevice(object): def get_state_attributes(self): """ Returns optional state attributes. """ - return None + return {} def update(self): """ Retrieve latest state from the real device. """ diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 8ac877508fa..96325b92fac 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -129,7 +129,7 @@ def setup(hass, config): switch.update_ha_state(hass) - # Track all wemos in a group + # Track all switches in a group group.setup_group(hass, GROUP_NAME_ALL_SWITCHES, ent_to_switch.keys(), False) diff --git a/homeassistant/loader.py b/homeassistant/loader.py index ebf23653941..87a76d9eb7d 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -63,6 +63,11 @@ def prepare(hass): 'custom_components.{}'.format(fil[0:-3])) +def set_component(comp_name, component): + """ Sets a component in the cache. """ + _COMPONENT_CACHE[comp_name] = component + + def get_component(comp_name): """ Tries to load specified component. Looks in config dir first, then built-in components. diff --git a/test/mock/switch_platform.py b/test/mock/switch_platform.py new file mode 100644 index 00000000000..c49f6221e91 --- /dev/null +++ b/test/mock/switch_platform.py @@ -0,0 +1,54 @@ +""" +test.mock.switch_platform +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Provides a mock switch platform. +""" +import homeassistant.components as components + + +class MockSwitch(components.ToggleDevice): + """ Fake switch. """ + def __init__(self, name, state): + self.name = name + self.state = state + + def get_name(self): + """ Returns the name of the device if any. """ + return self.name + + def turn_on(self, **kwargs): + """ Turn the device on. """ + self.state = components.STATE_ON + + def turn_off(self, **kwargs): + """ Turn the device off. """ + self.state = components.STATE_OFF + + def is_on(self): + """ True if device is on. """ + return self.state == components.STATE_ON + + def get_state_attributes(self): + """ Returns optional state attributes. """ + return {} + + +FAKE_NO_SWITCHES = False + +SWITCHES = [ + MockSwitch('AC', components.STATE_ON), + MockSwitch('AC', components.STATE_OFF) +] + + +def fake_no_switches(do_fake): + """ Set the platform to act as if it has no switches. """ + global FAKE_NO_SWITCHES + + FAKE_NO_SWITCHES = do_fake + + +def get_switches(hass, config): + """ Returns mock switches. """ + return [] if FAKE_NO_SWITCHES else SWITCHES diff --git a/test/test_component_switch.py b/test/test_component_switch.py new file mode 100644 index 00000000000..7f60d837642 --- /dev/null +++ b/test/test_component_switch.py @@ -0,0 +1,99 @@ +""" +test.test_component_chromecast +~~~~~~~~~~~ + +Tests Chromecast component. +""" +# pylint: disable=too-many-public-methods,protected-access +import unittest + +import homeassistant as ha +import homeassistant.loader as loader +import homeassistant.components as components +import homeassistant.components.switch as switch + +from mock import switch_platform + + +class TestSwitch(unittest.TestCase): + """ Test the switch module. """ + + def setUp(self): # pylint: disable=invalid-name + self.hass = ha.HomeAssistant() + loader.prepare(self.hass) + loader.set_component('switch.test', switch_platform) + + self.assertTrue(switch.setup( + self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}} + )) + + # Switch 1 is ON, switch 2 is OFF + self.switch_1, self.switch_2 = switch_platform.get_switches(None, None) + + def tearDown(self): # pylint: disable=invalid-name + """ Stop down stuff we started. """ + self.hass._pool.stop() + + def test_methods(self): + """ Test is_on, turn_on, turn_off methods. """ + self.assertTrue(switch.is_on(self.hass)) + self.assertEqual( + components.STATE_ON, + self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) + self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id)) + self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) + + switch.turn_off(self.hass, self.switch_1.entity_id) + switch.turn_on(self.hass, self.switch_2.entity_id) + + self.hass._pool.block_till_done() + + self.assertTrue(switch.is_on(self.hass)) + self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id)) + self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) + + # Turn all off + switch.turn_off(self.hass) + + self.hass._pool.block_till_done() + + self.assertFalse(switch.is_on(self.hass)) + self.assertEqual( + components.STATE_OFF, + self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) + self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id)) + self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) + + # Turn all on + switch.turn_on(self.hass) + + self.hass._pool.block_till_done() + + self.assertTrue(switch.is_on(self.hass)) + self.assertEqual( + components.STATE_ON, + self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state) + self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id)) + self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) + + def test_setup(self): + # Bogus config + self.assertFalse(switch.setup(self.hass, {})) + + self.assertFalse(switch.setup(self.hass, {switch.DOMAIN: {}})) + + # Test with non-existing component + self.assertFalse(switch.setup( + self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'nonexisting'}} + )) + + # Test if switch component returns 0 switches + switch_platform.fake_no_switches(True) + + self.assertEqual([], switch_platform.get_switches(None, None)) + + self.assertFalse(switch.setup( + self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}} + )) + + switch_platform.fake_no_switches(False) diff --git a/test/test_loader.py b/test/test_loader.py index 122021afadb..416eb832a7f 100644 --- a/test/test_loader.py +++ b/test/test_loader.py @@ -11,6 +11,8 @@ import homeassistant as ha import homeassistant.loader as loader import homeassistant.components.http as http +from mock import switch_platform + class TestLoader(unittest.TestCase): """ Test the loader module. """ @@ -22,6 +24,13 @@ class TestLoader(unittest.TestCase): """ Stop down stuff we started. """ self.hass._pool.stop() + def test_set_component(self): + """ Test if set_component works. """ + loader.set_component('switch.test', switch_platform) + + self.assertEqual( + switch_platform, loader.get_component('switch.test')) + def test_get_component(self): """ Test if get_component works. """ self.assertEqual(http, loader.get_component('http'))