Skip to content

Commit 0d79b8e

Browse files
committed
added locks
1 parent e480ceb commit 0d79b8e

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ switch:
9191
name: Switch 2
9292
```
9393

94+
To add a virtual lock use the following:
95+
96+
```yaml
97+
lock:
98+
- platform: virtual
99+
name: Front Door Lock
100+
```
101+
94102
To add a virtual device tracker use the following:
95103

96104
```yaml

changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
0.3: added locks
2+
0.2: added device trackers
13
0.1: initial release
24

custom_components/virtual/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77

88

9-
__version__ = '0.1'
9+
__version__ = '0.3'
1010

1111
_LOGGER = logging.getLogger(__name__)
1212

custom_components/virtual/lock.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
This component provides support for a virtual lock.
3+
4+
"""
5+
6+
import logging
7+
8+
import voluptuous as vol
9+
10+
import homeassistant.helpers.config_validation as cv
11+
from homeassistant.components.lock import LockDevice
12+
from homeassistant.helpers.config_validation import (PLATFORM_SCHEMA)
13+
14+
_LOGGER = logging.getLogger(__name__)
15+
16+
17+
CONF_NAME = "name"
18+
CONF_INITIAL_VALUE = "initial_value"
19+
20+
DEFAULT_INITIAL_VALUE = "locked"
21+
22+
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
23+
vol.Required(CONF_NAME): cv.string,
24+
vol.Optional(CONF_INITIAL_VALUE, default=DEFAULT_INITIAL_VALUE): cv.string,
25+
})
26+
27+
28+
async def async_setup_platform(_hass, config, async_add_entities, _discovery_info=None):
29+
locks = [VirtualLock(config)]
30+
async_add_entities(locks, True)
31+
32+
33+
class VirtualLock(LockDevice):
34+
"""Representation of a Virtual lock."""
35+
36+
def __init__(self, config):
37+
"""Initialize the Virtual lock device."""
38+
self._name = config.get(CONF_NAME)
39+
self._unique_id = self._name.lower().replace(' ', '_')
40+
self._state = config.get(CONF_INITIAL_VALUE)
41+
_LOGGER.info('VirtualLock: {} created'.format(self._name))
42+
43+
@property
44+
def unique_id(self):
45+
"""Return a unique ID."""
46+
return self._unique_id
47+
48+
@property
49+
def state(self):
50+
"""Return the state of the lock."""
51+
return self._state
52+
53+
@property
54+
def is_locked(self):
55+
"""Return true if lock is on."""
56+
return self.state == "locked"
57+
58+
def lock(self, **kwargs):
59+
self._state = 'locked'
60+
61+
def unlock(self, **kwargs):
62+
self._state = 'unlocked'
63+
64+
@property
65+
def device_state_attributes(self):
66+
"""Return the device state attributes."""
67+
attrs = {
68+
'friendly_name': self._name,
69+
'unique_id': self._unique_id,
70+
}
71+
return attrs
72+

0 commit comments

Comments
 (0)