From 8841eef2b7f039d36dd8b795273d861ad28c67a2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 29 Nov 2015 11:44:27 -0800 Subject: [PATCH] Add tests for lock component --- homeassistant/components/lock/__init__.py | 4 -- homeassistant/components/lock/demo.py | 22 +++------- tests/components/lock/__init__.py | 0 tests/components/lock/test_demo.py | 51 +++++++++++++++++++++++ 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 tests/components/lock/__init__.py create mode 100644 tests/components/lock/test_demo.py diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index fdc2da3e8d4..0d67679e82c 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -36,10 +36,6 @@ DISCOVERY_PLATFORMS = { wink.DISCOVER_LOCKS: 'wink' } -PROP_TO_ATTR = { - 'locked': ATTR_LOCKED -} - _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/lock/demo.py b/homeassistant/components/lock/demo.py index ac7bbed3dc2..472b17f46bf 100644 --- a/homeassistant/components/lock/demo.py +++ b/homeassistant/components/lock/demo.py @@ -5,25 +5,23 @@ homeassistant.components.lock.demo Demo platform that has two fake locks. """ from homeassistant.components.lock import LockDevice -from homeassistant.const import ( - DEVICE_DEFAULT_NAME, STATE_LOCKED, STATE_UNLOCKED) +from homeassistant.const import STATE_LOCKED, STATE_UNLOCKED # pylint: disable=unused-argument def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Find and return demo locks. """ add_devices_callback([ - DemoLock('Left Door', STATE_LOCKED, None), - DemoLock('Right Door', STATE_UNLOCKED, None) + DemoLock('Front Door', STATE_LOCKED), + DemoLock('Kitchen Door', STATE_UNLOCKED) ]) class DemoLock(LockDevice): """ Provides a demo lock. """ - def __init__(self, name, state, icon): - self._name = name or DEVICE_DEFAULT_NAME + def __init__(self, name, state): + self._name = name self._state = state - self._icon = icon @property def should_poll(self): @@ -35,18 +33,10 @@ class DemoLock(LockDevice): """ Returns the name of the device if any. """ return self._name - @property - def icon(self): - """ Returns the icon to use for device if any. """ - return self._icon - @property def is_locked(self): """ True if device is locked. """ - if self._state == STATE_LOCKED: - return True - else: - return False + return self._state == STATE_LOCKED def lock(self, **kwargs): """ Lock the device. """ diff --git a/tests/components/lock/__init__.py b/tests/components/lock/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/components/lock/test_demo.py b/tests/components/lock/test_demo.py new file mode 100644 index 00000000000..7320b1aa69a --- /dev/null +++ b/tests/components/lock/test_demo.py @@ -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))