mirror of
https://github.com/home-assistant/core
synced 2024-10-05 05:22:27 +00:00
Added tests for switch component
This commit is contained in:
parent
8c56b415cb
commit
5cbe7bf1b8
|
@ -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. """
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
54
test/mock/switch_platform.py
Normal file
54
test/mock/switch_platform.py
Normal file
|
@ -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
|
99
test/test_component_switch.py
Normal file
99
test/test_component_switch.py
Normal file
|
@ -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)
|
|
@ -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'))
|
||||
|
|
Loading…
Reference in a new issue