Add tests for lock component

pull/683/head
Paulus Schoutsen 2015-11-29 11:44:27 -08:00
parent 7d503e3f8b
commit 8841eef2b7
4 changed files with 57 additions and 20 deletions

View File

@ -36,10 +36,6 @@ DISCOVERY_PLATFORMS = {
wink.DISCOVER_LOCKS: 'wink' wink.DISCOVER_LOCKS: 'wink'
} }
PROP_TO_ATTR = {
'locked': ATTR_LOCKED
}
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -5,25 +5,23 @@ homeassistant.components.lock.demo
Demo platform that has two fake locks. Demo platform that has two fake locks.
""" """
from homeassistant.components.lock import LockDevice from homeassistant.components.lock import LockDevice
from homeassistant.const import ( from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED
DEVICE_DEFAULT_NAME, STATE_LOCKED, STATE_UNLOCKED)
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Find and return demo locks. """ """ Find and return demo locks. """
add_devices_callback([ add_devices_callback([
DemoLock('Left Door', STATE_LOCKED, None), DemoLock('Front Door', STATE_LOCKED),
DemoLock('Right Door', STATE_UNLOCKED, None) DemoLock('Kitchen Door', STATE_UNLOCKED)
]) ])
class DemoLock(LockDevice): class DemoLock(LockDevice):
""" Provides a demo lock. """ """ Provides a demo lock. """
def __init__(self, name, state, icon): def __init__(self, name, state):
self._name = name or DEVICE_DEFAULT_NAME self._name = name
self._state = state self._state = state
self._icon = icon
@property @property
def should_poll(self): def should_poll(self):
@ -35,18 +33,10 @@ class DemoLock(LockDevice):
""" Returns the name of the device if any. """ """ Returns the name of the device if any. """
return self._name return self._name
@property
def icon(self):
""" Returns the icon to use for device if any. """
return self._icon
@property @property
def is_locked(self): def is_locked(self):
""" True if device is locked. """ """ True if device is locked. """
if self._state == STATE_LOCKED: return self._state == STATE_LOCKED
return True
else:
return False
def lock(self, **kwargs): def lock(self, **kwargs):
""" Lock the device. """ """ Lock the device. """

View File

View File

@ -0,0 +1,51 @@
"""
tests.components.lock.test_demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests demo lock component.
"""
import unittest
import homeassistant.core as ha
from homeassistant.components import lock
FRONT = 'lock.front_door'
KITCHEN = 'lock.kitchen_door'
class TestLockDemo(unittest.TestCase):
""" Test the demo lock. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.assertTrue(lock.setup(self.hass, {
'lock': {
'platform': 'demo'
}
}))
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
self.hass.stop()
def test_is_locked(self):
self.assertTrue(lock.is_locked(self.hass, FRONT))
self.hass.states.is_state(FRONT, 'locked')
self.assertFalse(lock.is_locked(self.hass, KITCHEN))
self.hass.states.is_state(KITCHEN, 'unlocked')
def test_locking(self):
lock.lock(self.hass, KITCHEN)
self.hass.pool.block_till_done()
self.assertTrue(lock.is_locked(self.hass, KITCHEN))
def test_unlocking(self):
lock.unlock(self.hass, FRONT)
self.hass.pool.block_till_done()
self.assertFalse(lock.is_locked(self.hass, FRONT))